第一个程序
#include<iostream>
#include<string>
using namespace std;
#define MAX 10
//创建菜单
void menu()
{
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;
}
//创建通讯录与联系人的结构体
struct person
{
string name;
string sex;
int age;
string telphone;
string addr;
};
struct addressbook
{
struct person a[MAX];//尼玛的,整了一个花括号浪费老子好多时间,还不报错!!!!!!!!!!!!!!
int size;
};
//创建添加联系人功能
void Addperson(struct addressbook *ads)
{
if (ads->size == MAX)
{
cout << "不好意思,通讯录已满!" << endl;
system("pause");//否则一闪而过哦,大兄弟!!!!!!!
return;
}
else
{
string name;
cout << "请输入联系人姓名: " << endl;
cin >> name;
ads-> a[ads->size].name = name;
cout << "请输入联系人性别: " << endl;
string sex;
cin >> sex;
ads->a[ads->size].sex = sex;
cout << "请输入联系人年龄: " << endl;
int age;
cin >> age;
ads->a[ads->size].age = age;
cout << "请输入联系人电话号码: " << endl;
string tel;
cin >> tel;
ads->a[ads->size].telphone = tel;
cout << "请输入联系人地址: " << endl;
string add;
cin >> add;
ads->a[ads->size].addr = add;
}
ads->size++;
}
//显示联系人功能
void displayperson(struct addressbook* ads)
{
if (ads->size == 0)
{
cout << "现在通讯录是空的" << endl;
return;
}
else
{
for (int i = 0; i < ads->size; i++)
{
cout << "联系人"<<i+1<<"姓名: "<< ads->a[i].name << " ";
cout << "性别: " << ads->a[i].sex << " ";
cout << "年龄: " << ads->a[i].age << " ";
cout << "电话: " << ads->a[i].telphone << " ";
cout << "地址: " << ads->a[i].addr << endl;
}
system("pause");
system("cls");
}
}
//删除联系人功能
int deleteperson(struct addressbook* ads)
{
int ret=-1;
string name;
cout << "请输入要删除的联系人" << endl;
cin >> name;
for (int i = 0; i < ads->size; i++)
{
if (ads->a[i].name == name)
{
cout << "联系人" << i + 1 << "姓名: " << ads->a[i].name << " ";
cout << "性别: " << ads->a[i].sex << " ";
cout << "年龄: " << ads->a[i].age << " ";
cout << "电话: " << ads->a[i].telphone << " ";
cout << "地址: " << ads->a[i].addr << endl;
ret = i;
return ret;
}
}
if (ret >= 0)
{
for (int i = ret; i < ads->size; i++)
{
ads->a[i] = ads->a[i + 1];
}
ads->size--;
cout << "已成功删除" << endl;
}
else
cout << "查无此人" << endl;
system("pause");
system("cls");
}
//查找联系人功能
void findperson(struct addressbook* ads)
{
int t = 0;//以此判断遍历完通讯录之后是否有要查找的人.
string name;
cout << "请输入联系人姓名: " << " ";
cin >> name;
for (int i = 0; i < ads->size; i++)
{
if (ads->a[i].name == name)
{
cout << "联系人" << i + 1 << "姓名: " << ads->a[i].name << " ";
cout << "性别: " << ads->a[i].sex << " ";
cout << "年龄: " << ads->a[i].age << " ";
cout << "电话: " << ads->a[i].telphone << " ";
cout << "地址: " << ads->a[i].addr << endl;
t++;
}
}
if (t == 0)
cout << "查无此人!" << endl;
system("pause");
system("cls");
}
//修改联系人动能
int modifyperson(struct addressbook* ads)
{
int t = -1;//以此判断遍历完通讯录之后是否有要查找的人.
string name;
cout << "请输入要修改联系人的姓名: " << endl;
cin >> name;
for (int i = 0; i < ads->size; i++)
{
if (ads->a[i].name == name)
{
cout << "联系人" << i + 1 << "姓名: " << ads->a[i].name << " ";
cout << "性别: " << ads->a[i].sex << " ";
cout << "年龄: " << ads->a[i].age << " ";
cout << "电话: " << ads->a[i].telphone << " ";
cout << "地址: " << ads->a[i].addr << endl;
t=i;
string name;
string sex;
int age;
string telphone;
string addr;
cout << "请修改姓名" << endl;
cin >> name;
ads->a[t].name = name;
cout << "请修改性别" << endl;
cin >> sex;
ads->a[t].sex = sex;
cout << "请修改年龄" << endl;
cin >> age;
ads->a[t].age = age;
cout << "请修改电话号码" << endl;
cin >> telphone;
ads->a[t].telphone = telphone;
cout << "请修改地址" << endl;
cin >> addr;
ads->a[t].addr = addr;
cout << "修改完毕" << endl;
system("pause");
system("cls");
return t;
}
}
if (t == -1)
cout << "查无此人!" << endl;
system("pause");
system("cls");
}
//清空联系人功能
void clearperson(struct addressbook* ads)
{
int i;
cout << "是否要清空所有联系人?删除后就不能回复了哦!" << endl;
cout << "确认清空请输入1,考虑考虑请输入2:" << endl;
cin >> i;
if (i = 1)
ads->size = 0;
else if (i = 2)
cout << "感谢不杀之恩" << endl;
else
system("pause");
system("cls");
}
int main()
{
addressbook ads;
ads.size = 0;
while(1)
{
menu();
int option = 0;
cin >> option;
switch (option)
{
case 1:
Addperson(&ads);
system("cls");
break;
case 2:
displayperson(&ads);
break;
case 3:
deleteperson(&ads);
break;
case 4:
findperson(&ads);
break;
case 5:
modifyperson(&ads);
break;
case 6:
clearperson(&ads);
break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
break;
}
}
return 0;
}