鱼C论坛

 找回密码
 立即注册
查看: 1993|回复: 17

[作品展示] 浅浅玩一下,通讯录管理系统,如何使用C++来做(低级C++,其实够高级了)

[复制链接]
发表于 2023-1-9 08:52:55 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 C丁洞杀O 于 2023-1-9 18:44 编辑

emmm,不是大佬的我却始终想要模仿大佬,嗯,这次非常简单所以我就不写注释了,很整齐的,对吧?
废话不多说!上代码 (随意抄,随意改,无版权)
  1. // 代码可以改,高级版的话,我在做,大概实现的功能是导入通讯录,导出通讯录,可能在加个窗口,菜单啥的
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <strmif.h>
  7. #include <algorithm>
  8. #include <memory>
  9. using namespace std;
  10. #define MAXPATH 0xFFFFFFF

  11. void update(short a=0) {
  12.         if (a == 1)
  13.         {
  14.                 system("pause");
  15.         }
  16. #ifdef  _WIN32
  17.         system("cls");
  18. #endif //  _WIN32
  19.         system("claer");
  20.        
  21. }

  22. class MailList {
  23. protected:
  24.         struct  Person
  25.         {
  26.                 size_t _id;
  27.                 string _name;
  28.                 short _callP[11]; // 电话
  29.                 char _sex;        // 性别
  30.                 // Person(const Person&) = delete;
  31.                 Person() {
  32.                         _id = 0;
  33.                         _name = "";
  34.                         _sex = 0;
  35.                         for (size_t i = 0; i < 11; i++)
  36.                         {
  37.                                 _callP[i] = 0;
  38.                         }
  39.                 }
  40.                 void operator=(Person p) {
  41.                         _id = p._id;
  42.                         _name = p._name;
  43.                         for (size_t i = 0; i < 11; i++)
  44.                         {
  45.                                 _callP[i] = p._callP[i];
  46.                         }
  47.                         _sex = p._sex;
  48.                 }
  49.         };

  50.         void setCallP(Person& p, const char* callP) {
  51.                 for (size_t i = 0; i < 11; i++)
  52.                 {
  53.                         p._callP[i] = callP[i] - '0';
  54.                 }
  55.         }
  56. public:
  57.         MailList(const MailList&) = delete;
  58.         MailList() {
  59.                 me = new Person;
  60.                 me->_id = 0;
  61.                 me->_name = "自己";
  62.                 setCallP(*me, "0000000000");
  63.                 me->_sex = '_';
  64.         }
  65.         // 添加联系人
  66.         void addPerson(string _name,const char* _callP,char _sex) {
  67.                 Person temp;
  68.                 temp._id = getMailListLenth() + 1;
  69.                 temp._name = _name;
  70.                 setCallP(temp, _callP);
  71.                 temp._sex = _sex;
  72.                 callP.push_back(temp);
  73.                 cout << temp._name <<" : 添加成功!\n";
  74.                 update(1);
  75.         }
  76.         // 显示联系人
  77.         void showAllPerson() {
  78.                 size_t i = 1;
  79.                 for (auto it = callP.begin(); it < callP.end(); it++)
  80.                 {
  81.                         it->_id = i;
  82.                         cout << it->_id << ":" << it->_name << "; ";
  83.                         for (size_t i = 0; i < 11; i++)
  84.                         {
  85.                                 cout << it->_callP[i];
  86.                         }
  87.                         cout <<"; "<< it->_sex << endl;
  88.                         i++;
  89.                 }
  90.                 update(1);
  91.         }
  92.         // 获取联系人
  93.         size_t getPerson(string _name) {
  94.                 bool isFind=false;
  95.                 for (auto it = callP.begin(); it < callP.end(); it++)
  96.                 {
  97.                         if (it->_name == _name)
  98.                         {
  99.                                 isFind = true;
  100.                                 cout << it->_id << ": " << it->_name << "; ";
  101.                                 for (size_t i = 0; i < 11; i++)
  102.                                 {
  103.                                         cout << it->_callP[i];
  104.                                 }
  105.                                 cout << "; " << it->_sex << endl;
  106.                                 /*update(1);
  107.                                 return it->_id;*/
  108.                         }
  109.                 }
  110.                 if (!isFind)
  111.                 {
  112.                         cout << "未能找到, 请检查后再次输入!\n";
  113.                 }
  114.                 update(1);
  115.                 return 0;
  116.         }
  117.         // 删除联系人
  118.         void deletePerson(size_t _id) {
  119.                 bool rightFind = false;
  120.                 // 别见外,我不会vector的消除,干脆这样吧
  121.                 vector<Person> temp;
  122.                 for (auto it = callP.begin(); it < callP.end(); it++)
  123.                 {
  124.                         if (!(it->_id == _id))
  125.                         {
  126.                                 temp.push_back(*it);
  127.                         }
  128.                         else {
  129.                                 rightFind = true;
  130.                         }
  131.                 }
  132.                 callP = temp;
  133.                 if (rightFind)
  134.                 {
  135.                         cout << "删除成功!";
  136.                 }
  137.                 update(1);
  138.         }
  139.         // 清空联系人
  140.         void clearAllPerson() {
  141.                 callP.clear();
  142.                 cout << "清理成功!";
  143.                 update(1);
  144.         }
  145.         // 修改联系人
  146.         void resetPerson(size_t _id) {
  147.                 _id = _id - 1;
  148.                 update(0);
  149.                 while(1){
  150.                         cout << "1 修改名称 2 修改电话 3 修改性别 E 退出 \n";
  151.                         char command;
  152.                         command = getchar();
  153.                         switch (command)
  154.                         {
  155.                         case '1':
  156.                         {
  157.                                 string name;
  158.                                 cin >> name;
  159.                                 callP[_id]._name = name;
  160.                         }
  161.                         break;
  162.                         case '2':
  163.                         {
  164.                                 cout << "输入电话号码\n01234567890\n";
  165.                                 char _callP[20];
  166.                                 cin >> _callP;
  167.                                 if (!(strlen(_callP) == 11))
  168.                                 {
  169.                                         cout << "电话输入错误!" << endl;
  170.                                         cin >> _callP;
  171.                                 }
  172.                                 setCallP(callP[_id], _callP);
  173.                         }
  174.                                 break;
  175.                         case '3':
  176.                         {
  177.                                 cout << "请输入性别\n";
  178.                                 char sex;
  179.                                 cin >> sex;
  180.                                 callP[_id]._sex = sex;
  181.                         }
  182.                                 break;
  183.                         case 'e':case 'E':
  184.                                 return;
  185.                         default:
  186.                                 break;
  187.                         }
  188.                 }
  189.                 update(0);
  190.         }
  191.         ~MailList() {
  192.                 delete me;
  193.         }
  194. protected:
  195.         size_t getMailListLenth() const{
  196.                 return callP.size();
  197.         }
  198. private:
  199.         Person* me;
  200.         vector<Person> callP;
  201.        
  202. };

  203. //class MailListVIP : MailList {
  204. //
  205. //};

  206. class Command {
  207. public:
  208.         Command(const Command&) = delete;
  209.         Command() {
  210.                 cout << "-欢迎使用本通讯录管理系统-" << endl;
  211.                 cout << "----- 作者:C丁洞杀O -----" << endl;
  212.                 cout << "----- Made in China  -----" << endl;
  213.                 cout << "----- from Fishc.com -----" << endl;
  214.                 cout << "--- It's free to study ---" << endl;
  215.                 update(1);
  216.                 PmailList = new MailList;
  217.         }

  218.         void showMenu() {
  219.                 cout<<"$ 通讯录管理系统:菜单(Menu) $ \n";
  220.                 cout<<"##############################\n";
  221.                 cout<<"#######  1、添加联系人  #######\n";
  222.                 cout<<"#######  2、显示联系人  #######\n";
  223.                 cout<<"#######  3、删除联系人  #######\n";
  224.                 cout<<"#######  4、查找联系人  #######\n";
  225.                 cout<<"#######  5、清空联系人  #######\n";
  226.                 cout<<"#######  6、修改联系人  #######\n";
  227.                 cout<<"#######  E、退出此程序  #######\n";
  228.                 cout<<"##############################\n";
  229.                 getCommand();
  230.                 update();
  231.         }

  232.         int run() {
  233.                 while (1)
  234.                 {
  235.                         showMenu();
  236.                 }
  237.                
  238.         }

  239.         void getCommand() {
  240.                 string command;
  241.                 cin>>command;

  242.                 switch (command[0])
  243.                 {
  244.                 case '1':// 添加联系人
  245.                 {

  246.                         cout << "功能:添加联系人\n";
  247.                         string _name;
  248.                         char callP[20];
  249.                         char sex;
  250.                         cin >> _name >> callP >> sex;
  251.                         if (!(strlen(callP) == 11))
  252.                         {
  253.                                 cout << "电话输入错误!" << endl;
  254.                                 cin >> callP;
  255.                         }
  256.                         PmailList->addPerson(_name, callP, sex);
  257.                 }
  258.                         break;
  259.                 case '2':// 显示联系人
  260.                 {
  261.                         cout << "功能:显示联系人\n";
  262.                         PmailList->showAllPerson();
  263.                 }
  264.                         break;
  265.                 case '3': // 删除联系人
  266.                 {
  267.                         cout << "功能:删除联系人\n";
  268.                         cout << "输入联系人id进行删除!";
  269.                         size_t id;
  270.                         cin >> id;
  271.                         PmailList->deletePerson(id);
  272.                 }
  273.                         break;
  274.                 case '4':// 查找联系人
  275.                 {
  276.                         cout << "功能:查找联系人\n";
  277.                         cout << "输入名称查找联系人!";
  278.                         string name;
  279.                         cin >> name;
  280.                         PmailList->getPerson(name);
  281.                 }
  282.                         break;
  283.                 case '5':// 清空联系人
  284.                 {
  285.                         cout << "功能:清空联系人\n";
  286.                         PmailList->clearAllPerson();
  287.                 }
  288.                         break;
  289.                 case '6': // 修改联系人
  290.                 {
  291.                         cout << "功能:修改联系人\n";
  292.                         cout << "输入id修改联系人 $";
  293.                         size_t id;
  294.                         cin >> id;
  295.                         PmailList->resetPerson(id);
  296.                 }
  297.                         break;
  298.                 case 'E':case 'e':
  299.                         update();
  300.                         cout << "欢迎下次光临,本系统,永远为您服务!\n";
  301.                         update(1);
  302.                         exit(0);
  303.                         break;
  304.                 default:
  305.                         cout << "代码未能解析,请重新输入\n";
  306.                         update(1);
  307.                         break;
  308.                 }
  309.         }
  310.         ~Command() {
  311.                 delete PmailList;
  312.                 PmailList = nullptr;        // 删除函数,防止野指针
  313.         }
  314.        
  315.         MailList* PmailList;
  316.         // unique_ptr<MailList> pMailList; // 使用智能指针(其实不要担心)
  317.         // MailListVIP* ImailList;
  318. };

  319. int main() {
  320.         Command cmd;
  321.         return cmd.run();
  322. }
