#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#define Max 1000
using namespace std;
struct person
{
string name = {};
int sex = 0;
int age = 0;
string phone = {};
string address = {};
};
struct addressbook
{
struct person arr[Max] = {};
int size = 0;
};
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 << endl;
}
void addperson(addressbook *abs)
{
if (abs->size == 1000)
{
cout << "Contacts are full!" << endl;
return;
}
else
{
cout << "input name: "; cin >> abs->arr[abs->size].name;
cout << endl << "input sex: [1 for man | 0 for women] ";
int sex1 = 0;
while(true)
{
cin >> sex1;
if (sex1 == 1 || sex1 == 0)
{
abs->arr[abs->size].sex = sex1;
break;
}
cout << "wrong! input again : " << endl;
}
cout << endl << "input age: "; cin >> abs->arr[abs->size].age;
cout << endl << "input phone: "; cin >> abs->arr[abs->size].phone;
cout << endl << "input address: "; cin >> abs->arr[abs->size].address;
cout << endl << "addperson succes ! " << endl;
abs->size++;
system("pause");
system("cls");
}
}
void showperson(const struct addressbook* abs)
{
if (abs->size == 0) { cout << "empty !" << endl; }
for (int i = 0; i < abs->size; i++)
{
cout << "[" << i + 1 << "]"
<< " Name: " << abs->arr[i].name
<< "\t\tAge: " << (abs->arr[i].sex == 1 ? "男" : "女")
<< "\t\tAge: " << abs->arr[i].age
<< "\t\tPhone: " << abs->arr[i].phone
<< "\t\tAddress:" << abs->arr[i].address << endl;
}
system("pause");
system("cls");
}
int exist(addressbook* abs, string name)
{
for (int i = 0; i < abs->size; i++)
{
if (abs->arr[i].name == name)
{
return i;
}
}
return -1;
}
void deleteperson(addressbook* abs)
{
cout << "input the deleted name: " << endl;
string name;
cin >> name;
int ret = exist(abs, name);
if (ret == -1)
{
cout << "no person info: " << endl;
}
else
{
for (int i = ret; i < abs->size; i++)
{
abs->arr[i] = abs->arr[i + 1];
}
abs->size--;
cout << "delete pewrson succes! " << endl;
}
system("pause");
system("cls");
}
void findperson(addressbook* abs)
{
cout << "input the name you want to find : " << endl;
string name;
cin >> name;
int ret = exist(abs, name);
if (ret == -1)
{
cout << "no person info: " << endl;
}
else
{
cout << endl << "Person info : " << endl << "[" << ret + 1 << "]"
<< " Name: " << abs->arr[ret].name
<< "\tAge: " << (abs->arr[ret].age == 1 ? "男" : "女")
<< "\tPhone: " << abs->arr[ret].phone
<< "\tAddress:" << abs->arr[ret].address << endl << endl;
}
system("pause");
system("cls");
}
void editperson(addressbook* abs)
{
cout << "input the name you want to edit : " << endl;
string name;
cin >> name;
int ret = exist(abs, name);
if (ret == -1)
{
cout << "no person info: " << endl;
}
else
{
cout << "input name: "; cin >> abs->arr[ret].name;
cout << endl << "input sex: [1 for man | 0 for women] ";
int sex1 = 0;
while (true)
{
cin >> sex1;
if (sex1 == 0 || sex1 == 1)
{
abs->arr[ret].sex = sex1;
break;
}
cout << "wrong! input again : " << endl;
}
cout << endl << "input age: "; cin >> abs->arr[ret].age;
cout << endl << "input phone: "; cin >> abs->arr[ret].phone;
cout << endl << "input address: "; cin >> abs->arr[ret].address;
cout << endl << "editperson succes ! " << endl;
}
system("pause");
system("cls");
}
void clearperson(addressbook* abs)
{
abs->size = 0;
cout << "clear person succse! " << endl;
system("pause");
system("cls");
}
int main()
{
addressbook abs;
abs.size = 0;
int ch = 1;
while (ch != 0)
{
system("color 3F");
showmenu();
cin >> ch;
switch (ch)
{
case 1:addperson(&abs); break;
case 2:showperson(&abs); break;
case 3:deleteperson(&abs); break;
case 4:findperson(&abs); break;
case 5:editperson(&abs); break;
case 6:clearperson(&abs); break;
case 0:
cout << "退出 .... " << endl;
system("pause");
break;
default:break;
}
}
return 0;
}
通讯录管理系统
于 2022-03-29 15:42:11 首次发布