功能如下:
添加联系人:信息包括姓名、性别、年龄、联系电话、家庭住址等。
显示联系人:显示通讯录中所有联系人信息。
删除联系人:按照姓名进行删除指定联系人。
根据用户输入的联系人判断该通讯录中是否有此人;
查找到进行删除,并提示删除成功;
查不到提示查无此人。
查找联系人:按照姓名查看指定联系人信息。
修改联系人:按照姓名重新修改指定联系人。
清空联系人:清空通讯录中所有信息。
退出通讯录:退出当前使用的通讯录。
#include <iostream>
#include <string>
#define MAX 888
using namespace std;
//联系人结构体
struct Person
{
string Name;
int Sex;
int Age;
string Phone;
string Addr;
};
//设计通讯录结构体
struct Address_book
{
//通讯录中保存的联系人数组
struct Person personArray[MAX];
//通讯录中当前记录联系人个数
int Size;
};
//菜单界面
void showMenu()
{
cout << "**************************" << endl;
cout << "***** 1.Add contacts *****" << endl;
cout << "***** 2.Show contacts *****" << endl;
cout << "***** 3.Delete contacts *****" << endl;
cout << "***** 4.Find contacts *****" << endl;
cout << "***** 5.Modifying contact *****" << endl;
cout << "***** 6.Clear contacts *****" << endl;
cout << "***** 0.Exit address book *****" << endl;
cout << "**************************" << endl;
}
//1.添加联系人
void addPerson(Address_book* abs)
{
//判断通讯录是否已满,如果满了就不再添加
if (abs->Size == MAX)
{
cout << "The address book is full and cannot be added!" << endl;
return;
}
else
{
//添加具体联系人
//姓名
string name;
cout << "Please enter your name:" << endl;
cin >> name;
abs->personArray[abs->Size].Name = name;
//性别
cout << "Please enter gender:" << endl;
cout << "1-----male" << endl;
cout << "2-----female" << endl;
int sex = 0;
while(true)
{
//如果输入的是1或者2可以退出循环,因为输入的是正确值
//如果输入有误,重新输入
cin >> sex;
if(sex == 1 || sex == 2)
{
abs->personArray[abs->Size].Sex = sex;
break;
}
cout << "Please enter it again" << endl;
}
//年龄
cout << "Please enter age:" << endl;
int age = 0;
while(true)
{
cin >> age;
if(age > 0 && age < 120)
{
abs->personArray[abs->Size].Age = age;
break;
}
cout << "Please enter it again" << endl;
}
//电话
cout << "Please enter the phone number:" << endl;
string phone;
cin >> phone;
abs->personArray[abs->Size].Phone = phone;
//请输入家庭住址
cout << "Please enter your home address:" << endl;
string address;
cin >> address;
abs->personArray[abs->Size].Addr = address;
//更新通讯录人数
++abs->Size;
cout << "Add successfully" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
}
//2.显示联系人
void showPerson(Address_book* abs)
{
//判断通讯录中人数是否为0,如果为0,提示记录为空
//如果不为0,显示联系人信息
if (abs->Size == 0)
{
//当前记录为空
cout << "The current record is empty" << endl;
}
else
{
for (int i = 0; i < abs->Size; i++)
{
cout << "name:" << abs->personArray[i].Name << "\t";
cout << "gender:" << (abs->personArray[i].Sex == 1 ? "male" : "female") << "\t";
cout << "age:" << abs->personArray[i].Age << "\t";
cout << "telephone:" << abs->personArray[i].Phone << "\t";
cout << "address:" << abs->personArray[i].Addr << endl;
}
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//检测联系人是否存在,如果存在,返回联系人所在数组中的具体位置,不存在返回-1
int isExist(Address_book* abs, string name)
{
for (int i = 0; i < abs->Size; i++)
{
//找到用户输入的姓名了
if (abs->personArray[i].Name == name)
{
return i;//找到了,返回这个人在数组中的下标编号
}
}
return -1;//如果遍历结束都没有找到,返回-1
}
//3.删除指定联系人
void deletePerson(Address_book* abs)
{
cout << "Please enter the contact you want to delete:" << endl;
string name;
cin >> name;
//ret==-1 未查到
//ret !== -1 查到了
int ret = isExist(abs, name);
if(ret != -1)
{
//查到此人,要进行删除操作
for (int i = ret; i < abs->Size; i++)
{
//数据迁移
abs->personArray[i] = abs->personArray[i + 1];
}
abs->Size--;//更新通讯录中的人员数
cout << "deleted successfully" << endl;
}
else
{
cout << "No one has been found" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//4.查找指定联系人信息
void findPerson(Address_book* abs)
{
cout << "Please enter your search contacts:" << endl;
string name;
cin >> name;
//判断指定联系人是否在通讯录中
int ret = isExist(abs, name);
if(ret != -1)
{
cout << "name:" << abs->personArray[ret].Name << "\t";
cout << "gender:" << abs->personArray[ret].Sex << "\t";
cout << "age:" << abs->personArray[ret].Age << "\t";
cout << "telephone:" << abs->personArray[ret].Phone << "\t";
cout << "address:" << abs->personArray[ret].Addr << endl;
}
else
{
cout << "No one has been found" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//5.修改联系人
void modifyPerson(Address_book* abs)
{
cout << "Please enter the contact you want to modify:" << endl;
string name;
cin >> name;
//判断指定联系人是否在通讯录中
int ret = isExist(abs, name);
if(ret != -1)
{
//姓名
string name;
cout << "Please enter your name:" << endl;
cin >> name;
abs->personArray[ret].Name = name;
//性别
cout << "Please enter gender:" << endl;
cout << "1-----male" << endl;
cout << "2-----female" << endl;
int sex = 0;
while(true)
{
cin >> sex;
if(sex == 1 || sex == 2)
{
//输入正确 退出循环输入
abs->personArray[abs->Size].Sex = sex;
break;
}
cout << "Please enter it again" << endl;
}
//年龄
cout << "Please enter age:" << endl;
int age = 0;
while(true)
{
cin >> age;
if(age > 0 && age < 120)
{
abs->personArray[ret].Age = age;
break;
}
cout << "Please enter it again" << endl;
}
//电话
cout << "Please enter the phone number:" << endl;
string phone;
cin >> phone;
abs->personArray[ret].Phone = phone;
//请输入家庭住址
cout << "Please enter your home address:" << endl;
string address;
cin >> address;
abs->personArray[ret].Addr = address;
cout << "modified successfully" << endl;
}
else
{
cout << "No one has been found" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//6.清空联系人
void clearPerson(Address_book* abs)
{
abs->Size = 0;
cout << "Address book cleared" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
int main()
{
//创建通讯录结构体变量
Address_book abs;
//初始化通讯录中当前人员个数
abs.Size = 0;
int select = 0;//创建用户输入的变量
while(true)
{
//菜单调用
showMenu();
cin >> select;
switch(select)
{
case 1://1.添加联系人
addPerson(&abs);
break;
case 2://2.显示联系人
showPerson(&abs);
break;
case 3://3.删除联系人
deletePerson(&abs);
break;
case 4://4.查找联系人
findPerson(&abs);
break;
case 5:// 5.修改联系人
modifyPerson(&abs);
break;
case 6://6.清空联系人
clearPerson(&abs);
break;
case 0://0.退出通讯录
cout << "Welcome to use next time" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}