#include <iostream>
#include <string>
#define MAX 500
using namespace std;
struct person {
string name;
int age;
};
struct contact {
person persons[MAX];
int cur_index;
};
void ShowMenu(void)
{
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 AddPeople(contact* p) {
string name;
cout << "请输入姓名:" << endl;
cin >> name;
int age;
cout << "请输入年龄:" << endl;
cin >> age;
p->persons[p->cur_index].name = name;
p->persons[p->cur_index].age = age;
p->cur_index++;
cout << "信息添加完成!!!" << endl;
};
void ShowPeople(contact* p) {
if (p->cur_index == 0) {
cout << " 通信录没有人!! 请先添加人员信息!!!" << endl;
}
else {
for (int i = 0; i < p->cur_index; i++)
{
cout << "name : " << p->persons[i].name << " age: " << p->persons[i].age << endl;
}
}
};
void ExitAddr(void) {
cout << "欢迎下次继续使用!!" << endl;
};
void ClearAddr(contact *p) {
p->cur_index = 0;
cout << "您已经把通讯录清空!!!" << endl;
}
int IsExit(contact* p, string name) {
for (int i = 0; i < p->cur_index; i++) {
if (name == p->persons[i].name) {
return i;
}
}
return -1;
}
void FindPeople(contact* p) {
string name;
cout << "请输入想要查找的姓名:" << endl;
cin >> name;
int ret = IsExit(p, name);
if (ret == -1) {
cout << "此用户不存在,请查证后再查找!!" << endl;
}
else {
cout << "name :" << p->persons[ret].name << " age :" << p->persons[ret].age << endl;
cout << "查找成功!!" << endl;
}
}
void ModifyPeople(contact* p) {
string name;
cout << "请输入想要修改的姓名:" << endl;
cin >> name;
int ret = IsExit(p, name);
if (ret == -1) {
cout << "此用户不存在,请查证后再修改!!" << endl;
}
else {
string _name;
cout << "请重新输入姓名:" << endl;
cin >> _name;
int _age;
cout << "请重新输入年龄:" << endl;
cin >> _age;
p->persons[ret].name = _name;
p->persons[ret].age = _age;
cout << "信息修改成功!!!" << endl;
}
}
void DeletePeople(contact* p) {
string name;
cout << "请输入想要删除的姓名:" << endl;
cin >> name;
int ret = IsExit(p, name);
if (ret == -1) {
cout << "此用户不存在,请查证后再删除!!" << endl;
}
else {
for (int i = ret; i < p->cur_index; i++) {
p->persons[i].name = p->persons[i + 1].name;
p->persons[i].age = p->persons[i + 1].age;
}
p->cur_index -= 1;
cout << "删除信息成功!!" << endl;
}
}
int main()
{
struct contact con;
con.cur_index = 0;
while (true)
{
ShowMenu();
int num = -1;
cout << "请输入你想要的功能:" << endl;
cin >> num;
switch (num)
{
case 0: //退出通讯录
ExitAddr();
return 0;
case 1: // 添加联系人
AddPeople(&con);
break;
case 2: // 显示联系人
ShowPeople(&con);
break;
case 3: // 删除联系人
DeletePeople(&con);
break;
case 4: // 查找联系人
FindPeople(&con);
break;
case 5: // 修改联系人
ModifyPeople(&con);
break;
case 6: // 清空联系人
ClearAddr(&con);
break;
default:
break;
}
system("pause");
system("cls");
}
}