#include <iostream>
#include <string>
using namespace std;
#define MAX 1000 //通讯录最大容量(可更改)
void PMenu() //菜单
{
cout << "********************************************" << endl;
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;
cout << "********************* 版权所有:尹洪志 ***" << endl;
cout << "********************************************" << endl;
}
struct Person
{
string name; //姓名
int sex; //性别 性别用数字代替,便于检测
int age; //年龄
string namber; //电话号码
string address; //住址
};
struct PersonBooks
{
struct Person per[MAX]; //联系人结构数组
int size; //通讯录人数
};
//******添加联系人
void addR(struct PersonBooks *abs)
{
if (abs->size == MAX) //检测通讯录联系人人数是否达到最大值MAX
{
cout << "抱歉!通讯录已满!" << endl;
return;
}
else //没有满的话,开始添加联系人
{
cout << "请输入姓名:" << endl;
cin >> abs->per[abs->size].name;
cout << "\n请输入性别" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
while (true) /*性别验证*/
{
int sex2;
cin >> sex2;
if (sex2 == 1 || sex2 == 2)
{
abs->per[abs->size].sex = sex2;
break; //性别正确的话,break跳出while循环
}
cout << "您的输入有误,请重新输入。" << endl;
}
cout << "\n请输入年龄:" << endl;
cin >> abs->per[abs->size].age;
cout << "\n请输入电话:" << endl;
cin >> abs->per[abs->size].namber;
cout << "\n请输入住址:" << endl;
cin >> abs->per[abs->size].address;
}
cout << "添加成功" << endl;
abs->size ++; //联系人数量+1,PersonBooks中size(联系人人数)+1
system("pause"); //打印:摁任意键继续
system("cls"); //清屏
}
//******显示联系人
void PrintPerson(const PersonBooks* abs, int a)
{
if (a == 0) //检测通讯录中联系人人数是否为0
{
cout << "当前通讯录无联系人!" << endl;
}
else //联系人数量>=1
{
for (int i = 0; i < a; i++) //for循环 遍历打印结构数组
{
cout << "联系人" << i + 1 << "、 姓名:" << abs->per[i].name
<< " 性别:" << ((abs->per[i].sex > 1)? "女" : "男")
<< " 年龄:" << abs->per[i].age
<< " 电话:" << abs->per[i].namber
<< " 地址:" << abs->per[i].address << endl;
}
}
cout << endl;
system("pause"); //摁任意键继续
system("cls"); //清空屏幕
}
//检验人名存在否?
int Sexit(const PersonBooks* abs, string name)
{
for (int i = 0; i < (abs->size); i++) //遍历结构数组
{
if (abs->per[i].name == name) return i; //判断结构数组中是否有此名name 有的话直接返回i
}
return -1; //没有的话 返回 -1
}
//******删除联系人
void DeleteP(PersonBooks *abs)
{
string name;
cout << "请输入被删除的姓名:" << endl;
cin >> name;
int s = Sexit(abs, name); //检测通讯录中是否有此人
if (s == -1)
{
cout << "查无此人!" << endl;
system("pause");
system("cls");
}
else //通讯录中 有 此人
{
cout << "请确认是否删除\n1 -> 删除\n2 -> 保留" << endl; //*防止误删
int sf;
while (true)
{
cin >> sf;
if (sf == 1 || sf == 2)
{
if (sf == 1) {
for (; s < (abs->size - 1 ); s++) //通过后者赋值给前者,达到-1
{
abs->per[s] = abs->per[s + 1];
}
abs->size--; //PersonBooks中size(联系人数量)-1
cout << "删除联系人成功!" << endl;
}
else cout << "已取消!" << endl;
system("pause");
system("cls");
break;
}
cout << "您的输入有误,请重新输入。" << endl;
}
}
}
//******查找联系人
void SearchP(PersonBooks* abs)
{
string name;
cout << "请输入要查找的姓名:" << endl;
cin >> name;
int temp = Sexit(abs, name);//1.检验此人是否存在 //2.存在 =》 返回 i / 不存在 =》 返回-1
if (temp == -1)
{
cout << "抱歉,查无此人!" << endl;
system("pause");
system("cls");
}
else
{
cout << "姓名:" << name << endl;
cout << "\t性别:" << ((abs->per[temp].sex > 1) ? "女" : "男") << endl;
cout << "\t年龄:" << abs->per[temp].age<< endl;
cout << "\t电话:" << abs->per[temp].namber << endl;
cout << "\t地址:" << abs->per[temp].address << endl;
system("pause");
system("cls");
}
}
//******修改联系人
void Change(PersonBooks* abs)
{
cout << "请输入要修改的姓名:" << endl;
string name;
cin >> name;
int temp = Sexit(abs, name); //检测通讯录中是否有此人
if (temp == -1)
{
cout << "抱歉,查无此人!" << endl;
system("pause");
system("cls");
}
else
{
string t_sex, t_name, t_namber, t_address; //定义临时变量,获取用户输入的内容
int t_sex2,t_age; //定义临时变量,获取用户输入的内容
cout << "您要修改的联系人信息为:" << endl; //打印 修改 前 信息
cout << "姓名:" << abs->per[temp].name
<< " \t性别:" << ((abs->per[temp].sex > 1) ? "女" : "男")
<< " \t年龄:" << abs->per[temp].age
<< " \t电话:" << abs->per[temp].namber
<< " \t地址:" << abs->per[temp].address << endl;
cout << "\n请输入您修改后的信息:(两个信息之间以 空格 隔开)" << endl; //获取用户输入的信息
cout << "姓名: \t性别: \t年龄: \t电话: \t地址:" << endl;
cin >> t_name >> t_sex >> t_age >> t_namber >> t_address;
if (t_sex == "男") t_sex2 = 1; //性别从1-男 之间转换
else t_sex2 = 2;
//cout << t_name << t_sex << t_age << t_namber << t_address << endl;
abs->per[temp].name = t_name;
abs->per[temp].sex = t_sex2;
abs->per[temp].age = t_age;
abs->per[temp].namber = t_namber;
abs->per[temp].address = t_address;
cout << "修改成功!" << endl;
system("pause");
system("cls");
}
}
//******清空联系人
void AllDelete(PersonBooks* abs)
{
if (abs->size == 0) //当通讯录中无联系人 时
{
cout << "您的通讯录里 无联系人 可清空" << endl;
system("pause");
system("cls");
}
else {
cout << "请确认是否清空通讯录?\n1 -> 删除\n2 -> 保留" << endl; //防止误清,二次确认
int sf;
while (true)
{
cin >> sf;
if (sf == 1 || sf == 2)
{
if (sf == 1)
{
abs->size = 0;
cout << "通讯录清空成功!" << endl;
}
else cout << "已取消!" << endl;
system("pause");
system("cls");
break;
}
cout << "您的输入有误,请重新输入。" << endl;
}
}
}
void main()
{
PersonBooks abs; //定义结构变量
abs.size = 0; //size(联系人数量)初始化
while (true)
{
PMenu();
int choose;
cin >> choose; //用户输入目录的选择
switch (choose)
{
case 1: //******添加联系人
addR(&abs);
break;
case 2: //******显示联系人
PrintPerson(&abs,abs.size);
break;
case 3: //******删除联系人
DeleteP(&abs);
break;
case 4: //******查找联系人
SearchP(&abs);
break;
case 5: //******修改联系人
Change(&abs);
break;
case 6: //******清空联系人
AllDelete(&abs);
break;
case 0:
cout << "欢迎您再次使用!" << endl;
return ;
default:
cout << "您输入有误,请重新输入:" << endl;
break;
}
}
system("pause");
}
|