最多存储1000人
#include<iostream>
using namespace std;
//添加联系人(姓名,性别,年龄)
//显示联系人
//删除联系人:按照姓名删
//查找联系人:按照名字查
//修改联系人:按照名字修改
//清空联系人:清空全部
//退出通讯录:退出
struct people {
string name;
string sex;
int age;
};
void add(people arr[],int i) {
cout<<"请输入姓名:" << endl;
cin >> arr[i].name;
cout << "请输入性别:" << endl;
cin >> arr[i].sex;
cout << "请输入年龄:" << endl;
cin >> arr[i].age;
}
void show(people arr[], int i) {
for (int j = 0; j < i; j++) {
cout<<"姓名:" << arr[j].name << "\t性别:" << arr[j].sex << "\t年龄:" << arr[j].age << endl;
}
}
int flag=-1;
void delet(people arr[], int * i) {
cout<<"输入删除人的姓名:" << endl;
string name;
cin >> name;
for (int j = 0; j < *i; j++) {
if (name == arr[j].name) {
flag = 1;
for (int m = j; m < *i-1; m++) {
arr[m] = arr[m + 1];
}
*i=*i-1;
cout << "删除成功!!!" << endl;
break;
}
}
if (flag == -1) {
cout << "查无此人!" << endl;
}
flag = -1;
}
void find(people arr[], int i) {
cout<<"输入要查找的姓名:" << endl;
string tmp;
cin >> tmp;
for (int j = 0; j < i; j++) {
if (tmp == arr[j].name) {
cout << "姓名:" << arr[j].name << "\t性别:" << arr[j].sex << "\t年龄:" << arr[j].age << endl;
cout << "查找成功!!!" << endl;
flag = 1;
break;
}
}
if (flag == -1) {
cout << "查无此人!" << endl;
}
flag = -1;
}
void update(people arr[], int i) {
cout << "输入要修改的人的姓名:" << endl;
string tmp;
cin >> tmp;
for (int j = 0; j < i; j++) {
if (tmp == arr[j].name) {
cout << "姓名:" << arr[j].name << "\t性别:" << arr[j].sex << "\t年龄:" << arr[j].age << endl;
cout<< "姓名:" <<endl;
string name;
cin >> name;
arr[j].name = name;
cout << "性别:" << endl;
string sex;
cin >> sex;
arr[j].sex = sex;
cout << "年龄:" << endl;
int age;
cin >> age;
arr[j].age = age;
cout<<"修改成功!" << endl;
cout << "姓名:" << arr[j].name << "\t性别:" << arr[j].sex << "\t年龄:" << arr[j].age << endl;
flag = 1;
break;
}
}
if (flag == -1) {
cout << "查无此人!" << endl;
}
flag = -1;
}
void trun(people arr[], int * i) {
*i = 0;
cout<<"清空成功" << endl;
}
int main() {
people arr[1000];//最多存储1000人
int i=0;
while (true) {
cout << "1《添加联系人》" << endl;
cout << "2《显示联系人》" << endl;
cout << "3《删除联系人》" << endl;
cout << "4《查找联系人》" << endl;
cout << "5《修改联系人》" << endl;
cout << "6《清空联系人》" << endl;
cout << "7《退出通讯录》" << endl;
cout << "您要做的操作是:" << endl;
int choise;
cin>>choise;
switch (choise)
{
case 1:
cout << "1《添加联系人》" << endl;
add(arr,i);
cout << "添加成功!!!" << endl;
i++;
system("pause");//按任意键继续
system("cls");//清屏
break;
case 2:
cout << "2《显示联系人》" << endl;
show(arr,i);
cout << "显示成功!!!" << endl;
system("pause");
system("cls");
break;
case 3:
cout << "3《删除联系人》" << endl;
delet(arr, &i);
system("pause");
system("cls");
break;
case 4:
cout << "4《查找联系人》" << endl;
find(arr, i);
system("pause");
system("cls");
break;
case 5:
cout << "5《修改联系人》" << endl;
update(arr, i);
system("pause");
system("cls");
break;
case 6:
cout << "6《清空联系人》" << endl;
trun(arr, &i);
system("pause");
system("cls");
break;
case 7:
cout<< "7《退出通讯录》" << endl;
cout<<"退出成功!!!" << endl;
return false;
default:
cout << "输入错误!!!" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
return 0;
}