#include<iostream>
using namespace std;
#include<string>
#define MAX 200//通讯录最大人数限制
int exist(struct list *List, string name);//查找联系人是否存在函数的声明
void menu()//主界面
{
cout << "\t|通讯录|" << endl;
cout << "**********************" << endl;
cout << "*****1.添加联系人*****" << endl;
cout << "*****2.修改联系人*****" << endl;
cout << "*****3.显示联系人*****" << endl;
cout << "*****4.删除联系人*****" << endl;
cout << "*****5.查找联系人*****" << endl;
cout << "*****6.清空联系人*****" << endl;
cout << "***** 7.退出系统 *****" << endl;
cout << "**********************" << endl;
}
struct member//成员基本信息
{
string m_name;
string m_sex;
int m_age;
string m_phone;
string m_addr;
};
struct list
{
struct member arr[MAX];
int num;//记录通讯录中成员个数
};
void add(struct list *List)//1.添加联系人函数
{
if (List->num == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
}
else
{
cout << "请输入联系人姓名:" << endl;
string name;
cin >> name;
List->arr[List->num].m_name = name;
cout << "请输入联系人性别:" << endl;
string sex;
while (1)
{
cin >> sex;
if (sex == "男" || sex == "女")
{
List->arr[List->num].m_sex = sex;
break;
}
else
cout << "输入错误,请重新输入!" << endl;
}
cout << "请输入联系人年龄:" << endl;
int age;
cin >> age;
List->arr[List->num].m_age = age;
cout << "请输入联系人电话:" << endl;
string phone;
cin >> phone;
List->arr[List->num].m_phone = phone;
cout << "请输入联系人地址:" << endl;
string addr;
cin >> addr;
List->arr[List->num].m_addr = addr;
List->num++;
cout << "添加成功!" << endl;
}
system("pause");
system("cls");//清屏
}
void modify(struct list *List)//2.修改联系人函数
{
cout << "请输入要修改的联系人姓名" << endl;
string name;
cin >> name;
int ret=exist(List, name);
if (ret == -1)
cout << "查无此人!" << endl;
else
{
cout << "输入修改后的姓名:" << endl;
cin >> name;
List->arr[ret].m_name = name;
cout << "输入修改后的性别:" << endl;
string sex;
cin >> sex;
List->arr[ret].m_sex = sex;
cout << "输入修改后的年龄:" << endl;
int age;
cin >> age;
List->arr[ret].m_age = age;
cout << "输入修改后的电话:" << endl;
string phone;
cin >> phone;
List->arr[ret].m_phone = phone;
cout << "输入修改后的地址:" << endl;
string addr;
cin >>addr;
List->arr[ret].m_addr = addr;
cout << "修改成功!" << endl;
}
system("pause");
system("cls");
}
void show(struct list List)//3.显示联系人函数
{
if (List.num == 0)
cout << "没有联系人!" << endl;
else
{
for (int i = 0; i < List.num; i++)
{
cout << i + 1<<"."<<"姓名:" << List.arr[i].m_name << endl;
cout << " 性别:" << List.arr[i].m_sex << endl;
cout << " 年龄:" << List.arr[i].m_age << endl;
cout << " 电话:" << List.arr[i].m_phone << endl;
cout << " 地址:" << List.arr[i].m_addr << endl;
}
cout << "共" << List.num << "个联系人" << endl;
}
system("pause");
system("cls");
}
int exist(struct list *List,string name)//查找联系人是否存在函数
{
for (int i = 0; i < List->num; i++)
{
if (List->arr[i].m_name == name)
{
return i;//找到已存入通讯录的姓名,返回该序号
}
}
return -1;//没找到
}
void del(struct list *List)//4.删除联系人函数
{
cout << "请输入要删除的联系人姓名" << endl;
string name;
cin >> name;
int ret=exist(List, name);
if (ret == -1)
{
cout << "查无此人!" << endl;
}
else
{
for (int i = ret; i < List->num; i++)
{
List->arr[i] = List->arr[i + 1];//使后面的数据逐一覆盖到前一个数据上,使要删除的数据消失
}
List->num--;//通讯录成员数量减一
cout << "已删除!" << endl;
}
system("pause");
system("cls");
}
void find(struct list *List)//5.查找联系人函数
{
cout << "请输入所要查找的联系人姓名:" << endl;
string name;
cin >> name;
int ret=exist(List, name);
if (ret == -1)
{
cout << "查无此人!" << endl;
}
else
{
cout << "姓名:" << List->arr[ret].m_name << endl;
cout << "性别:" << List->arr[ret].m_sex << endl;
cout << "年龄:" << List->arr[ret].m_age << endl;
cout << "电话:" << List->arr[ret].m_phone << endl;
cout << "地址:" << List->arr[ret].m_addr << endl;
}
system("pause");
system("cls");
}
void clear(struct list *List)//6.清空联系人函数
{
List->num = 0;
cout << "通讯录已清空!" << endl;
system("pause");
system("cls");
}
int main()
{
struct list List;
List.num = 0;//初始化成员个数为0
int select = 0;
while (1)
{
menu();
cin >> select;
switch (select)
{
case 1:
add(&List);
break;
case 2:
modify(&List);
break;
case 3:
show(List);
break;
case 4:
del(&List);
break;
case 5:
find(&List);
break;
case 6:
clear(&List);
break;
case 7:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}