张三负了李四,王五无情躺枪诶
咱就是说,要删,当初为何要加呢
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
struct person {
string m_name;
int m_sex;
int m_age;
string m_phone;
string m_addr;
};
struct addressbooks {
struct person personarray[MAX];
int m_size; //当前记录联系人个数
};
//1、菜单界面
void showmenu(void)
{
cout << " You're welcome " << endl;
cout << "************Mr.Qi************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "*****************************" << endl;
}
//2、添加联系人
void addperson(struct addressbooks* abs)
{
//先判断是否满了
if (abs->m_size == MAX)
{
cout << "通讯录已满,thankyou!" << endl;
return ;
}
else
{
//姓名
cout << "请输入姓名:" << endl;
string name;
cin >> name;
abs->personarray[abs->m_size].m_name = name;
//性别,1--男,2--女
cout << "请输入性别(1--男,2--女):" << endl;
while (1)
{
int sex;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[abs->m_size].m_sex = sex;
break;
}
else {
cout << "please input again!" << endl;
}
}
//年龄
cout << "请输入年龄:" << endl;
int age;
cin >> age;
abs->personarray[abs->m_size].m_age = age;
//电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
abs->personarray[abs->m_size].m_phone = phone;
//地址
cout << "请输入地址:" << endl;
string ad;
cin >> ad;
abs->personarray[abs->m_size].m_addr = ad;
//更新通讯录
(abs->m_size)++;
cout << "添加成功哦~" << endl;
system("pause");//请按任意键继续
system("cls");//清屏函数,注意学
}
}
//3、显示联系人
void showperson(struct addressbooks* abs)
{
if (abs->m_size == 0)
{
cout << "Mr.Qi no one!" << endl;
}
else
{
cout << "name\t\tsex\t\tage\t\tphone\t\taddress" << endl;
for (int i = 0; i < abs->m_size; i++)
{
cout << abs->personarray[i].m_name << "\t\t";
cout << ((abs->personarray[i].m_sex==1)?"男":"女") << "\t\t";
cout << abs->personarray[i].m_age << "\t\t";
cout << abs->personarray[i].m_phone << "\t";
cout << abs->personarray[i].m_addr << endl;
}
}
system("pause");
system("cls");
}
//4、检查联系人是否存在
int check(struct addressbooks* abs,string arr)
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->personarray[i].m_name == arr)
{ //注意哦,string定义的字符串,在某些使用上如+-
//比C语言方便的太太,C中比较两个字符串只能用strcmp函数
return i;
}
}
return -1;
}
//5、删除联系人
void deleteperson(struct addressbooks* abs)
{
cout << "为保留温情,留一份情谊,why删?" << endl;
cout << "您若无情,那就删吧!please input who!!!" << endl;
string name;
cin >> name;
int ret = check(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_size; i++)
{
abs->personarray[i] = abs->personarray[i + 1];
}
(abs->m_size)--;
cout << "congratulations!烦恼无了" << endl;
}
else {
cout << "看来,您还没有他(她)电话,咋删呢!" << endl;
}
system("pause");
system("cls");
}
//6、查找联系人
void findperson(struct addressbooks* abs)
{
cout << "请输入您要查找的联系人,看来好久没联系了吧!" << endl;
string name;
cin >> name;
int ret = check(abs, name);
if (ret != -1)
{
cout << "您要查的联系人的信息:";
cout << abs->personarray[ret].m_name << "\t";
cout << ((abs->personarray[ret].m_sex == 1) ? "男" : "女") << "\t";
cout << abs->personarray[ret].m_age << "\t";
cout << abs->personarray[ret].m_phone << "\t";
cout << abs->personarray[ret].m_addr << endl;
}
else {
cout << "很遗憾,您未加该联系人" << endl;
}
system("pause");
system("cls");
}
//7、修改联系人
void modifyperson(struct addressbooks* abs)
{
cout << "请输入要修改的联系人" << endl;
string name;
cin >> name;
int ret = check(abs, name);
if (ret != -1)
{
cout << "请输入名字:" << endl;
string mname;
cin >> mname;
abs->personarray[ret].m_name = mname;
cout << "请输入性别(1--男,2--女):" << endl;
while (1)
{
int sex;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[ret].m_sex = sex;
break;
}
else {
cout << "please input again!" << endl;
}
}
cout << "请输入年龄:" << endl;
int age;
cin >> age;
abs->personarray[ret].m_age = age;
//电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
abs->personarray[ret].m_phone = phone;
//地址
cout << "请输入地址:" << endl;
string ad;
cin >> ad;
abs->personarray[ret].m_addr = ad;
cout << "修改成功哦~" << endl;
}
else {
cout << "很遗憾,您未加该联系人" << endl;
}
system("pause");
system("cls");
}
//8、清空联系人
void cleanperson(struct addressbooks* abs)
{
abs->m_size = 0;
cout << "您的通讯录已清" << endl;
system("pause");
system("cls");
}
int main()
{
struct addressbooks abs; //创建通讯录abs
abs.m_size = 0;
while (1)
{
showmenu();
int select;
cin >> select;
switch (select)
{
case 1: //添加联系人
addperson(&abs);
break;
case 2: //显示联系人
showperson(&abs);
break;
case 3: //删除联系人
deleteperson(&abs);
break;
case 4: //查找联系人
findperson(&abs);
break;
case 5: //修改联系人
modifyperson(&abs);
break;
case 6: //清空通讯录
cleanperson(&abs);
break;
case 0:
cout << "欢迎下次使用Mr.Qi!" << endl;
system("pause");//按任意键结束
return 0;//提前结束main函数,即退出程序
break;
default:
break;
}
}
system("pause");//按任意键继续
return 0;
}//咱就是说,要删,当初为何要加呢