|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
using namespace std;
#include <string>
#define MAX 1000
struct Person//定义的成员
{
string NAME;
int Sex;
int Age;
string Phone;
string Address;
};
struct Addressbooks//定义的通讯录
{
struct Person PeasonArray[MAX];
int Size;
};
void addPerson(Addressbooks * abs)//输入用户的函数
{
if (abs->Size == MAX)
{
cout << "通讯录已满,无法添加!"<<endl;
return;
}
else
{
//输入名字
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->PeasonArray[abs->Size].NAME = name;
//输入性别
cout << "请输入性别: " << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int sex = 0;
cin >> sex;
while (true)
{
if (sex == 1 || sex == 2)
{
abs->PeasonArray[abs->Size].Sex = sex;
break;
}
else
{
cout << "输入错误,请重新输入" << endl;
}
}
//输入年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->PeasonArray[abs->Size].Age = age;
//输入电话
cout << "请输入联系电话: " << endl;
string phone;
cin >> phone;
abs->PeasonArray[abs->Size].Phone = phone;
//输入住址
cout << "请输入家庭住址:"<<endl;
string address;
cin >> address;
abs->PeasonArray[abs->Size].Address = address;
//加入成员后数组改变
abs->Size++;
cout << "添加成功" << endl;
system("pause");//按任意继续
system("cls");//清屏
}
}
void showPerson(Addressbooks * abs)
{
if (abs->Size == 0)
{
cout << "没有联系人请添加" << endl;
}
else
{
for (int i = 0; i < abs->Size; i++)
{
cout << "姓名" << abs->PeasonArray[i].NAME << endl;
cout << "性别" << abs->PeasonArray[i].Sex << endl;
cout << "年龄" << abs->PeasonArray[i].Age << endl;
cout << "电话" << abs->PeasonArray[i].Phone << endl;
cout << "住址" << abs->PeasonArray[i].Address << endl;
}
}
system("pause");
system("cls");
}
int isExist(Addressbooks* abs, string name)
{
for (int i = 0; i < abs->Size; i++)
{
if (abs->PeasonArray[i].NAME == name)
{
return i;
}
else
{
return -1;
}
}
}
void deletePerson(Addressbooks* abs)
{
cout << "请输入你要删除的人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->Size; i++)
{
abs->PeasonArray[i] = abs->PeasonArray[i + 1];
}
abs->Size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void findPerson(Addressbooks* abs)
{
cout << "请输入你要查找的人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "姓名" << abs->PeasonArray[ret].NAME << endl;
cout << "性别" << abs->PeasonArray[ret].Sex << endl;
cout << "年龄" << abs->PeasonArray[ret].Age << endl;
cout << "电话" << abs->PeasonArray[ret].Phone << endl;
cout << "住址" << abs->PeasonArray[ret].Address << endl;
}
system("pause");
system("cls");
}
void modityPerson(Addressbooks* abs)
{
cout << "请输入你要修改的人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout<< "查无此人" << endl;
}
else
{
string name;
cout << "请输入你要修改的名字" << endl;
cin >> name;
abs->PeasonArray[ret].NAME = name;
cout << "请输入性别:" << endl;
cout << "1 -- 男" << endl;
cout << "2 -- 女" << endl;
int sex = 0;
cin >> sex;
while (true)
{
if (sex == 1 || sex == 2)
{
abs->PeasonArray[ret].Sex = sex;
}
else
{
cout << "输入错误,请重新输入。" << endl;
}
}
cout << "请输入你要修改的年龄" << endl;
int age = 0;
cin >> age;
abs->PeasonArray[ret].Age = age;
cout << "请输入你要修改的电话号码" << endl;
string phone;
cin >> phone;
abs->PeasonArray[ret].Phone = phone;
cout << "请输入你要修改的住址" <<endl;
string address;
cin >> address;
abs->PeasonArray[ret].Address = address;
}
system("pause");
system("cls");
}
void cleanPerson(Addressbooks* 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()
{
Addressbooks abs;
abs.Size = 0;
int select = 0;
while (true)
{
showmenu();
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:
modityPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout << "欢迎下次使用通讯录" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
} |
|