#include<iostream>
#include<string>
using namespace std;
const int MAX = 1001;
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;
}
struct person
{
string name;//姓名
int sex;//0:男 1:女
int age;//年龄
string phone;//电话号码
string addr;//地址
};
//通讯录结构体
struct menu
{
person array[MAX];
int size=0;
void addperson()
{
if (size == MAX)cout << "MAX" << endl;
else
{
cout << "请输入姓名" << endl;
string name;
cin >> name;
array[size].name = name;
cout << "输入名字成功" << endl;
int sex;
while (1) {
cout << "请输入性别" << endl;
cout << "1-----男性" << endl;
cout << "0-----女性 " << endl;
cin >> sex;
if (sex == 1 || sex == 0) { array[size].sex = sex; break; }
else cout << "输入错误请重试" << endl;
}
//age
cout << "请输入年龄" << endl;
int age;
cin >> age;
array[size].age = age;
cout << "请输入电话号码" << endl;
string pnumber;
cin >> pnumber;
array[size].phone = pnumber;
cout << "请输入地址" << endl;
string addr;
cin >> addr;
array[size].addr = addr;
size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
void showperson()
{
if (size < 1) { cout << "暂无联系人" << endl; return; }
else
{
for (int i = 0; i < size; i++)
{
cout << "姓名" << array[i].name << "\t";
cout << "性别" <<(array[i].sex==1?"男":"女")<< "\t";
cout << "年龄" << array[i].age << "\t";
cout << "电话" << array[i].phone << "\t";
cout << "住址" << array[i].addr<< endl;
}
}
system("pause");
system("cls");
}
int findname(string name)
{
for (int i = 0; i < size; i++)
{
if (array[i].name == name)return i;
}
return -1;
}
void dele(string name1)
{
int ret = findname(name1);
if (ret == -1) { cout << "查无此人"; return; }
else
{
for (int i = ret; i < size; i++)
array[i] = array[i + 1];
size--;
cout << "删除成功" << endl;
}
}
void changep(string name)
{
int ret = findname(name);
if (ret == -1) { cout << "查无此人" << endl; return; }
else
{
cout << "请输入姓名" << endl;
string name;
cin >> name;
array[ret].name = name;
cout << "输入名字成功" << endl;
int sex;
while (1) {
cout << "请输入性别" << endl;
cout << "1-----男性" << endl;
cout << "0-----女性 " << endl;
cin >> sex;
if (sex == 1 || sex == 0) { array[ret].sex = sex; break; }
else cout << "输入错误请重试" << endl;
}
//age
cout << "请输入年龄" << endl;
int age;
cin >> age;
array[ret].age = age;
cout << "请输入电话号码" << endl;
string pnumber;
cin >> pnumber;
array[ret].phone = pnumber;
cout << "请输入地址" << endl;
string addr;
cin >> addr;
array[ret].addr = addr;
cout << "修改成功" << endl;
system("pause");
system("cls");
}
}
};
int main()
{
menu abs;
abs.size = 0;
int select = 0;
while (true)
{
showmenu();
cin >> select;
switch (select)
{
case 1: //添加联系人
abs.addperson();
break;
case 2: //显示联系人
abs.showperson();
break;
case 3: //删除联系人
{
cout << "请输入联系人名字" << endl;
string name1;
cin >> name1;
abs.dele(name1);
}
break;
case 4: //查找联系人
{
cout << "请输入联系人姓名" << endl;
string name;
cin >> name;
if (abs.findname(name) != -1)cout << abs.findname(name) << endl;
}
break;
case 5: //修改联系人
{
cout << "请输入修改前的人名" << endl;
string name;
cin >> name;
abs.changep(name);
system("pause");
system("cls");
}
break;
case 6: //清空联系人
{
abs.size = 0;
cout << "清除成功" << endl;
system("pause");
system("cls");
}
break;
case 0: //退出通讯录
cout << "欢迎继续使用" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}
c++简单通讯录实现
最新推荐文章于 2024-05-21 09:15:43 发布