#include<iostream>
using namespace std;
#define max 1000
#include<string.h>
//显示菜单界面;
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;
}
struct person
{
string name;
int sex;//1,男 2女;
int age;
string number;
string home;
};
struct addressbooks
{
struct person arrange[max];//保存数组;
int size;//记录个数
};
//1,添加联系人
void addperson(addressbooks* abs)
{
//判断通讯录是否满了
if (abs->size == max)
{
cout << "通讯已满,无法添加" << endl;
return;
}
else
{
//添加联系人
//姓名
string name;
cout << "请输入姓名" << endl;
cin >> name;
abs->arrange[abs->size].name = name;
//年龄,电话,性别。
cout << "请输入性别" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex = 0;
while (true) {
cin >> sex;//不是1就会退出循环
if (sex == 1 || sex == 2)
{
abs->arrange[abs->size].sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->arrange[abs->size].age = age;
cout << "输入电话" << endl;
string phone;
cin >> phone;
abs->arrange[abs->size].number = phone;
cout << "请输入地址" << endl;
string address;
cin >> address;
abs->arrange[abs->size].home = address;
abs->size++;
cout << "添加成功" << endl;
system("pause");
system("cls");//清屏
}
}
//2,显示联系人
void showperson(addressbooks* abs)//为零提示为空//不为0,显示信息
{
if (abs->size==0)//判断人数是不是0;
{
cout << "当前为空" << endl;
}
else
{
for (int i = 0; i < abs->size; i++)
{
cout << "姓名:" << abs->arrange[i].name <<"\t";
cout << "性别:" <<( abs->arrange[i].sex==1?"男":"女") <<"\t";
cout << "年龄:" << abs->arrange[i].age <<"\t";
cout << "电话:" << abs->arrange[i].number<<"\t";
cout << "住址:" << abs->arrange[i].home << endl;
}
}
system("pause");
system("cls");
}//检测联系人是否存在
int isexit(addressbooks* abs, string name)
{
for (int i = 0; i < abs->size; i++)
{
if (abs->arrange[i].name == name)
{
return i;//找到了,返回下标;
}
}
return -1;
}
void outperson(addressbooks *abs)
{
cout << "请输入你要删除的联系人" << endl;
string name;
cin >> name;
isexit(abs, name);
//ret==-1//没有这个人
int ret = isexit(abs, name);
if (ret != -1)
{
//找到人
for (int i = ret; i < abs->size; i++)
{
abs->arrange[i] = abs->arrange[i + 1];
}
abs->size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void findperson(addressbooks *abs)
{
cout << "请输入查找的纤细人" << endl;
string name;
cin >> name;
//判断联系人是否存在
int ret = isexit(abs, name);
if (ret!= -1)
{
cout << "姓名" << abs->arrange[ret].name<< "\t";
cout << "性别" << abs->arrange[ret].sex << "\t";
cout << "年龄" << abs->arrange[ret].age<< "\t";
cout << "电话" << abs->arrange[ret].number << "\t";
cout << "住址" << abs->arrange[ret].home << "\t";
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void changeperson(addressbooks* abs)
{
cout << "请输入修改联系人" << endl;
string name;
cin >> name;
int ret = isexit(abs, name);
if (ret != -1)
{
string name;
cout << "请输入新的姓名" << endl;
cin >> name;
abs->arrange[ret].name = name;
cout << "请新的输入性别" << endl;
cout << "1------男" << endl;
cout << "2------女" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->arrange[ret].sex = sex;
break;
}
cout << "输入有误" << endl;
}
cout << "请输入新的年龄" << endl;
int age;
cin >> age;
abs->arrange[ret].age = age;
cout << "请输入新的电话" << endl;
string number;
cin >> number;
abs->arrange[ret].number = number;
cout << "请输入新的地址" << endl;
string home;
cin >> home;
abs->arrange[ret].home = home;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void killallperson(addressbooks* abs)
{
abs->size = 0;
cout << "已经清空" << endl;
system("pause");
system("cls");
}
int main()
{//创建通讯录变量
struct addressbooks abs;
abs.size = 0;
int select = 0;
while (true) {
showmenu();
cin >> select;
switch (select)
{
case 1://添加联系人
addperson(&abs);//利用地址传递
break;
case 2://显示联系人
showperson(&abs);
break;
case 3://删除联系人 {
/*{
cout << "请输入删除联系人的姓名" << endl;
string name;
cin >> name;
isexit(&abs, name);
if (isexit(&abs, name) == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "找到此人" << endl;
}
break; }*/
outperson(&abs);
break;
case 4://查找联系人
findperson(&abs);
break;
case 5://修改联系人
changeperson(&abs);
break;
case 6://清空联系人
killallperson(&abs);
break;
case 0://退出通讯录
cout << "欢迎下次使用" << endl;
return 0;
default:
cout << "请输入对应的数字" << endl;
break;
}
}
system("pause");
return 0;
}