C++ 通讯录
/*编写调试环境VS2019*/#include <iostream>
#include <string>
using namespace std;
const intMAX = 100;
//设计联系人结构体
struct Person
{
string m_Name; //姓名
int m_Sex; //性别 1 男2 女
int m_Age; //年龄
string m_Phone; //手机号码
string m_Address; //地址
};
struct Addressbooks
{
struct Person personArray; //通讯录中保存的联系人数组
int m_Size; //通讯录当前人的个数
};
/*菜单界面*/
void showMenu()
{
cout << "***************************" << endl;
cout << "*****1、添加联系人*****" << endl;
cout << "*****2、显示联系人*****" << endl;
cout << "*****3、删除联系人*****" << endl;
cout << "*****4、查找联系人*****" << endl;
cout << "*****5、修改联系人*****" << endl;
cout << "*****6、清空联系人*****" << endl;
cout << "*****0、退出通讯录*****" << endl;
cout << "***************************" << endl;
}
// 1、添加联系人
void addPerson(Addressbooks*abs)
{
//判断通讯录是否已满,如果满了就不再添加
if (abs->m_Size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray.m_Name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int sex = 0;
while (true)
{
cin >> sex; //如果输入是1或者2可以退出循环,如果有误重新输入
if (sex == 1 || sex == 2)
{
abs->personArray.m_Sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
cout << "请输入年龄: " << endl;
int age = 0;
while (true)
{
cin >> age;
if(age > 0 && age < 100) //年龄输入必须在 0 < age < 100范围内
{
abs->personArray.m_Age = age;
break;
}
cout << "年龄输入不符合实际,请重新输入" << endl;
}
//电话
cout << "请输入联系电话: " << endl;
string phone;
cin >> phone;
abs->personArray.m_Phone = phone;
//地址
string address;
cout << "请输入家庭住址: " << endl;
cin >> address;
abs->personArray.m_Address = address;
//更新通讯录人数
abs->m_Size++;
cout << "添加成功" << endl;
system("cls"); //清屏
}
}
// 2、显示所有联系人
void showPerson(const Addressbooks* abs)
{
//判断通讯录中人数是否为0,如果为0,提示记录为空
//如果不为0,显示记录的联系人信息
if (abs->m_Size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名: " << abs->personArray.m_Name << "\t";
cout << "性别: " << (abs->personArray.m_Sex == 1 ? "男":"女") << "\t";
cout << "年龄: " << abs->personArray.m_Age << "\t";
cout << "电话: " << abs->personArray.m_Phone << "\t";
cout << "住址: " << abs->personArray.m_Address << endl;
}
}
system("pause");
system("cls");
}
//查找联系人是否存在,如果存在返回联系人具体位置,不存在返回-1
//参数1 被查找的通讯录 参数2 查找的人名
int isExist(Addressbooks* abs, string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArray.m_Name == name) //找到返回被查找联系人位置
return i;
}
return -1;
}
//3、删除联系人
void delPerson(Addressbooks* abs)
{
cout << "请输入你要删除的联系人" << endl;
string name;
cin >> name;
// ret == -1 查无此人
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_Size; i++)
{
//数据前移
abs->personArray = abs->personArray;
}
abs->m_Size--; //更新通讯录人数
cout << "删除联系人成功" << endl;
}
else
{
cout << "查无此人,无法删除" << endl;
}
system("pause");
system("cls");
}
//4、查找联系人
void findPerson(Addressbooks* abs)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
//判断联系人是否存在通讯录中
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "姓名: " << abs->personArray.m_Name << "\t";
cout << "性别: " << (abs->personArray.m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄: " << abs->personArray.m_Age << "\t";
cout << "电话: " << abs->personArray.m_Phone << "\t";
cout << "住址: " << abs->personArray.m_Address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//5、修改联系人
void modifyPerson(Addressbooks* abs)
{
cout << "请输入要修改的联系人姓名:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
string m_name;
cout << "请输入联系人姓名:" << endl;
cin >> m_name;
abs->personArray.m_Name = m_name;
int m_sex;
cout << "请输入性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
cin >> m_sex;
abs->personArray.m_Sex = m_sex;
cout << "请输入年龄:" << endl;
int m_age;
cin >> m_age;
abs->personArray.m_Age = m_age;
string m_phone;
cout << "请输入号码:" << endl;
cin >> m_phone;
abs->personArray.m_Phone = m_phone;
cout << "请输入住址:" << endl;
string m_addres;
cin >> m_addres;
abs->personArray.m_Address = m_addres;
cout << "修改联系人成功!" << endl;
}
else
{
cout << "查无此人,无法修改联系人" << endl;
}
system("pause");
system("cls");
}
// 6、清空联系人
void cleanPerson(Addressbooks* abs)
{
int flag = 0;
cout << "请再次确认是否清空通讯录!" << endl;
cout << "1 --- 确定" << endl;
cout << "2 --- 取消" << endl;
while (true)
{
cin >> flag;
if (flag == 1 || flag == 2)
{
if (flag == 1)
{
abs->m_Size = 0; //将当前联系人数量置为零
cout << "通讯录已经清空" << endl;
break;
}
else
{
cout << "通讯录清空已取消" << endl;
break;
}
}
else
{
cout << "输入非法,请重新输入1或2" << endl;
}
}
system("pause");
system("cls");
}
int main()
{
Addressbooks abs; //创建通讯录结构体变量
abs.m_Size = 0;
int select = 0; //创建用户输入的变量
while (true)
{
showMenu(); //菜单调用
cin >> select;
switch (select)
{
case 1: //添加联系人
addPerson(&abs);
break;
case 2: //显示联系人
showPerson(&abs);
break;
case 3: //删除联系人
delPerson(&abs);
break;
case 4: //查找联系人
findPerson(&abs);
break;
case 5: //修改联系人
modifyPerson(&abs);
break;
case 6: //清空联系人
cleanPerson(&abs);
break;
case 0: //退出通讯录
cout << "欢迎下次使用" << endl;
return 0;
break;
}
}
return 0;
} {:10_254:}
页:
[1]