复制代码

有人反馈了一些问题,已改;
补充一下,保证编译环境为Vs2019;Debug;x86;多字节字符集;控制台程序;Windows11;其他环境不保证!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-1-9 08:57:23 | 显示全部楼层
我把大部分的注释都删掉了,emmm,最后留下的只是功能实现代码,当然,我没有刻意追求效率,否则的话,这个代码可以上1000行了,查找,搜索,啥的都可以用算法,还有,其实,可以做网络版的,就是真的能通话的那种,完整版的话,我觉得要做个局域网聊天+通讯录管理+窗口+数据保存,然后在做点聊天室,当然,这不可能一个程序完成,分成多个,甚至是搭配dll, lib等等,完整的项目就出来了,对吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 09:32:48 | 显示全部楼层

回帖奖励 +1 鱼币

编译报错了,请问有头绪吗?
  1. test.cpp:28:22: error: definition of implicit copy constructor for 'Person' is deprecated because it has a user-provided copy assignment operator [-Werror,-Wdeprecated-copy-with-user-provided-copy]
  2.                 void operator=(Person& p) {
  3.                      ^
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-9 10:27:11 | 显示全部楼层
dolly_yos2 发表于 2023-1-9 09:32
编译报错了,请问有头绪吗?

文件格式cpp, 编译标准C++14,Debug x86 Vs2019
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 12:20:41 | 显示全部楼层
C丁洞杀O 发表于 2023-1-9 10:27
文件格式cpp, 编译标准C++14,Debug x86 Vs2019

问题太多了,只是程序能解析出来的就画了满屏幕的波浪线。就这样吧。
可能离您理想的 C++ 应该有的样子和/或效率还有距离
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-9 13:21:07 | 显示全部楼层
dolly_yos2 发表于 2023-1-9 12:20
问题太多了,只是程序能解析出来的就画了满屏幕的波浪线。就这样吧。
可能离您理想的 C++ 应该有的样子 ...

我的编译器没有问题呀,无论是编译还是链接都没有问题,警告请无视,那个是版本问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 13:44:49 | 显示全部楼层
C丁洞杀O 发表于 2023-1-9 13:21
我的编译器没有问题呀,无论是编译还是链接都没有问题,警告请无视,那个是版本问题
  1. In file included from /usr/include/c++/12.2.0/string:50,
  2.                  from /usr/include/c++/12.2.0/bits/locale_classes.h:40,
  3.                  from /usr/include/c++/12.2.0/bits/ios_base.h:41,
  4.                  from /usr/include/c++/12.2.0/ios:42,
  5.                  from /usr/include/c++/12.2.0/ostream:38,
  6.                  from /usr/include/c++/12.2.0/iostream:39,
  7.                  from main.cpp:2:
  8. /usr/include/c++/12.2.0/bits/stl_algobase.h: In instantiation of ‘static constexpr _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const MailList::Person*; _OI = MailList::Person*]’:
  9. /usr/include/c++/12.2.0/bits/stl_algobase.h:492:12:   required from ‘constexpr _OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = const MailList::Person*; _OI = MailList::Person*]’
  10. /usr/include/c++/12.2.0/bits/stl_algobase.h:522:42:   required from ‘constexpr _OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = const MailList::Person*; _OI = MailList::Person*]’
  11. /usr/include/c++/12.2.0/bits/stl_algobase.h:530:31:   required from ‘constexpr _OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const MailList::Person*, vector<MailList::Person> >; _OI = __gnu_cxx::__normal_iterator<MailList::Person*, vector<MailList::Person> >]’
  12. /usr/include/c++/12.2.0/bits/stl_algobase.h:620:7:   required from ‘constexpr _OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const MailList::Person*, vector<MailList::Person> >; _OI = __gnu_cxx::__normal_iterator<MailList::Person*, vector<MailList::Person> >]’
  13. /usr/include/c++/12.2.0/bits/vector.tcc:244:31:   required from ‘constexpr std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = MailList::Person; _Alloc = std::allocator<MailList::Person>]’
  14. main.cpp:106:17:   required from here
  15. /usr/include/c++/12.2.0/bits/stl_algobase.h:385:25: error: binding reference of type ‘MailList::Person&’ to ‘const MailList::Person’ discards qualifiers
  16.   385 |               *__result = *__first;
  17.       |               ~~~~~~~~~~^~~~~~~~~~
  18. main.cpp:26:32: note:   initializing argument 1 of ‘void MailList::Person::operator=(MailList::Person&)’
  19.    26 |         void operator=(Person &p) {
  20.       |                        ~~~~~~~~^
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 13:50:12 From FishC Mobile | 显示全部楼层
C丁洞杀O 发表于 2023-1-9 13:21
我的编译器没有问题呀,无论是编译还是链接都没有问题,警告请无视,那个是版本问题

我理解,我们的编译器大不相同,不过这些问题我能轻松解决
问题的核心在于您的实现。恕我直言,您没能发挥您热爱的 C++ 的潜力,有很多能够用程序自动检测出的,有调优空间的地方,比如不必要的复制传递
一些警告也不是版本问题,而是代码编写的错误或者说至少是容易导致使用时出现问题的地方,比如复制赋值运算符的重载导致隐式声明的复制构造函数被弃用,C++ 的通常范式下如果重载了复制赋值运算符就应该同时自定义赋值构造函数和析构函数( Rule of Three )
new 申请的空间没有对应的 delete,不过也有看法认为固定申请常数次、保有到程序结束而未释放的内存不算内存泄漏,姑且不管了
一些设计上的问题更是见仁见智,这里就更不多说了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 13:53:56 | 显示全部楼层
  1. // 代码可以改,高级版的话,我在做,大概实现的功能是导入通讯录,导出通讯录,可能在加个窗口,菜单啥的
  2. #include <iostream>
  3. #include <map>
  4. #include <vector>
  5. //#include <strmif.h>
  6. #include <string.h>
  7. #include <algorithm>
  8. #include <string>

  9. using namespace std;

  10. #define MAXPATH 0xFFFFFFF

  11. void update(short a = 0) {
  12.     if(a == 1) system("pause");
  13.     //system("cls");
  14.     system("clear");
  15. }

  16. class MailList {
  17. protected:
  18.     struct Person {
  19.         size_t _id;
  20.         string _name;
  21.         short _callP[11]; // 电话
  22.         char _sex;        // 性别
  23.         //void operator=(Person &p) {
  24.         void operator=(const Person &p) {
  25.             _id = p._id;
  26.             _name = p._name;
  27.             for(size_t i = 0; i < 11; i++) {
  28.                 _callP[i] = p._callP[i];
  29.             }
  30.             _sex = p._sex;
  31.         }
  32.     };
  33.     void setCallP(Person &p, const char *callP) {
  34.         for(size_t i = 0; i < 11; i++) {
  35.             p._callP[i] = callP[i] - '0';
  36.         }
  37.     }

  38. public:
  39.     MailList() {
  40.         me._id = 0;
  41.         me._name = "自己";
  42.         setCallP(me, "0000000000");
  43.         me._sex = '_';
  44.     }
  45.     // 添加联系人
  46.     void addPerson(string _name, const char *_callP, char _sex) {
  47.         Person temp;
  48.         temp._id = getMailListLenth() + 1;
  49.         temp._name = _name;
  50.         setCallP(temp, _callP);
  51.         temp._sex = _sex;
  52.         callP.push_back(temp);
  53.         cout << temp._name << " : 添加成功!\n";
  54.         update(1);
  55.     }
  56.     // 显示联系人
  57.     void showAllPerson() {
  58.         size_t i = 1;
  59.         for(auto it = callP.begin(); it < callP.end(); it++) {
  60.             it->_id = i;
  61.             cout << it->_id << ":" << it->_name << "; ";
  62.             for(size_t i = 0; i < 11; i++) {
  63.                 cout << it->_callP[i];
  64.             }
  65.             cout << "; " << it->_sex << endl;
  66.             i++;
  67.         }
  68.         update(1);
  69.     }
  70.     // 获取联系人
  71.     size_t getPerson(string _name) {
  72.         bool isFind = false;
  73.         for(auto it = callP.begin(); it < callP.end(); it++) {
  74.             if(it->_name == _name) {
  75.                 isFind = true;
  76.                 cout << it->_id << ": " << it->_name << "; ";
  77.                 for(size_t i = 0; i < 11; i++) {
  78.                     cout << it->_callP[i];
  79.                 }
  80.                 cout << "; " << it->_sex << endl;
  81.                 /*update(1);
  82.                 return it->_id;*/
  83.             }
  84.         }
  85.         if(!isFind) {
  86.             cout << "未能找到, 请检查后再次输入!\n";
  87.         }
  88.         update(1);
  89.         return 0;
  90.     }
  91.     // 删除联系人
  92.     void deletePerson(size_t _id) {
  93.         bool rightFind = false;
  94.         // 别见外,我不会vector的消除,干脆这样吧
  95.         vector<Person> temp;
  96.         for(auto it = callP.begin(); it < callP.end(); it++) {
  97.             if(!(it->_id == _id)) {
  98.                 temp.push_back(*it);
  99.             } else {
  100.                 rightFind = true;
  101.             }
  102.         }
  103.         callP = temp;
  104.         if(rightFind) {
  105.             cout << "删除成功!";
  106.         }
  107.         update(1);
  108.     }
  109.     // 清空联系人
  110.     void clearAllPerson() {
  111.         callP.clear();
  112.         cout << "清理成功!";
  113.         update(1);
  114.     }
  115.     // 修改联系人
  116.     void resetPerson(size_t _id) {
  117.         _id = _id - 1;
  118.         update(0);
  119.         while(1) {
  120.             cout << "1 修改名称 2 修改电话 3 修改性别 E 退出 \n";
  121.             char command;
  122.             command = getchar();
  123.             switch(command) {
  124.             case '1': {
  125.                 string name;
  126.                 cin >> name;
  127.                 callP[_id]._name = name;
  128.             } break;
  129.             case '2': {
  130.                 cout << "输入电话号码\n01234567890\n";
  131.                 char _callP[20];
  132.                 cin >> _callP;
  133.                 if(!(strlen(_callP) == 11)) {
  134.                     cout << "电话输入错误!" << endl;
  135.                     cin >> _callP;
  136.                 }
  137.                 setCallP(callP[_id], _callP);
  138.             } break;
  139.             case '3': {
  140.                 cout << "请输入性别\n";
  141.                 char sex;
  142.                 cin >> sex;
  143.                 callP[_id]._sex = sex;
  144.             } break;
  145.             case 'e':
  146.             case 'E':
  147.                 return;
  148.             default:
  149.                 break;
  150.             }
  151.         }
  152.         update(0);
  153.     }

  154. protected:
  155.     size_t getMailListLenth() const { return callP.size(); }

  156. private:
  157.     Person me;
  158.     vector<Person> callP;
  159. };

  160. // class MailListVIP : MailList {
  161. //
  162. // };

  163. class Command {
  164. public:
  165.     Command() {
  166.         cout << "-欢迎使用本通讯录管理系统-" << endl;
  167.         cout << "----- 作者:C丁洞杀O -----" << endl;
  168.         cout << "----- Made in China  -----" << endl;
  169.         cout << "----- from Fishc.com -----" << endl;
  170.         cout << "--- It's free to study ---" << endl;
  171.         update(1);
  172.         PmailList = new MailList;
  173.     }

  174.     // 不需要释放内存吗?
  175.     ~Command() {delete PmailList;}

  176.     void showMenu() {
  177.         cout << "$ 通讯录管理系统:菜单(Menu) $ \n";
  178.         cout << "##############################\n";
  179.         cout << "#######  1、添加联系人  #######\n";
  180.         cout << "#######  2、显示联系人  #######\n";
  181.         cout << "#######  3、删除联系人  #######\n";
  182.         cout << "#######  4、查找联系人  #######\n";
  183.         cout << "#######  5、清空联系人  #######\n";
  184.         cout << "#######  6、修改联系人  #######\n";
  185.         cout << "#######  E、退出此程序  #######\n";
  186.         cout << "##############################\n";
  187.         getCommand();
  188.         update();
  189.     }

  190.     int run() {
  191.         while(1) {
  192.             showMenu();
  193.         }
  194.     }

  195.     void getCommand() {
  196.         string command;
  197.         cin >> command;

  198.         switch(command[0]) {
  199.         case '1': // 添加联系人
  200.         {
  201.             cout << "功能:添加联系人\n";
  202.             string _name;
  203.             char callP[20];
  204.             char sex;
  205.             cin >> _name >> callP >> sex;
  206.             if(!(strlen(callP) == 11)) {
  207.                 cout << "电话输入错误!" << endl;
  208.                 cin >> callP;
  209.             }
  210.             PmailList->addPerson(_name, callP, sex);
  211.         } break;
  212.         case '2': // 显示联系人
  213.         {
  214.             cout << "功能:显示联系人\n";
  215.             PmailList->showAllPerson();
  216.         } break;
  217.         case '3': // 删除联系人
  218.         {
  219.             cout << "功能:删除联系人\n";
  220.             cout << "输入联系人id进行删除!";
  221.             size_t id;
  222.             cin >> id;
  223.             PmailList->deletePerson(id);
  224.         } break;
  225.         case '4': // 查找联系人
  226.         {
  227.             cout << "功能:查找联系人\n";
  228.             cout << "输入名称查找联系人!";
  229.             string name;
  230.             cin >> name;
  231.             PmailList->getPerson(name);
  232.         } break;
  233.         case '5': // 清空联系人
  234.         {
  235.             cout << "功能:清空联系人\n";
  236.             PmailList->clearAllPerson();
  237.         } break;
  238.         case '6': // 修改联系人
  239.         {
  240.             cout << "功能:修改联系人\n";
  241.             cout << "输入id修改联系人 $";
  242.             size_t id;
  243.             cin >> id;
  244.             PmailList->resetPerson(id);
  245.         } break;
  246.         case 'E':
  247.         case 'e':
  248.             update();
  249.             cout << "欢迎下次光临,本系统,永远为您服务!\n";
  250.             update(1);
  251.             exit(0);
  252.             break;
  253.         default:
  254.             cout << "代码未能解析,请重新输入\n";
  255.             update(1);
  256.             break;
  257.         }
  258.     }

  259.     MailList *PmailList;
  260.     // MailListVIP* ImailList;
  261. };

  262. int main() {
  263.     Command cmd;
  264.     return cmd.run();
  265. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-9 15:44:48 | 显示全部楼层
dolly_yos2 发表于 2023-1-9 13:50
我理解,我们的编译器大不相同,不过这些问题我能轻松解决
问题的核心在于您的实现。恕我直言,您没能发 ...

我这编译器一般默认会提供构造函数和解析函数啊?这不是正常的吗?而且我也说了这是低级C++,有些地方我也没有用C++提供的算法库,是直接写的,其实我还能更加简单,但这不是怕不能用吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-9 15:45:22 | 显示全部楼层

Liunx版的?我是Windows上用的,可能会有差异吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 15:50:44 | 显示全部楼层
C丁洞杀O 发表于 2023-1-9 15:45
Liunx版的?我是Windows上用的,可能会有差异吧

嗯,所以操作系统/编译器特定的东西还是尽量不要用
因为你不知道 你的代码会运行在什么机器上

我这编译器一般默认会提供构造函数和解析函数
那个默认的析构函数会帮你 delete PmailList; 吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-9 15:52:28 | 显示全部楼层
本帖最后由 C丁洞杀O 于 2023-1-9 15:57 编辑
人造人 发表于 2023-1-9 15:50
嗯,所以操作系统/编译器特定的东西还是尽量不要用
因为你不知道 你的代码会运行在什么机器上


return的时候,系统帮你回收,这个不用担心,进程结束的时候,内存自动释放,当然,感谢您的提醒,我已把析构和构造函数补上的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-9 15:59:10 | 显示全部楼层
dolly_yos2 发表于 2023-1-9 13:50
我理解,我们的编译器大不相同,不过这些问题我能轻松解决
问题的核心在于您的实现。恕我直言,您没能发 ...

设计这方面,这是阉割版,我可能在删的时候没有删干净,不好意思,而且原来我是分文件编写的,有什么错误欢迎指出,目前已改,有没有达到您满意的需求呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 16:04:42 | 显示全部楼层
C丁洞杀O 发表于 2023-1-9 15:52
return的时候,系统帮你回收,这个不用担心,进程结束的时候,内存自动释放,当然,感谢您的提醒,我已 ...

这是有系统的时候,你不释放,系统会帮你释放
如果没有系统的时候,你不释放,谁帮你释放?
你以为C/C++就能写些操作系统上面运行的应用程序?
还是上面那句话,不要假设你的代码会在什么环境下运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 16:05:34 From FishC Mobile | 显示全部楼层
C丁洞杀O 发表于 2023-1-9 15:44
我这编译器一般默认会提供构造函数和解析函数啊?这不是正常的吗?而且我也说了这是低级C++,有些地方我 ...

是正常的,但是自定义的复制赋值操作符会阻止隐式提供的复制构造函数([class.copy.ctor])
语义上,自定义的复制赋值操作符意味着默认的无法满足需求,这通常也意味着复制构造函数同样需要自定义,析构也需要额外的(或特殊的)操作,比如保证所有权唯一性。这也就是 Rule of Three 背后的逻辑
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-9 16:10:05 | 显示全部楼层
C丁洞杀O 发表于 2023-1-9 15:59
设计这方面,这是阉割版,我可能在删的时候没有删干净,不好意思,而且原来我是分文件编写的,有什么错误 ...

哪有什么“要求”,就像我说的,设计上见仁见智,把模型设计的“显然没有问题”是极其困难的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-9 16:22:22 | 显示全部楼层
dolly_yos2 发表于 2023-1-9 16:05
是正常的,但是自定义的复制赋值操作符会阻止隐式提供的复制构造函数([class.copy.ctor])
语义上,自 ...

已经将复制构造函数禁止了,多谢提醒!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 19:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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