鱼C论坛

 找回密码
 立即注册
查看: 2336|回复: 5

[作品展示] c++简易通讯录

[复制链接]
发表于 2022-9-24 19:10:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 蜜柑面包 于 2022-9-24 19:11 编辑
  1. #include<iostream>
  2. #include<string>

  3. #define zuidarenshu 1000

  4. using namespace std;

  5. struct lianxiren
  6. {
  7.         string name;
  8.         int xingbie;//1男2女
  9.         int age;
  10.         string phoneNumber;
  11.         string dizhi;
  12. };

  13. struct tongxunlu
  14. {
  15.         struct lianxiren renyuan[zuidarenshu];
  16.         int renshu;
  17. };

  18. void tianjia(tongxunlu* txl)
  19. {
  20.         if (txl->renshu == zuidarenshu)
  21.         {
  22.                 cout << "通讯录已满,无法添加!" << endl;
  23.                 return;
  24.         }
  25.         else
  26.         {
  27.                 string name;
  28.                 cout << "请输入姓名:" << endl;
  29.                 cin >> name;
  30.                 txl->renyuan[txl->renshu].name = name;

  31.                 cout << "请输入性别:" << endl;
  32.                 cout << "1 --- 男" << endl;
  33.                 cout << "2 --- 女" << endl;
  34.                 int xingbie = 0;

  35.                 while (true)
  36.                 {
  37.                         cin >> xingbie;
  38.                         if (xingbie == 1 || xingbie == 2)
  39.                         {
  40.                                 txl->renyuan[txl->renshu].xingbie = xingbie;
  41.                                 break;
  42.                         }
  43.                         cout << "输入有误,请重新输入" << endl;
  44.                 }

  45.                 cout << "请输入年龄:" << endl;
  46.                 int age = 0;

  47.                 while (true)
  48.                 {
  49.                         cin >> age;
  50.                         if (age > 0)
  51.                         {
  52.                                 txl->renyuan[txl->renshu].age = age;
  53.                                 break;
  54.                         }
  55.                         cout << "输入有误,请重新输入" << endl;
  56.                 }

  57.                 cout << "请输入联系电话:" << endl;
  58.                 string pn;
  59.                 cin >> pn;
  60.                 txl->renyuan[txl->renshu].phoneNumber = pn;

  61.                 cout << "请输入家庭住址:" << endl;
  62.                 string zhuzhi;
  63.                 cin >> zhuzhi;
  64.                 txl->renyuan[txl->renshu].dizhi = zhuzhi;

  65.                 txl->renshu++;

  66.                 cout << "添加成功" << endl;

  67.                 system("pause");
  68.                 system("cls");
  69.         }
  70. }

  71. void xianshi(tongxunlu* txl)
  72. {
  73.         if (txl->renshu == 0)
  74.         {
  75.                 cout << "当前的记录为空" << endl;
  76.         }
  77.         else
  78.         {
  79.                 for (int i = 0; i < txl->renshu; i++)
  80.                 {
  81.                         cout << "姓名: " << txl->renyuan[i].name << '\t';
  82.                         cout << "性别: " << (txl->renyuan[i].xingbie == 1 ? "男" : "女") << '\t';
  83.                         cout << "年龄: " << txl->renyuan[i].age << '\t';
  84.                         cout << "电话: " << txl->renyuan[i].phoneNumber << '\t';
  85.                         cout << "住址: " << txl->renyuan[i].dizhi << endl;
  86.                 }
  87.         }

  88.         system("pause");
  89.         system("cls");
  90. }

  91. int jiance(tongxunlu* txl, string name)
  92. {
  93.         for (int i = 0; i < txl->renshu; i++)
  94.         {
  95.                 if (txl->renyuan[i].name == name)
  96.                 {
  97.                         return i;
  98.                 }
  99.         }
  100.         return -1;
  101. }

  102. void shanchu(tongxunlu* txl)
  103. {
  104.         cout << "请输入您要删除的联系人:" << endl;

  105.         string name;
  106.         cin >> name;

  107.         int jieguo = jiance(txl, name);

  108.         if (jieguo != -1)
  109.         {
  110.                 for (int i = jieguo; i < txl->renshu; i++)
  111.                 {
  112.                         txl->renyuan[i] = txl->renyuan[i + 1];
  113.                 }
  114.                 txl->renshu--;
  115.                 cout << "删除成功" << endl;
  116.         }
  117.         else
  118.         {
  119.                 cout << "查无此人" << endl;
  120.         }

  121.         system("pause");
  122.         system("cls");
  123. }

  124. void chazhao(tongxunlu* txl)
  125. {
  126.         cout << "请输入您要查找的联系人:" << endl;
  127.         string name;
  128.         cin >> name;

  129.         int jieguo = jiance(txl, name);

  130.         if (jieguo != -1)
  131.         {
  132.                 cout << "姓名: " << txl->renyuan[jieguo].name << '\t';
  133.                 cout << "性别: " << (txl->renyuan[jieguo].xingbie == 1 ? "男" : "女") << '\t';
  134.                 cout << "年龄: " << txl->renyuan[jieguo].age << '\t';
  135.                 cout << "号码: " << txl->renyuan[jieguo].phoneNumber << '\t';
  136.                 cout << "住址: " << txl->renyuan[jieguo].dizhi << endl;
  137.         }
  138.         else
  139.         {
  140.                 cout << "查无此人" << endl;
  141.         }

  142.         system("pause");
  143.         system("cls");
  144. }

  145. void xiugai(tongxunlu* txl)
  146. {
  147.         cout << "请输入您要修改的联系人:" << endl;
  148.         string name;
  149.         cin >> name;

  150.         int jieguo = jiance(txl, name);

  151.         if (jieguo != -1)
  152.         {
  153.                 string name;
  154.                 cout << "请输入姓名:" << endl;
  155.                 cin >> name;
  156.                 txl->renyuan[jieguo].name = name;

  157.                 cout << "请输入性别:" << endl;
  158.                 cout << "1 --- 男" << endl;
  159.                 cout << "2 --- 女" << endl;
  160.                 int xingbie = 0;

  161.                 while (true)
  162.                 {
  163.                         cin >> xingbie;
  164.                         if (xingbie == 1 || xingbie == 2)
  165.                         {
  166.                                 txl->renyuan[jieguo].xingbie = xingbie;
  167.                                 break;
  168.                         }
  169.                         cout << "输入有误,请重新输入" << endl;
  170.                 }

  171.                 cout << "请输入年龄:" << endl;
  172.                 int age = 0;

  173.                 while (true)
  174.                 {
  175.                         cin >> age;
  176.                         if (age > 0)
  177.                         {
  178.                                 txl->renyuan[jieguo].age = age;
  179.                                 break;
  180.                         }
  181.                         cout << "输入有误,请重新输入" << endl;
  182.                 }

  183.                 cout << "请输入联系电话:" << endl;
  184.                 string pn;
  185.                 cin >> pn;
  186.                 txl->renyuan[jieguo].phoneNumber = pn;

  187.                 cout << "请输入家庭住址:" << endl;
  188.                 string zhuzhi;
  189.                 cin >> zhuzhi;
  190.                 txl->renyuan[jieguo].dizhi = zhuzhi;

  191.                 cout << "修改成功" << endl;
  192.         }
  193.         else
  194.         {
  195.                 cout << "查无此人" << endl;
  196.         }

  197.         system("pause");
  198.         system("cls");
  199. }

  200. void qingkong(tongxunlu* txl)
  201. {
  202.         if (txl->renshu != 0)
  203.         {
  204.                 flag:
  205.                 cout << "是否确定清空通讯录,清空后无法恢复!" << endl;
  206.                 cout << "1 --- 清空" << endl;
  207.                 cout << "2 --- 不清空" << endl;
  208.                 int jieguo = 0;
  209.                 cin >> jieguo;
  210.                 if (jieguo == 1)
  211.                 {
  212.                         txl->renshu = 0;
  213.                         cout << "通讯录已清空" << endl;
  214.                 }
  215.                 else if (jieguo == 2)
  216.                         cout << "已取消" << endl;
  217.                 else
  218.                 {
  219.                        
  220.                         system("cls");

  221.                         cout << "错误!请重新输入:" << endl;

  222.                         goto flag;
  223.                 }
  224.         }
  225.         else
  226.         {
  227.                 cout << "当前的记录为空" << endl;
  228.         }
  229.         system("pause");
  230.         system("cls");
  231. }

  232. void caidan()
  233. {
  234.         cout << "***************************" << endl;
  235.         cout << "*****  1、添加联系人  *****" << endl;
  236.         cout << "*****  2、显示联系人  *****" << endl;
  237.         cout << "*****  3、删除联系人  *****" << endl;
  238.         cout << "*****  4、查找联系人  *****" << endl;
  239.         cout << "*****  5、修改联系人  *****" << endl;
  240.         cout << "*****  6、清空联系人  *****" << endl;
  241.         cout << "*****  0、退出通讯录  *****" << endl;
  242.         cout << "***************************" << endl;
  243. }

  244. int main()
  245. {
  246.         tongxunlu txl;

  247.         txl.renshu = 0;

  248.         int xuanze = 0;

  249.         while (true)
  250.         {
  251.                 caidan();

  252.                 cin >> xuanze;
  253.                
  254.                 switch (xuanze)
  255.                 {
  256.                 case 1:
  257.                         tianjia(&txl);
  258.                         break;
  259.                 case 2:
  260.                         xianshi(&txl);
  261.                         break;
  262.                 case 3:
  263.                         shanchu(&txl);
  264.                         break;
  265.                 case 4:
  266.                         chazhao(&txl);
  267.                         break;
  268.                 case 5:
  269.                         xiugai(&txl);
  270.                         break;
  271.                 case 6:
  272.                         qingkong(&txl);
  273.                         break;
  274.                 case 0:
  275.                         cout << "欢迎下次使用" << endl;
  276.                         system("pause");
  277.                         return 0;
  278.                         break;
  279.                 default:
  280.                         cout << "请重新输入:" << endl;
  281.                         break;
  282.                 }
  283.         }
  284. }


