马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 蜜柑面包 于 2022-9-24 19:11 编辑 #include<iostream>
#include<string>
#define zuidarenshu 1000
using namespace std;
struct lianxiren
{
string name;
int xingbie;//1男2女
int age;
string phoneNumber;
string dizhi;
};
struct tongxunlu
{
struct lianxiren renyuan[zuidarenshu];
int renshu;
};
void tianjia(tongxunlu* txl)
{
if (txl->renshu == zuidarenshu)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
txl->renyuan[txl->renshu].name = name;
cout << "请输入性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int xingbie = 0;
while (true)
{
cin >> xingbie;
if (xingbie == 1 || xingbie == 2)
{
txl->renyuan[txl->renshu].xingbie = xingbie;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0)
{
txl->renyuan[txl->renshu].age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入联系电话:" << endl;
string pn;
cin >> pn;
txl->renyuan[txl->renshu].phoneNumber = pn;
cout << "请输入家庭住址:" << endl;
string zhuzhi;
cin >> zhuzhi;
txl->renyuan[txl->renshu].dizhi = zhuzhi;
txl->renshu++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
void xianshi(tongxunlu* txl)
{
if (txl->renshu == 0)
{
cout << "当前的记录为空" << endl;
}
else
{
for (int i = 0; i < txl->renshu; i++)
{
cout << "姓名: " << txl->renyuan[i].name << '\t';
cout << "性别: " << (txl->renyuan[i].xingbie == 1 ? "男" : "女") << '\t';
cout << "年龄: " << txl->renyuan[i].age << '\t';
cout << "电话: " << txl->renyuan[i].phoneNumber << '\t';
cout << "住址: " << txl->renyuan[i].dizhi << endl;
}
}
system("pause");
system("cls");
}
int jiance(tongxunlu* txl, string name)
{
for (int i = 0; i < txl->renshu; i++)
{
if (txl->renyuan[i].name == name)
{
return i;
}
}
return -1;
}
void shanchu(tongxunlu* txl)
{
cout << "请输入您要删除的联系人:" << endl;
string name;
cin >> name;
int jieguo = jiance(txl, name);
if (jieguo != -1)
{
for (int i = jieguo; i < txl->renshu; i++)
{
txl->renyuan[i] = txl->renyuan[i + 1];
}
txl->renshu--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void chazhao(tongxunlu* txl)
{
cout << "请输入您要查找的联系人:" << endl;
string name;
cin >> name;
int jieguo = jiance(txl, name);
if (jieguo != -1)
{
cout << "姓名: " << txl->renyuan[jieguo].name << '\t';
cout << "性别: " << (txl->renyuan[jieguo].xingbie == 1 ? "男" : "女") << '\t';
cout << "年龄: " << txl->renyuan[jieguo].age << '\t';
cout << "号码: " << txl->renyuan[jieguo].phoneNumber << '\t';
cout << "住址: " << txl->renyuan[jieguo].dizhi << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void xiugai(tongxunlu* txl)
{
cout << "请输入您要修改的联系人:" << endl;
string name;
cin >> name;
int jieguo = jiance(txl, name);
if (jieguo != -1)
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
txl->renyuan[jieguo].name = name;
cout << "请输入性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int xingbie = 0;
while (true)
{
cin >> xingbie;
if (xingbie == 1 || xingbie == 2)
{
txl->renyuan[jieguo].xingbie = xingbie;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0)
{
txl->renyuan[jieguo].age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入联系电话:" << endl;
string pn;
cin >> pn;
txl->renyuan[jieguo].phoneNumber = pn;
cout << "请输入家庭住址:" << endl;
string zhuzhi;
cin >> zhuzhi;
txl->renyuan[jieguo].dizhi = zhuzhi;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void qingkong(tongxunlu* txl)
{
if (txl->renshu != 0)
{
flag:
cout << "是否确定清空通讯录,清空后无法恢复!" << endl;
cout << "1 --- 清空" << endl;
cout << "2 --- 不清空" << endl;
int jieguo = 0;
cin >> jieguo;
if (jieguo == 1)
{
txl->renshu = 0;
cout << "通讯录已清空" << endl;
}
else if (jieguo == 2)
cout << "已取消" << endl;
else
{
system("cls");
cout << "错误!请重新输入:" << endl;
goto flag;
}
}
else
{
cout << "当前的记录为空" << endl;
}
system("pause");
system("cls");
}
void caidan()
{
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()
{
tongxunlu txl;
txl.renshu = 0;
int xuanze = 0;
while (true)
{
caidan();
cin >> xuanze;
switch (xuanze)
{
case 1:
tianjia(&txl);
break;
case 2:
xianshi(&txl);
break;
case 3:
shanchu(&txl);
break;
case 4:
chazhao(&txl);
break;
case 5:
xiugai(&txl);
break;
case 6:
qingkong(&txl);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "请重新输入:" << endl;
break;
}
}
}
|