鱼C论坛

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

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

[复制链接]
发表于 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: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:50:44 | 显示全部楼层
C丁洞杀O 发表于 2023-1-9 15:45
Liunx版的?我是Windows上用的,可能会有差异吧

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

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

使用道具 举报

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-2 02:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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