c++简易通讯录
本帖最后由 蜜柑面包 于 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;
int renshu;
};
void tianjia(tongxunlu* txl)
{
if (txl->renshu == zuidarenshu)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
txl->renyuan.name = name;
cout << "请输入性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int xingbie = 0;
while (true)
{
cin >> xingbie;
if (xingbie == 1 || xingbie == 2)
{
txl->renyuan.xingbie = xingbie;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0)
{
txl->renyuan.age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入联系电话:" << endl;
string pn;
cin >> pn;
txl->renyuan.phoneNumber = pn;
cout << "请输入家庭住址:" << endl;
string zhuzhi;
cin >> zhuzhi;
txl->renyuan.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.name << '\t';
cout << "性别: " << (txl->renyuan.xingbie == 1 ? "男" : "女") << '\t';
cout << "年龄: " << txl->renyuan.age << '\t';
cout << "电话: " << txl->renyuan.phoneNumber << '\t';
cout << "住址: " << txl->renyuan.dizhi << endl;
}
}
system("pause");
system("cls");
}
int jiance(tongxunlu* txl, string name)
{
for (int i = 0; i < txl->renshu; i++)
{
if (txl->renyuan.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 = txl->renyuan;
}
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.name << '\t';
cout << "性别: " << (txl->renyuan.xingbie == 1 ? "男" : "女") << '\t';
cout << "年龄: " << txl->renyuan.age << '\t';
cout << "号码: " << txl->renyuan.phoneNumber << '\t';
cout << "住址: " << txl->renyuan.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.name = name;
cout << "请输入性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int xingbie = 0;
while (true)
{
cin >> xingbie;
if (xingbie == 1 || xingbie == 2)
{
txl->renyuan.xingbie = xingbie;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0)
{
txl->renyuan.age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入联系电话:" << endl;
string pn;
cin >> pn;
txl->renyuan.phoneNumber = pn;
cout << "请输入家庭住址:" << endl;
string zhuzhi;
cin >> zhuzhi;
txl->renyuan.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;
}
}
}
有bug可以指出 挺不错的{:10_256:} 功能很简单,但是却很稳定,没有bug,建议加上
#include
system("color 数字")
我觉得会更好哟{:10_254:} 发表一些我自己的看法吧,不喜勿喷。
我觉得这个代码定义名称的时候不要写拼音,比如通讯录可以写AddressBook、联系人可以写AddressPerson、最大人数就写MAXimum等,比拼音直接容易看懂。
然后你用1/2判断性别的时候,如果用户不输入这两个字的情况你也应该考虑,或者用户不输入数字,输入其他字符或者汉字。这个时候你的程序是否还能正常运行下去。
我的建议是内存足够的情况下,能尽量加判断语句就加吧,毕竟不是每个用户都那么“乖”。 C++是面向对象的,这里没有对象,就是用了C++的语法干了C的事
是否能把员工弄成一个类,加人就NEW一个,减人就DELETE一个
我正在学C++,咱们共同加油
页:
[1]