#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
struct person {
string name;
int sex; //1:男 2:女
int age;
string number;
string address;
};
struct book
{
struct person personArray[MAX];
int 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;
}
void addPerson(struct book *book1)
{
if(book1->size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
string name;
cout << "请输入联系人姓名:" << endl;
cin >> name;
book1->personArray[book1->size].name = name;
cout << "请输入联系人性别(1:男 2:女):" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
book1->personArray[book1->size].sex = sex;
break;
}
cout << "输入有误,请重新输入!" << endl;
}
cout << "请输入联系人年龄:" << endl;
int age = 0;
cin >> age;
book1->personArray[book1->size].age = age;
cout << "请输入联系人电话:" << endl;
string number;
cin >> number;
book1->personArray[book1->size].number = number;
cout << "请输入联系人地址:" << endl;
string address;
cin >> address;
book1->personArray[book1->size].address = address;
book1->size++;
cout << "添加成功" << endl;
system("pause");//按任意键结束
system("cls"); //清屏操作
}
}
void showPerson(struct book *book1)
{
if (book1->size == 0)
{
cout << "当前记录为空!" << endl;
system("pause");
system("cls");
}
else
{
for (int i = 0; i < book1->size; i++)
{
cout << "姓名:" << book1->personArray[i].name << "\t";
cout << "性别:" << (book1->personArray[i].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << book1->personArray[i].age << "\t";
cout << "电话:" << book1->personArray[i].number << "\t";
cout << "地址:" << book1->personArray[i].address << endl;
}
system("pause");
system("cls");
}
}
int isExist(struct book *book1, string name)
{
for (int i = 0; i < book1->size; i++)
{
if (book1->personArray[i].name == name)
return i;
}
return -1;
}
void deletePreson(struct book *book1)
{
cout << "请输入你要删除的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(book1, name);
if (ret == -1)
cout << "查无此人" << endl;
else
{
for (int i = ret; i < book1->size - 1; i++)
{
book1->personArray[i] = book1->personArray[i + 1];
}
book1->size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void findPerson(struct book *book1)
{
cout << "请输入您要查找的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(book1,name);
if (ret != -1)
{
cout << "姓名:" << book1->personArray[ret].name << "\t";
cout << "性别:" << (book1->personArray[ret].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << book1->personArray[ret].age << "\t";
cout << "电话:" << book1->personArray[ret].number << "\t";
cout << "地址:" << book1->personArray[ret].address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void modifyPerson(struct book *book1)
{
cout << "请输入您要修改的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(book1,name);
if (ret != -1)
{
cout << "请问您打算修改哪一项:(1.姓名 2.性别 3.年龄 4.电话 5.地址)" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
cout << "请输入姓名:" << endl;
string name1;
cin >> name1;
book1->personArray[ret].name = name1;
break;
}
case 2:
{
cout << "请输入性别:" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
book1->personArray[ret].sex = sex;
break;
}
else
cout << "请重新输入!" << endl;
}
}
case 3:
{
cout << "请输入年龄:" << endl;
int age;
cin >> age;
book1->personArray[ret].age = age;
break;
}
case 4:
{
cout << "请输入电话:" << endl;
int number;
cin >> number;
book1->personArray[ret].number = number;
break;
}
case 5:
{
cout << "请输入地址:" << endl;
int address;
cin >> address;
book1->personArray[ret].address = address;
break;
}
}
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//将size置为0即可
void cleanPerson(struct book *book1)
{
book1->size = 0;
cout << "通讯录已清空!" << endl;
system("pause");
system("cls");
}
int main()
{
struct book book1;
book1.size = 0;
int selete = 0;
while (true)
{
showMenu();
cin >> selete;
switch (selete)
{
case 1: //1、添加联系人
addPerson(&book1); //利用地址传递可以修改实参
break;
case 2: //2、显示联系人
showPerson(&book1);
break;
case 3: //3、删除联系人
deletePreson(&book1);
break;
case 4: //4、查找联系人
findPerson(&book1);
break;
case 5: //5、修改联系人
modifyPerson(&book1);
break;
case 6: //6、清空联系人
cleanPerson(&book1);
break;
case 0: //退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
05-29
1846
01-19