#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
//定义通信结构体
struct Person
{
string Name; //姓名
int Sex; //性别:1男 2女
int Age; //年龄
string Phone; //电话
string Address; //住址
};
struct Addressbook
{
int size; //记录通信条数
struct Person p[MAX]; // 来储存Person的结构体
};
void add_person(struct Addressbook * abs)
{
//姓名
string name;
cout << "输入你要储存的姓名" << endl;
cin >> name;
abs->p[abs->size].Name = name;
//性别
int sex;
cout << "输入该人的性别" << endl;
cin >> sex;
abs->p[abs->size].Sex = sex;
//年龄
int age;
cout << "输入该人的年龄" << endl;
cin >> age;
abs->p[abs->size].Age = age;
//电话
string phone;
cout << "输入该人的电话" << endl;
cin >> phone;
abs->p[abs->size].Phone = phone;
//地址
string address;
cout << "输入该人的地址" << endl;
cin >> address;
abs->p[abs->size].Address = address;
// 计数器自增
abs->size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
void display_person(struct Addressbook * abs)
{
if (abs->size == 0)
{
cout << "结构体数据为空" << endl;
}
else
{
for (int i = 0; i < abs->size; i++)
{
cout << "姓名为:" << abs->p[i].Name << " 性别:" << abs->p[i].Sex << " 年龄:" << abs->p[i].Age << " 电话:" << abs->p[i].Phone << " 地址为:" << abs->p[i].Address << endl;
}
}
system("pause");
system("cls");
}
int isExist(struct Addressbook * abs,string name) //封装一个函数 用来判断是否存在 因为多个函数都需要用到
{
for(int i=0; i < abs->size; i++)
{
if(abs->p->Name == name)
{
return i;
}
}
return -1;
}
void del_person(struct Addressbook * abs, string name)
{
int res = isExist(abs, name);
if(res != -1)
{
for(int i=res; i<abs->size; i++)
{
abs->p[i] = abs->p[i+1];
}
abs->size--;
cout << "删除成功!" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void find_person(struct Addressbook * abs, string name)
{
int res = isExist(abs, name);
if(res != -1)
{
for(int i=0; i<abs->size; i++)
{
if(abs->p[i].Name == name)
{
cout <<"姓名为:"<< abs->p[i].Name << " 性别:" << abs->p[i].Sex << " 年龄:" << abs->p[i].Age << " 电话:" << abs->p[i].Phone << " 地址为:" << abs->p[i].Address << endl;
}
}
}
else
{
cout << "查无此人" << endl;
}
}
void edit_person(struct Addressbook * abs, string name)
{
int res = isExist(abs, name);
string re_Name; //姓名
int re_Sex; //性别:1男 2女
int re_Age; //年龄
string re_Phone; //电话
string re_Address; //住址
if(res != -1)
{
for(int i=0; i<abs->size; i++)
{
cout <<"重新修改的姓名为:"<< endl;
cin >> re_Name;
abs->p[i].Name = re_Name;
cout <<"重新修改的性别为:"<< endl;
cin >> re_Sex;
abs->p[i].Sex = re_Sex;
cout <<"重新修改的年龄为:"<< endl;
cin >> re_Age;
abs->p[i].Age = re_Age;
cout <<"重新修改的电话为:"<< endl;
cin >> re_Phone;
abs->p[i].Phone = re_Phone;
cout <<"重新修改的住址为:"<< endl;
cin >> re_Address;
abs->p[i].Address = re_Address;
cout << "修改成功" << endl;
system("pause");
system("cls");
}
}
else
{
cout << "查无此人" << endl;
}
}
void clear_person(struct Addressbook * abs){
abs->size = 0;
cout << "清空完成" << endl;
system("pause");
system("cls");
}
//入口
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;
}
int main()
{
struct Addressbook abs; //定义一个Addressbook的结构体
abs.size = 0; //初始化num的值
int choose = 0; //初始化choose的值
while (true)
{
showMenu();
cout << "输入你想进行操作的序号:";
cin >> choose;
switch (choose)
{
case 1:
//添加联系人
add_person(&abs); //这里为什么要传地址呢,因为我们输入的数据都要保存,而不是函数中允许完就消失
//cout << abs.p[abs.size-1].Name; // 这个用来调试下,输出上一个size的记录数
break;
case 2:
//显示联系人
display_person(&abs); //这里可以传地址也可以传值,因为只需要输出就够了,但是还需要知道当前的结构体中的数量len,用于循环次数
break;
case 3:
//删除联系人
{
string name;
cin >> name;
del_person(&abs,name);
}
break;
case 4:
{
string name;
cin >> name;
find_person(&abs, name);
}
break;
case 5:
//修改联系人
{
string name;
cin >> name;
edit_person(&abs, name);
}
break;
case 6:
//清空联系人
{
int flag = 0;
cout << "是否确认要清空联系人表?(输入1为true 2为false)" << endl;
cin >> flag;
if(flag == 1){
clear_person(&abs);
}
}
break;
case 0:
//退出通讯录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
}
}
}