复制代码

评分

参与人数 1荣誉 +5 收起 理由
高山 + 5 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-9-24 19:12:50 | 显示全部楼层
有bug可以指出
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-25 10:51:00 | 显示全部楼层
挺不错的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-27 20:50:48 | 显示全部楼层
功能很简单,但是却很稳定,没有bug,建议加上
  1. #include
  2. system("color 数字")
复制代码

我觉得会更好哟
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-28 10:13:19 | 显示全部楼层
发表一些我自己的看法吧,不喜勿喷。
我觉得这个代码定义名称的时候不要写拼音,比如通讯录可以写AddressBook、联系人可以写AddressPerson、最大人数就写MAXimum等,比拼音直接容易看懂。
然后你用1/2判断性别的时候,如果用户不输入这两个字的情况你也应该考虑,或者用户不输入数字,输入其他字符或者汉字。这个时候你的程序是否还能正常运行下去。
我的建议是内存足够的情况下,能尽量加判断语句就加吧,毕竟不是每个用户都那么“乖”。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-28 14:51:15 | 显示全部楼层
C++是面向对象的,这里没有对象,就是用了C++的语法干了C的事
是否能把员工弄成一个类,加人就NEW一个,减人就DELETE一个
我正在学C++,咱们共同加油
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 14:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表