鱼C论坛

 找回密码
 立即注册
查看: 4575|回复: 28

switch 语句报错

[复制链接]
发表于 2022-12-25 09:26:06 | 显示全部楼层 |阅读模式

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

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

x

我怎么也想不通,为什么老是报[Error] jump to case label?
  1. switch (...) {
  2.     case ...:
  3.         ...
  4.     ...
  5.     case 8: // Error
  6.         ...
  7.     case 9: // Error
  8.         ...
  9. }
复制代码

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

使用道具 举报

 楼主| 发表于 2022-12-25 09:27:53 | 显示全部楼层
实际上有511行的,我怕鱼C论坛炸掉
所以我就扔了个模型。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-25 09:39:32 | 显示全部楼层
现在变这样子了:
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe        C:\Users\hzk20\AppData\Local\Temp\ccruxmxX.o:口算.cpp:(.text+0x10ae): undefined reference to `read(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe        C:\Users\hzk20\AppData\Local\Temp\ccruxmxX.o:口??cpp:(.text+0x1131): undefined reference to `write(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
C:\Users\hzk20\Desktop\collect2.exe        [Error] ld returned 1 exit status
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-25 09:41:10 | 显示全部楼层
【Error】ld returned 1 exit status
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-25 09:42:45 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-25 10:20:04 | 显示全部楼层
你扔一个这模型说明不了问题
因为这个就是标准的switch case用法
这个模型是没问题的
对于这件事你自己是知道的吧
你扔一个switch case的用法上来做什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-25 10:26:53 | 显示全部楼层
没写break结束,还是已经有一个小黑窗在运行了,又运行同一个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-25 10:39:54 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-25 10:40:27 | 显示全部楼层
步入进去,这样更容易找到是哪里出了问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-25 19:22:06 | 显示全部楼层

源码【不许侵权】:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cerrno>

  5. using namespace std;

  6. typedef unsigned int uint, *puint;
  7. typedef const char cchar, *pcchar;

  8. #define randint(a, b) ((rand() % (b-a+1)) + a)
  9. #define MAX_LEVEL 15

  10. struct Account {
  11.         string name = "Player";
  12.         uint max_score = 0;
  13.         uint score = 0;
  14.         uint level = 0;
  15.         uint exp = 0;
  16.         long long atid = randint(100000000000000, 999999999999999);
  17. };

  18. bool add(uint a, uint b);
  19. bool sub(uint a, uint b);
  20. bool mcl(uint a, uint b);
  21. bool div(uint a, uint b);
  22. bool writeInfo(string file, Account *account);
  23. bool readInfo(string file, Account *account);

  24. int main(void) {
  25.         short int code;
  26.         int number, score, operatorNum;
  27.         Account *account;
  28.         bool savable = false;
  29.         string rfile, wfile;
  30.        
  31.         cout << "---欢迎来到口算小能手---" << endl;
  32.        
  33.         while (code != 0) {
  34.                 cout << "请选择模式:" << endl;
  35.                 cout << "1 -- 纯加法[+1-3]" << endl;
  36.                 cout << "2 -- 纯减法[+1-3]" << endl;
  37.                 cout << "3 -- 纯乘法[+2-4]" << endl;
  38.                 cout << "4 -- 纯除法[+2-4]" << endl;
  39.                 cout << "5 -- 混合运算[作对一题就+1]" << endl;
  40.                 cout << "6 -- 死亡算法[作对一题就+3]" << endl;
  41.                 cout << "7 -- 导入数据[+0]" << endl;
  42.                 cout << "8 -- 保存数据[+0]" << endl;
  43.                 cout << "9 -- 查看账户信息[+0]" << endl;
  44.                 cout << "0 -- Exit退出[+1]" << endl;
  45.                 cout << "注:200积分升一级,共15级qwq!" << endl;
  46.                 cout << "请输入指令代码:";
  47.                 cin >> code;

  48.                 if ((*account).exp >= 200 && (*account).level < 15) {
  49.                         (*account).exp -= 200;
  50.                         (*account).level += 1;
  51.                 }
  52.                
  53.                 switch (code) {
  54.                         case 0:
  55.                                 if ((*account).score > 0) {
  56.                                         if (savable) {
  57.                                                 exit(EXIT_SUCCESS);
  58.                                         }
  59.                                         else {
  60.                                                 char choose;
  61.                                                
  62.                                                 cout << "你还没有保存信息哦!" << endl;
  63.                                                 cout << "确定要退出吗?【N/y】";
  64.                                                 cin >> choose;
  65.                                                
  66.                                                 switch (choose) {
  67.                                                         case 'Y':
  68.                                                         case 'y':
  69.                                                                 exit(EXIT_SUCCESS);
  70.                                                                 break;
  71.                                                                
  72.                                                         case 'N':
  73.                                                         case 'n':
  74.                                                                 break;
  75.                                                 }
  76.                                         }
  77.                                 }
  78.                                 else {
  79.                                         cout << "别想钻空子!加分不是白加的!" << endl;
  80.                                 }
  81.                                 code = 10;
  82.                                 break;
  83.                                
  84.                         case 1:
  85.                                 cout << "请输入题目数:";
  86.                                 cin >> number;
  87.                                
  88.                                 for (int count = number;count > 0;count--) {
  89.                                         if (add(randint(100, 999), randint(100, 999))) {
  90.                                                 score++;
  91.                                         }
  92.                                 }
  93.                                
  94.                                 (*account).score += score;
  95.                                
  96.                                 if (score >= (*account).max_score) {
  97.                                         (*account).max_score = score;
  98.                                 }
  99.                                
  100.                                 if (score >= number/5) {
  101.                                         (*account).exp += 1;
  102.                                 }
  103.                                 else if (score >= number/3) {
  104.                                         (*account).exp += 2;
  105.                                 }
  106.                                 else if (score >= number/1) {
  107.                                         (*account).exp += 3;
  108.                                 }
  109.                                
  110.                                 score = 0;
  111.                                 break;
  112.                                
  113.                         case 2:
  114.                                 cout << "请输入题目数:";
  115.                                 cin >> number;
  116.                                
  117.                                 uint a, b;
  118.                                
  119.                                 while (true) {
  120.                                         a = randint(100, 999);
  121.                                         b = randint(100, 999);
  122.                                        
  123.                                         if (a >= b) {
  124.                                                 break;
  125.                                         }
  126.                                 }
  127.                                
  128.                                 for (int count = number;count > 0;count--) {
  129.                                         if (sub(a, b)) {
  130.                                                 score++;
  131.                                         }
  132.                                 }
  133.                                
  134.                                 (*account).score += score;
  135.                                
  136.                                 if (score >= (*account).max_score) {
  137.                                         (*account).max_score = score;
  138.                                 }
  139.                                
  140.                                 if (score >= number/5) {
  141.                                         (*account).exp += 1;
  142.                                 }
  143.                                 else if (score >= number/3) {
  144.                                         (*account).exp += 2;
  145.                                 }
  146.                                 else if (score >= number/1) {
  147.                                         (*account).exp += 3;
  148.                                 }
  149.                                
  150.                                 score = 0;
  151.                                 break;
  152.                        
  153.                         case 3:
  154.                                 cout << "请输入题目数:";
  155.                                 cin >> number;
  156.                                
  157.                                 for (int count = number;count > 0;count--) {
  158.                                         if (mcl(randint(100, 999), randint(100, 999))) {
  159.                                                 score++;
  160.                                         }
  161.                                 }
  162.                                
  163.                                 (*account).score += score;
  164.                                
  165.                                 if (score >= (*account).max_score) {
  166.                                         (*account).max_score = score;
  167.                                 }
  168.                                
  169.                                 if (score >= number/5) {
  170.                                         (*account).exp += 2;
  171.                                 }
  172.                                 else if (score >= number/3) {
  173.                                         (*account).exp += 3;
  174.                                 }
  175.                                 else if (score >= number/1) {
  176.                                         (*account).exp += 4;
  177.                                 }
  178.                                
  179.                                 score = 0;
  180.                                 break;
  181.                                
  182.                         case 4:
  183.                                 cout << "请输入题目数:";
  184.                                 cin >> number;
  185.                                
  186.                                 uint c, d;
  187.                                
  188.                                 while (true) {
  189.                                         c = randint(100, 999);
  190.                                         d = randint(100, 999);
  191.                                        
  192.                                         if (c % d == 0) {
  193.                                                 break;
  194.                                         }
  195.                                 }
  196.                                
  197.                                 for (int count = number;count > 0;count--) {
  198.                                         if (div(randint(c, d), randint(c, d))) {
  199.                                                 score++;
  200.                                         }
  201.                                 }
  202.                                
  203.                                 (*account).score += score;
  204.                                
  205.                                 if (score >= (*account).max_score) {
  206.                                         (*account).max_score = score;
  207.                                 }
  208.                                
  209.                                 if (score >= number/5) {
  210.                                         (*account).exp += 2;
  211.                                 }
  212.                                 else if (score >= number/3) {
  213.                                         (*account).exp += 3;
  214.                                 }
  215.                                 else if (score >= number/1) {
  216.                                         (*account).exp += 4;
  217.                                 }
  218.                                
  219.                                 score = 0;
  220.                                 break;
  221.                                
  222.                         case 5:
  223.                                 cout << "请输入题目数:";
  224.                                 cin >> number;
  225.                                
  226.                                 for (int count = number;count > 0;count--) {
  227.                                         operatorNum = randint(1, 4);
  228.                                         uint exp;
  229.                                        
  230.                                         switch (operatorNum) {
  231.                                                 case 1:
  232.                                                         if (add(randint(100, 999), randint(100, 999))) {
  233.                                                                 score++;
  234.                                                                 exp++;
  235.                                                         }
  236.                                                        
  237.                                                         break;
  238.                                                        
  239.                                                 case 2:
  240.                                                         uint a, b;
  241.                                                        
  242.                                                         while (true) {
  243.                                                                 a = randint(100, 999);
  244.                                                                 b = randint(100, 999);
  245.                                                                
  246.                                                                 if (a >= b) {
  247.                                                                         break;
  248.                                                                 }
  249.                                                         }
  250.                                                        
  251.                                                         if (sub(a, b)) {
  252.                                                                 score++;
  253.                                                                 exp++;
  254.                                                         }
  255.                                                        
  256.                                                         break;
  257.                                                        
  258.                                                 case 3:
  259.                                                         if (mcl(randint(100, 999), randint(100, 999))) {
  260.                                                                 score++;
  261.                                                                 exp++;
  262.                                                         }
  263.                                                        
  264.                                                         break;
  265.                                                        
  266.                                                 case 4:
  267.                                                         uint c, d;
  268.                                                        
  269.                                                         while (true) {
  270.                                                                 c = randint(100, 999);
  271.                                                                 d = randint(100, 999);
  272.                                                                
  273.                                                                 if (c % d == 0) {
  274.                                                                         break;
  275.                                                                 }
  276.                                                         }
  277.                                                        
  278.                                                         if (div(c, d)) {
  279.                                                                 score++;
  280.                                                                 exp++;
  281.                                                         }
  282.                                         }
  283.                                 }
  284.                                
  285.                                 (*account).score += score;
  286.                                
  287.                                 if (score >= (*account).max_score) {
  288.                                         (*account).max_score = score;
  289.                                 }
  290.                                
  291.                                 score = 0;
  292.                                 break;
  293.                                
  294.                         case 6:
  295.                                 cout << "请输入题目数:";
  296.                                 cin >> number;
  297.                                
  298.                                 for (int count = number;count > 0;count--) {
  299.                                         operatorNum = randint(1, 4);
  300.                                         uint exp;
  301.                                        
  302.                                         switch (operatorNum) {
  303.                                                 case 1:
  304.                                                         if (add(randint(10000, 99999), randint(10000, 99999))) {
  305.                                                                 score++;
  306.                                                                 exp += 3;
  307.                                                         }
  308.                                                        
  309.                                                         break;
  310.                                                        
  311.                                                 case 2:
  312.                                                         uint a, b;
  313.                                                        
  314.                                                         while (true) {
  315.                                                                 a = randint(10000, 99999);
  316.                                                                 b = randint(10000, 99999);
  317.                                                                
  318.                                                                 if (a >= b) {
  319.                                                                         break;
  320.                                                                 }
  321.                                                         }
  322.                                                        
  323.                                                         if (sub(a, b)) {
  324.                                                                 score++;
  325.                                                                 exp += 3;
  326.                                                         }
  327.                                                        
  328.                                                         break;
  329.                                                        
  330.                                                 case 3:
  331.                                                         if (mcl(randint(10000, 99999), randint(10000, 99999))) {
  332.                                                                 score++;
  333.                                                                 exp += 3;
  334.                                                         }
  335.                                                        
  336.                                                         break;
  337.                                                        
  338.                                                 case 4:
  339.                                                         uint e, f;
  340.                                                        
  341.                                                         while (true) {
  342.                                                                 e = randint(10000, 99999);
  343.                                                                 f = randint(10000, 99999);
  344.                                                                
  345.                                                                 if (e % f == 0) {
  346.                                                                         break;
  347.                                                                 }
  348.                                                         }
  349.                                                        
  350.                                                         if (div(e, f)) {
  351.                                                                 score++;
  352.                                                                 exp += 3;
  353.                                                         }
  354.                                         }
  355.                                 }
  356.                                
  357.                                 (*account).score += score;
  358.                                
  359.                                 if (score >= (*account).max_score) {
  360.                                         (*account).max_score = score;
  361.                                 }
  362.                                
  363.                                 score = 0;
  364.                                 break;
  365.                                
  366.                         case 7:
  367.                                 cout << "请输入文件路径:";
  368.                                 cin >> rfile;
  369.                                
  370.                                 if (readInfo(rfile.c_str(), account)) {
  371.                                         cout << "读取信息成功!" << endl;
  372.                                 }
  373.                                 else {
  374.                                         cout << "读取文件失败,请重试。" << endl;
  375.                                 }
  376.                                
  377.                                 break;
  378.                                
  379.                         case 8:
  380.                                 cout << "请输入要保存的路径:";
  381.                                 cin >> wfile;
  382.                                
  383.                                 if (writeInfo(wfile.c_str(), account)) {
  384.                                         cout << "保存文件成功!" << endl;
  385.                                         savable = true;
  386.                                 }
  387.                                 else {
  388.                                         cout << "保存文件失败,请重试。" << endl;
  389.                                 }
  390.                                
  391.                                 break;
  392.                                
  393.                         case 9:
  394.                                 cout << "Account Information:" << endl;
  395.                                 cout << "Name(姓名):" << (*account).name << endl;
  396.                                 cout << "Max-Score(最高分数):" << (*account).max_score << endl;
  397.                                 cout << "Score(累计分数):" << (*account).score << endl;
  398.                                 cout << "Level(等级):" << (*account).score << endl;
  399.                                 cout << "Exp(经验):" << (*account).score << endl;
  400.                                 cout << "ATID(账户ID):" << (*account).atid << endl;
  401.                                
  402.                                 break;
  403.                                
  404.                 }
  405.         }
  406.        
  407.         return 0;
  408. }

  409. bool add(uint a, uint b) {
  410.         uint answer = a + b;
  411.         uint userinput;
  412.        
  413.         cout << "%d + %d = " << a, b;
  414.         cin >> userinput;
  415.        
  416.         if (userinput == answer) {
  417.                 cout << "答对啦!" << endl;
  418.                 return true;
  419.         }
  420.         else {
  421.                 cout << "答错啦!" << endl;
  422.                 return false;
  423.         }
  424. }

  425. bool sub(uint a, uint b) {
  426.         uint answer = a - b;
  427.         uint userinput;
  428.        
  429.         cout << "%d - %d = " << a, b;
  430.         cin >> userinput;
  431.        
  432.         if (userinput == answer) {
  433.                 cout << "答对啦!" << endl;
  434.                 return true;
  435.         }
  436.         else {
  437.                 cout << "答错啦!" << endl;
  438.                 return false;
  439.         }
  440. }

  441. bool mcl(uint a, uint b) {
  442.         uint answer = a * b;
  443.         uint userinput;
  444.        
  445.         cout << "%d × %d = " << a, b;
  446.         cin >> userinput;
  447.        
  448.         if (userinput == answer) {
  449.                 cout << "答对啦!" << endl;
  450.                 return true;
  451.         }
  452.         else {
  453.                 cout << "答错啦!" << endl;
  454.                 return false;
  455.         }
  456. }

  457. bool div(uint a, uint b) {
  458.         uint answer = a / b;
  459.         uint userinput;
  460.        
  461.         cout << "%d ÷ %d = " << a, b;
  462.         cin >> userinput;
  463.        
  464.         if (userinput == answer) {
  465.                 cout << "答对啦!" << endl;
  466.                 return true;
  467.         }
  468.         else {
  469.                 cout << "答错啦!" << endl;
  470.                 return false;
  471.         }
  472. }

  473. bool writeInfo(pcchar file, Account *account) {
  474.         FILE *fp = NULL;
  475.        
  476.         if ((fp = fopen(file, "w")) == NULL) {
  477.                 cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
  478.                 return false;
  479.         }
  480.        
  481.         fwrite(account, sizeof(Account), 1, fp);
  482.         return true;
  483. }

  484. bool readInfo(pcchar file, Account *account) {
  485.         FILE *fp = NULL;
  486.         char choose;
  487.        
  488.         if ((fp = fopen(file, "r")) == NULL) {
  489.                 cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
  490.                 return false;
  491.         }
  492.        
  493.         cout << "确定覆盖现在的信息吗?【Y/n】";
  494.         cin >> choose;
  495.        
  496.         switch (choose) {
  497.                 case 'Y':
  498.                 case 'y':
  499.                         break;
  500.                 case 'N':
  501.                 case 'n':
  502.                         cout << "用户取消了操作。" << endl;
  503.                         return true;
  504.                        
  505.                 default:
  506.                         cout << "请认真输入!" << endl;
  507.                         return true;
  508.         }
  509.        
  510.         fread(account, sizeof(Account), 1, fp);
  511.         return true;
  512. }
复制代码


条款:
README.md(务必看完)


README.md.zip

424 Bytes, 下载次数: 12

条款

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2022-12-25 21:05:55 | 显示全部楼层
学编程最重要的一件事就是,千万不要看编译器的提示
不管是什么提示,只要是提示就一定不要看
对吧?
不至于吧?怎么能把代码写成这样呢?
先把编译器给出的警告解决了再说吧


  1. sh-5.1$ cat main.cpp
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cerrno>

  6. using namespace std;

  7. typedef unsigned int uint, *puint;
  8. typedef const char cchar, *pcchar;

  9. #define randint(a, b) ((rand() % (b-a+1)) + a)
  10. #define MAX_LEVEL 15

  11. struct Account {
  12.         string name = "Player";
  13.         uint max_score = 0;
  14.         uint score = 0;
  15.         uint level = 0;
  16.         uint exp = 0;
  17.         long long atid = randint(100000000000000, 999999999999999);
  18. };

  19. bool add(uint a, uint b);
  20. bool sub(uint a, uint b);
  21. bool mcl(uint a, uint b);
  22. bool div(uint a, uint b);
  23. bool writeInfo(string file, Account *account);
  24. bool readInfo(string file, Account *account);

  25. int main(void) {
  26.         //short int code;     // variable 'code' is used uninitialized whenever function 'main' is called (FixIt)
  27.         short int code = 0;
  28.         int number, score, operatorNum;
  29.         Account *account;
  30.         bool savable = false;
  31.         string rfile, wfile;

  32.         cout << "---欢迎来到口算小能手---" << endl;



  33.         account = NULL;     // line: 61



  34.         while (code != 0) {
  35.                 cout << "请选择模式:" << endl;
  36.                 cout << "1 -- 纯加法[+1-3]" << endl;
  37.                 cout << "2 -- 纯减法[+1-3]" << endl;
  38.                 cout << "3 -- 纯乘法[+2-4]" << endl;
  39.                 cout << "4 -- 纯除法[+2-4]" << endl;
  40.                 cout << "5 -- 混合运算[作对一题就+1]" << endl;
  41.                 cout << "6 -- 死亡算法[作对一题就+3]" << endl;
  42.                 cout << "7 -- 导入数据[+0]" << endl;
  43.                 cout << "8 -- 保存数据[+0]" << endl;
  44.                 cout << "9 -- 查看账户信息[+0]" << endl;
  45.                 cout << "0 -- Exit退出[+1]" << endl;
  46.                 cout << "注:200积分升一级,共15级qwq!" << endl;
  47.                 cout << "请输入指令代码:";
  48.                 cin >> code;
  49.                 if ((*account).exp >= 200 && (*account).level < 15) {   // variable 'account' is uninitialized when used here (FixIt)
  50.                         (*account).exp -= 200;
  51.                         (*account).level += 1;
  52.                 }

  53.                 switch (code) {
  54.                         case 0:
  55.                                 if ((*account).score > 0) {
  56.                                         if (savable) {
  57.                                                 exit(EXIT_SUCCESS);
  58.                                         }
  59.                                         else {
  60.                                                 char choose;

  61.                                                 cout << "你还没有保存信息哦!" << endl;
  62.                                                 cout << "确定要退出吗?【N/y】";
  63.                                                 cin >> choose;

  64.                                                 switch (choose) {
  65.                                                         case 'Y':
  66.                                                         case 'y':
  67.                                                                 exit(EXIT_SUCCESS);
  68.                                                                 break;

  69.                                                         case 'N':
  70.                                                         case 'n':
  71.                                                                 break;
  72.                                                 }
  73.                                         }
  74.                                 }
  75.                                 else {
  76.                                         cout << "别想钻空子!加分不是白加的!" << endl;
  77.                                 }
  78.                                 code = 10;
  79.                                 break;

  80.                         case 1:
  81.                                 cout << "请输入题目数:";
  82.                                 cin >> number;

  83.                                 for (int count = number;count > 0;count--) {
  84.                                         if (add(randint(100, 999), randint(100, 999))) {
  85.                                                 score++;
  86.                                         }
  87.                                 }

  88.                                 (*account).score += score;

  89.                                 if (score >= (*account).max_score) {
  90.                                         (*account).max_score = score;
  91.                                 }

  92.                                 if (score >= number/5) {
  93.                                         (*account).exp += 1;
  94.                                 }
  95.                                 else if (score >= number/3) {
  96.                                         (*account).exp += 2;
  97.                                 }
  98.                                 else if (score >= number/1) {
  99.                                         (*account).exp += 3;
  100.                                 }

  101.                                 score = 0;
  102.                                 break;

  103.                         case 2:
  104.                                 cout << "请输入题目数:";
  105.                                 cin >> number;

  106.                                 uint a, b;

  107.                                 while (true) {
  108.                                         a = randint(100, 999);
  109.                                         b = randint(100, 999);

  110.                                         if (a >= b) {
  111.                                                 break;
  112.                                         }
  113.                                 }

  114.                                 for (int count = number;count > 0;count--) {
  115.                                         if (sub(a, b)) {
  116.                                                 score++;
  117.                                         }
  118.                                 }

  119.                                 (*account).score += score;

  120.                                 if (score >= (*account).max_score) {
  121.                                         (*account).max_score = score;
  122.                                 }

  123.                                 if (score >= number/5) {
  124.                                         (*account).exp += 1;
  125.                                 }
  126.                                 else if (score >= number/3) {
  127.                                         (*account).exp += 2;
  128.                                 }
  129.                                 else if (score >= number/1) {
  130.                                         (*account).exp += 3;
  131.                                 }

  132.                                 score = 0;
  133.                                 break;

  134.                         case 3:
  135.                                 cout << "请输入题目数:";
  136.                                 cin >> number;

  137.                                 for (int count = number;count > 0;count--) {
  138.                                         if (mcl(randint(100, 999), randint(100, 999))) {
  139.                                                 score++;
  140.                                         }
  141.                                 }

  142.                                 (*account).score += score;

  143.                                 if (score >= (*account).max_score) {
  144.                                         (*account).max_score = score;
  145.                                 }

  146.                                 if (score >= number/5) {
  147.                                         (*account).exp += 2;
  148.                                 }
  149.                                 else if (score >= number/3) {
  150.                                         (*account).exp += 3;
  151.                                 }
  152.                                 else if (score >= number/1) {
  153.                                         (*account).exp += 4;
  154.                                 }

  155.                                 score = 0;
  156.                                 break;

  157.                         case 4:
  158.                                 cout << "请输入题目数:";
  159.                                 cin >> number;

  160.                                 uint c, d;

  161.                                 while (true) {
  162.                                         c = randint(100, 999);
  163.                                         d = randint(100, 999);

  164.                                         if (c % d == 0) {
  165.                                                 break;
  166.                                         }
  167.                                 }

  168.                                 for (int count = number;count > 0;count--) {
  169.                                         if (div(randint(c, d), randint(c, d))) {
  170.                                                 score++;
  171.                                         }
  172.                                 }

  173.                                 (*account).score += score;

  174.                                 if (score >= (*account).max_score) {
  175.                                         (*account).max_score = score;
  176.                                 }

  177.                                 if (score >= number/5) {
  178.                                         (*account).exp += 2;
  179.                                 }
  180.                                 else if (score >= number/3) {
  181.                                         (*account).exp += 3;
  182.                                 }
  183.                                 else if (score >= number/1) {
  184.                                         (*account).exp += 4;
  185.                                 }

  186.                                 score = 0;
  187.                                 break;

  188.                         case 5:
  189.                                 cout << "请输入题目数:";
  190.                                 cin >> number;

  191.                                 for (int count = number;count > 0;count--) {
  192.                                         operatorNum = randint(1, 4);
  193.                                         uint exp;

  194.                                         switch (operatorNum) {
  195.                                                 case 1:
  196.                                                         if (add(randint(100, 999), randint(100, 999))) {
  197.                                                                 score++;
  198.                                                                 // 至于吗?这么多?怎么写代码的?
  199.                                                                 exp++; // variable 'exp' is uninitialized when used here (FixIt)
  200.                                                         }

  201.                                                         break;

  202.                                                 case 2:
  203.                                                         uint a, b;

  204.                                                         while (true) {
  205.                                                                 a = randint(100, 999);
  206.                                                                 b = randint(100, 999);

  207.                                                                 if (a >= b) {
  208.                                                                         break;
  209.                                                                 }
  210.                                                         }

  211.                                                         if (sub(a, b)) {
  212.                                                                 score++;
  213.                                                                 exp++;
  214.                                                         }

  215.                                                         break;

  216.                                                 case 3:
  217.                                                         if (mcl(randint(100, 999), randint(100, 999))) {
  218.                                                                 score++;
  219.                                                                 exp++;
  220.                                                         }

  221.                                                         break;

  222.                                                 case 4:
  223.                                                         uint c, d;

  224.                                                         while (true) {
  225.                                                                 c = randint(100, 999);
  226.                                                                 d = randint(100, 999);

  227.                                                                 if (c % d == 0) {
  228.                                                                         break;
  229.                                                                 }
  230.                                                         }

  231.                                                         if (div(c, d)) {
  232.                                                                 score++;
  233.                                                                 exp++;
  234.                                                         }
  235.                                         }
  236.                                 }

  237.                                 (*account).score += score;

  238.                                 if (score >= (*account).max_score) {
  239.                                         (*account).max_score = score;
  240.                                 }

  241.                                 score = 0;
  242.                                 break;

  243.                         case 6:
  244.                                 cout << "请输入题目数:";
  245.                                 cin >> number;

  246.                                 for (int count = number;count > 0;count--) {
  247.                                         operatorNum = randint(1, 4);
  248.                                         // 说真的,这代码可以重写了
  249.                                         uint exp;   // variable 'exp' set but not used

  250.                                         switch (operatorNum) {
  251.                                                 case 1:
  252.                                                         if (add(randint(10000, 99999), randint(10000, 99999))) {
  253.                                                                 score++;
  254.                                                                 exp += 3;   // 我都懒得给你注释了
  255.                                                         }

  256.                                                         break;

  257.                                                 case 2:
  258.                                                         uint a, b;

  259.                                                         while (true) {
  260.                                                                 a = randint(10000, 99999);
  261.                                                                 b = randint(10000, 99999);

  262.                                                                 if (a >= b) {
  263.                                                                         break;
  264.                                                                 }
  265.                                                         }

  266.                                                         if (sub(a, b)) {
  267.                                                                 score++;
  268.                                                                 exp += 3;
  269.                                                         }

  270.                                                         break;

  271.                                                 case 3:
  272.                                                         if (mcl(randint(10000, 99999), randint(10000, 99999))) {
  273.                                                                 score++;
  274.                                                                 exp += 3;
  275.                                                         }

  276.                                                         break;

  277.                                                 case 4:
  278.                                                         uint e, f;

  279.                                                         while (true) {
  280.                                                                 e = randint(10000, 99999);
  281.                                                                 f = randint(10000, 99999);

  282.                                                                 if (e % f == 0) {
  283.                                                                         break;
  284.                                                                 }
  285.                                                         }

  286.                                                         if (div(e, f)) {
  287.                                                                 score++;
  288.                                                                 exp += 3;
  289.                                                         }
  290.                                         }
  291.                                 }

  292.                                 (*account).score += score;

  293.                                 if (score >= (*account).max_score) {
  294.                                         (*account).max_score = score;
  295.                                 }

  296.                                 score = 0;
  297.                                 break;

  298.                         case 7:
  299.                                 cout << "请输入文件路径:";
  300.                                 cin >> rfile;

  301.                                 if (readInfo(rfile.c_str(), account)) {
  302.                                         cout << "读取信息成功!" << endl;
  303.                                 }
  304.                                 else {
  305.                                         cout << "读取文件失败,请重试。" << endl;
  306.                                 }

  307.                                 break;

  308.                         case 8:
  309.                                 cout << "请输入要保存的路径:";
  310.                                 cin >> wfile;

  311.                                 if (writeInfo(wfile.c_str(), account)) {
  312.                                         cout << "保存文件成功!" << endl;
  313.                                         savable = true;
  314.                                 }
  315.                                 else {
  316.                                         cout << "保存文件失败,请重试。" << endl;
  317.                                 }

  318.                                 break;

  319.                         case 9:
  320.                                 cout << "Account Information:" << endl;
  321.                                 cout << "Name(姓名):" << (*account).name << endl;
  322.                                 cout << "Max-Score(最高分数):" << (*account).max_score << endl;
  323.                                 cout << "Score(累计分数):" << (*account).score << endl;
  324.                                 cout << "Level(等级):" << (*account).score << endl;
  325.                                 cout << "Exp(经验):" << (*account).score << endl;
  326.                                 cout << "ATID(账户ID):" << (*account).atid << endl;

  327.                                 break;

  328.                 }
  329.         }

  330.         return 0;
  331. }

  332. bool add(uint a, uint b) {
  333.         uint answer = a + b;
  334.         uint userinput;

  335.         cout << "%d + %d = " << a, b;
  336.         cin >> userinput;

  337.         if (userinput == answer) {
  338.                 cout << "答对啦!" << endl;
  339.                 return true;
  340.         }
  341.         else {
  342.                 cout << "答错啦!" << endl;
  343.                 return false;
  344.         }
  345. }

  346. bool sub(uint a, uint b) {
  347.         uint answer = a - b;
  348.         uint userinput;

  349.         cout << "%d - %d = " << a, b;
  350.         cin >> userinput;

  351.         if (userinput == answer) {
  352.                 cout << "答对啦!" << endl;
  353.                 return true;
  354.         }
  355.         else {
  356.                 cout << "答错啦!" << endl;
  357.                 return false;
  358.         }
  359. }

  360. bool mcl(uint a, uint b) {
  361.         uint answer = a * b;
  362.         uint userinput;

  363.         cout << "%d × %d = " << a, b;
  364.         cin >> userinput;

  365.         if (userinput == answer) {
  366.                 cout << "答对啦!" << endl;
  367.                 return true;
  368.         }
  369.         else {
  370.                 cout << "答错啦!" << endl;
  371.                 return false;
  372.         }
  373. }

  374. bool div(uint a, uint b) {
  375.         uint answer = a / b;
  376.         uint userinput;

  377.         cout << "%d ÷ %d = " << a, b;
  378.         cin >> userinput;

  379.         if (userinput == answer) {
  380.                 cout << "答对啦!" << endl;
  381.                 return true;
  382.         }
  383.         else {
  384.                 cout << "答错啦!" << endl;
  385.                 return false;
  386.         }
  387. }

  388. bool writeInfo(pcchar file, Account *account) {
  389.         FILE *fp = NULL;

  390.         if ((fp = fopen(file, "w")) == NULL) {
  391.                 cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
  392.                 return false;
  393.         }

  394.         fwrite(account, sizeof(Account), 1, fp);
  395.         return true;
  396. }

  397. bool readInfo(pcchar file, Account *account) {
  398.         FILE *fp = NULL;
  399.         char choose;

  400.         if ((fp = fopen(file, "r")) == NULL) {
  401.                 cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
  402.                 return false;
  403.         }

  404.         cout << "确定覆盖现在的信息吗?【Y/n】";
  405.         cin >> choose;

  406.         switch (choose) {
  407.                 case 'Y':
  408.                 case 'y':
  409.                         break;
  410.                 case 'N':
  411.                 case 'n':
  412.                         cout << "用户取消了操作。" << endl;
  413.                         return true;

  414.                 default:
  415.                         cout << "请认真输入!" << endl;
  416.                         return true;
  417.         }

  418.         fread(account, sizeof(Account), 1, fp);
  419.         return true;
  420. }
  421. sh-5.1$ g++ -g -Wall -o main main.cpp
  422. main.cpp: In function ‘int main()’:
  423. main.cpp:109:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
  424.   109 |                                 if (score >= (*account).max_score) {
  425.       |                                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
  426. main.cpp:149:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
  427.   149 |                                 if (score >= (*account).max_score) {
  428.       |                                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
  429. main.cpp:178:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
  430.   178 |                                 if (score >= (*account).max_score) {
  431.       |                                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
  432. main.cpp:218:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
  433.   218 |                                 if (score >= (*account).max_score) {
  434.       |                                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
  435. main.cpp:301:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
  436.   301 |                                 if (score >= (*account).max_score) {
  437.       |                                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
  438. main.cpp:374:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
  439.   374 |                                 if (score >= (*account).max_score) {
  440.       |                                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
  441. main.cpp: In function ‘bool add(uint, uint)’:
  442. main.cpp:429:37: warning: right operand of comma operator has no effect [-Wunused-value]
  443.   429 |         cout << "%d + %d = " << a, b;
  444.       |                                     ^
  445. main.cpp: In function ‘bool sub(uint, uint)’:
  446. main.cpp:446:37: warning: right operand of comma operator has no effect [-Wunused-value]
  447.   446 |         cout << "%d - %d = " << a, b;
  448.       |                                     ^
  449. main.cpp: In function ‘bool mcl(uint, uint)’:
  450. main.cpp:463:37: warning: right operand of comma operator has no effect [-Wunused-value]
  451.   463 |         cout << "%d × %d = " << a, b;
  452.       |                                     ^
  453. main.cpp: In function ‘bool div(uint, uint)’:
  454. main.cpp:480:37: warning: right operand of comma operator has no effect [-Wunused-value]
  455.   480 |         cout << "%d ÷ %d = " << a, b;
  456.       |                                     ^
  457. /usr/bin/ld: /tmp/ccd3nG4I.o: in function `main':
  458. /tmp/main.cpp:385: undefined reference to `readInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
  459. /usr/bin/ld: /tmp/main.cpp:398: undefined reference to `writeInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
  460. collect2: error: ld returned 1 exit status
  461. sh-5.1$ clang++ -g -Wall -o main main.cpp
  462. main.cpp:315:46: warning: variable 'exp' set but not used [-Wunused-but-set-variable]
  463.                                         uint exp;   // variable 'exp' set but not used
  464.                                              ^
  465. main.cpp:321:65: warning: variable 'exp' is uninitialized when used here [-Wuninitialized]
  466.                                                                 exp += 3;   // 我都懒得给你注释了
  467.                                                                 ^~~
  468. main.cpp:315:49: note: initialize the variable 'exp' to silence this warning
  469.                                         uint exp;   // variable 'exp' set but not used
  470.                                                 ^
  471.                                                  = 0
  472. main.cpp:248:65: warning: variable 'exp' is uninitialized when used here [-Wuninitialized]
  473.                                                                 exp++; // variable 'exp' is uninitialized when used here (FixIt)
  474.                                                                 ^~~
  475. main.cpp:241:49: note: initialize the variable 'exp' to silence this warning
  476.                                         uint exp;
  477.                                                 ^
  478.                                                  = 0
  479. main.cpp:429:36: warning: expression result unused [-Wunused-value]
  480.         cout << "%d + %d = " << a, b;
  481.                                    ^
  482. main.cpp:446:36: warning: expression result unused [-Wunused-value]
  483.         cout << "%d - %d = " << a, b;
  484.                                    ^
  485. main.cpp:463:37: warning: expression result unused [-Wunused-value]
  486.         cout << "%d × %d = " << a, b;
  487.                                    ^
  488. main.cpp:480:37: warning: expression result unused [-Wunused-value]
  489.         cout << "%d ÷ %d = " << a, b;
  490.                                    ^
  491. 7 warnings generated.
  492. /usr/bin/ld: /tmp/main-52f47d.o: in function `main':
  493. <unknown>:385: undefined reference to `readInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
  494. /usr/bin/ld: <unknown>:398: undefined reference to `writeInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
  495. clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
  496. sh-5.1$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-26 13:18:08 | 显示全部楼层
本帖最后由 小凯2013 于 2022-12-26 13:22 编辑


用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.34.31937 版
版权所有(C) Microsoft Corporation。保留所有权利。

口算.cpp
Microsoft (R) Incremental Linker Version 14.34.31937.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:口算.exe
口算.obj
口算.obj : error LNK2019: 无法解析的外部符号 "bool __cdecl writeInfo(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct Account *)" (?writeInfo@@YA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAUAccount@@@Z),函数 main 中引用了该符号
  已定义且可能匹配的符号上的提示:
    "bool __cdecl writeInfo(char const *,struct Account *)" (?writeInfo@@YA_NPEBDPEAUAccount@@@Z)
口算.obj : error LNK2019: 无法解析的外部符号 "bool __cdecl readInfo(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct Account *)" (?readInfo@@YA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAUAccount@@@Z),函数 main 中引用了该符号
  已定义且可能匹配的符号上的提示:
    "bool __cdecl readInfo(char const *,struct Account *)" (?readInfo@@YA_NPEBDPEAUAccount@@@Z)
口算.exe : fatal error LNK1120: 2 个无法解析的外部命令

怎么办???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-26 13:36:26 | 显示全部楼层
小凯2013 发表于 2022-12-26 13:18
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.34.31937 版
版权所有(C) Microsoft Corporation。保留 ...

先把上面的编译器警告全改了再说
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-26 14:53:27 | 显示全部楼层
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cerrno>

  6. // Informations: In README.md

  7. using namespace std;

  8. typedef unsigned int uint, *puint;
  9. typedef const char cchar, *pcchar;

  10. #define randint(a, b) ((rand() % (b-a+1)) + a)
  11. #define MAX_LEVEL 15

  12. struct Account {
  13.         string name = "Player";
  14.         uint max_score = 0;
  15.         uint score = 0;
  16.         uint level = 0;
  17.         uint exp = 0;
  18.         long long int atid = randint(100000000000000, 999999999999999);
  19. };

  20. bool add(uint a, uint b);
  21. bool sub(uint a, uint b);
  22. bool mcl(uint a, uint b);
  23. bool div(uint a, uint b);
  24. bool writeInfo(string file, Account *account);
  25. bool readInfo(string file, Account *account);

  26. int main(void) {
  27.         short int code = 9;
  28.         int number, score, operatorNum;
  29.         Account *account = (Account *)malloc(1024);
  30.         bool savable = false;
  31.         string rfile, wfile;
  32.        
  33.         (*account).name = "Player";
  34.         (*account).max_score = 0;
  35.         (*account).score = 0;
  36.         (*account).level = 0;
  37.         (*account).exp = 0;
  38.         (*account).atid = randint(100000000000000, 999999999999999);
  39.        
  40.         cout << "---欢迎来到口算小能手---" << endl;
  41.        
  42.         while (code != 0) {
  43.                 cout << "请选择模式:" << endl;
  44.                 cout << "1 -- 纯加法[+1-3]" << endl;
  45.                 cout << "2 -- 纯减法[+1-3]" << endl;
  46.                 cout << "3 -- 纯乘法[+2-4]" << endl;
  47.                 cout << "4 -- 纯除法[+2-4]" << endl;
  48.                 cout << "5 -- 混合运算[作对一题就+1]" << endl;
  49.                 cout << "6 -- 死亡算法[作对一题就+3]" << endl;
  50.                 cout << "7 -- 导入数据[+0]" << endl;
  51.                 cout << "8 -- 保存数据[+0]" << endl;
  52.                 cout << "9 -- 查看账户信息[+0]" << endl;
  53.                 cout << "0 -- Exit退出[+1]" << endl;
  54.                 cout << "注:200积分升一级,共15级qwq!" << endl;
  55.                 cout << "请输入指令代码:";
  56.                 cin >> code;

  57.                 if ((*account).exp >= 200 && (*account).level < 15) {
  58.                         (*account).exp -= 200;
  59.                         (*account).level += 1;
  60.                 }
  61.                
  62.                 switch (code) {
  63.                         case 0:
  64.                                 if ((*account).score > 0) {
  65.                                         if (savable) {
  66.                                                 exit(EXIT_SUCCESS);
  67.                                         }
  68.                                         else {
  69.                                                 char choose;
  70.                                                
  71.                                                 cout << "你还没有保存信息哦!" << endl;
  72.                                                 cout << "确定要退出吗?【N/y】";
  73.                                                 cin >> choose;
  74.                                                
  75.                                                 switch (choose) {
  76.                                                         case 'Y':
  77.                                                         case 'y':
  78.                                                                 exit(EXIT_SUCCESS);
  79.                                                                 break;
  80.                                                                
  81.                                                         case 'N':
  82.                                                         case 'n':
  83.                                                                 break;
  84.                                                 }
  85.                                         }
  86.                                 }
  87.                                 else {
  88.                                         cout << "别想钻空子!加分不是白加的!" << endl;
  89.                                 }
  90.                                 code = 10;
  91.                                 break;
  92.                                
  93.                         case 1:
  94.                                 cout << "请输入题目数:";
  95.                                 cin >> number;
  96.                                
  97.                                 for (int count = number;count > 0;count--) {
  98.                                         if (add(randint(100, 999), randint(100, 999))) {
  99.                                                 score++;
  100.                                         }
  101.                                 }
  102.                                
  103.                                 (*account).score += score;
  104.                                
  105.                                 if (score >= (*account).max_score) {
  106.                                         (*account).max_score = score;
  107.                                 }
  108.                                
  109.                                 if (score >= number/5) {
  110.                                         (*account).exp += 1;
  111.                                 }
  112.                                 else if (score >= number/3) {
  113.                                         (*account).exp += 2;
  114.                                 }
  115.                                 else if (score >= number/1) {
  116.                                         (*account).exp += 3;
  117.                                 }
  118.                                
  119.                                 score = 0;
  120.                                 break;
  121.                                
  122.                         case 2:
  123.                                 cout << "请输入题目数:";
  124.                                 cin >> number;
  125.                                
  126.                                 uint a, b;
  127.                                
  128.                                 while (true) {
  129.                                         a = randint(100, 999);
  130.                                         b = randint(100, 999);
  131.                                        
  132.                                         if (a >= b) {
  133.                                                 break;
  134.                                         }
  135.                                 }
  136.                                
  137.                                 for (int count = number;count > 0;count--) {
  138.                                         if (sub(a, b)) {
  139.                                                 score++;
  140.                                         }
  141.                                 }
  142.                                
  143.                                 (*account).score += score;
  144.                                
  145.                                 if (score >= (*account).max_score) {
  146.                                         (*account).max_score = score;
  147.                                 }
  148.                                
  149.                                 if (score >= number/5) {
  150.                                         (*account).exp += 1;
  151.                                 }
  152.                                 else if (score >= number/3) {
  153.                                         (*account).exp += 2;
  154.                                 }
  155.                                 else if (score >= number/1) {
  156.                                         (*account).exp += 3;
  157.                                 }
  158.                                
  159.                                 score = 0;
  160.                                 break;
  161.                        
  162.                         case 3:
  163.                                 cout << "请输入题目数:";
  164.                                 cin >> number;
  165.                                
  166.                                 for (int count = number;count > 0;count--) {
  167.                                         if (mcl(randint(100, 999), randint(100, 999))) {
  168.                                                 score++;
  169.                                         }
  170.                                 }
  171.                                
  172.                                 (*account).score += score;
  173.                                
  174.                                 if (score >= (*account).max_score) {
  175.                                         (*account).max_score = score;
  176.                                 }
  177.                                
  178.                                 if (score >= number/5) {
  179.                                         (*account).exp += 2;
  180.                                 }
  181.                                 else if (score >= number/3) {
  182.                                         (*account).exp += 3;
  183.                                 }
  184.                                 else if (score >= number/1) {
  185.                                         (*account).exp += 4;
  186.                                 }
  187.                                
  188.                                 score = 0;
  189.                                 break;
  190.                                
  191.                         case 4:
  192.                                 cout << "请输入题目数:";
  193.                                 cin >> number;
  194.                                
  195.                                 uint c, d;
  196.                                
  197.                                 while (true) {
  198.                                         c = randint(100, 999);
  199.                                         d = randint(100, 999);
  200.                                        
  201.                                         if (c % d == 0) {
  202.                                                 break;
  203.                                         }
  204.                                 }
  205.                                
  206.                                 for (int count = number;count > 0;count--) {
  207.                                         if (div(randint(c, d), randint(c, d))) {
  208.                                                 score++;
  209.                                         }
  210.                                 }
  211.                                
  212.                                 (*account).score += score;
  213.                                
  214.                                 if (score >= (*account).max_score) {
  215.                                         (*account).max_score = score;
  216.                                 }
  217.                                
  218.                                 if (score >= number/5) {
  219.                                         (*account).exp += 2;
  220.                                 }
  221.                                 else if (score >= number/3) {
  222.                                         (*account).exp += 3;
  223.                                 }
  224.                                 else if (score >= number/1) {
  225.                                         (*account).exp += 4;
  226.                                 }
  227.                                
  228.                                 score = 0;
  229.                                 break;
  230.                                
  231.                         case 5:
  232.                                 cout << "请输入题目数:";
  233.                                 cin >> number;
  234.                                
  235.                                 for (int count = number;count > 0;count--) {
  236.                                         operatorNum = randint(1, 4);
  237.                                         uint exp;
  238.                                        
  239.                                         switch (operatorNum) {
  240.                                                 case 1:
  241.                                                         if (add(randint(100, 999), randint(100, 999))) {
  242.                                                                 score++;
  243.                                                                 exp++;
  244.                                                         }
  245.                                                        
  246.                                                         break;
  247.                                                        
  248.                                                 case 2:
  249.                                                         uint a, b;
  250.                                                        
  251.                                                         while (true) {
  252.                                                                 a = randint(100, 999);
  253.                                                                 b = randint(100, 999);
  254.                                                                
  255.                                                                 if (a >= b) {
  256.                                                                         break;
  257.                                                                 }
  258.                                                         }
  259.                                                        
  260.                                                         if (sub(a, b)) {
  261.                                                                 score++;
  262.                                                                 exp++;
  263.                                                         }
  264.                                                        
  265.                                                         break;
  266.                                                        
  267.                                                 case 3:
  268.                                                         if (mcl(randint(100, 999), randint(100, 999))) {
  269.                                                                 score++;
  270.                                                                 exp++;
  271.                                                         }
  272.                                                        
  273.                                                         break;
  274.                                                        
  275.                                                 case 4:
  276.                                                         uint c, d;
  277.                                                        
  278.                                                         while (true) {
  279.                                                                 c = randint(100, 999);
  280.                                                                 d = randint(100, 999);
  281.                                                                
  282.                                                                 if (c % d == 0) {
  283.                                                                         break;
  284.                                                                 }
  285.                                                         }
  286.                                                        
  287.                                                         if (div(c, d)) {
  288.                                                                 score++;
  289.                                                                 exp++;
  290.                                                         }
  291.                                         }
  292.                                 }
  293.                                
  294.                                 (*account).score += score;
  295.                                
  296.                                 if (score >= (*account).max_score) {
  297.                                         (*account).max_score = score;
  298.                                 }
  299.                                
  300.                                 score = 0;
  301.                                 break;
  302.                                
  303.                         case 6:
  304.                                 cout << "请输入题目数:";
  305.                                 cin >> number;
  306.                                
  307.                                 for (int count = number;count > 0;count--) {
  308.                                         operatorNum = randint(1, 4);
  309.                                         uint exp;
  310.                                        
  311.                                         switch (operatorNum) {
  312.                                                 case 1:
  313.                                                         if (add(randint(10000, 99999), randint(10000, 99999))) {
  314.                                                                 score++;
  315.                                                                 exp += 3;
  316.                                                         }
  317.                                                        
  318.                                                         break;
  319.                                                        
  320.                                                 case 2:
  321.                                                         uint a, b;
  322.                                                        
  323.                                                         while (true) {
  324.                                                                 a = randint(10000, 99999);
  325.                                                                 b = randint(10000, 99999);
  326.                                                                
  327.                                                                 if (a >= b) {
  328.                                                                         break;
  329.                                                                 }
  330.                                                         }
  331.                                                        
  332.                                                         if (sub(a, b)) {
  333.                                                                 score++;
  334.                                                                 exp += 3;
  335.                                                         }
  336.                                                        
  337.                                                         break;
  338.                                                        
  339.                                                 case 3:
  340.                                                         if (mcl(randint(10000, 99999), randint(10000, 99999))) {
  341.                                                                 score++;
  342.                                                                 exp += 3;
  343.                                                         }
  344.                                                        
  345.                                                         break;
  346.                                                        
  347.                                                 case 4:
  348.                                                         uint e, f;
  349.                                                        
  350.                                                         while (true) {
  351.                                                                 e = randint(10000, 99999);
  352.                                                                 f = randint(10000, 99999);
  353.                                                                
  354.                                                                 if (e % f == 0) {
  355.                                                                         break;
  356.                                                                 }
  357.                                                         }
  358.                                                        
  359.                                                         if (div(e, f)) {
  360.                                                                 score++;
  361.                                                                 exp += 3;
  362.                                                         }
  363.                                         }
  364.                                 }
  365.                                
  366.                                 (*account).score += score;
  367.                                
  368.                                 if (score >= (*account).max_score) {
  369.                                         (*account).max_score = score;
  370.                                 }
  371.                                
  372.                                 score = 0;
  373.                                 break;
  374.                                
  375.                         case 7:
  376.                                 cout << "请输入文件路径:";
  377.                                 cin >> rfile;
  378.                                
  379.                                 if (readInfo(rfile.c_str(), account)) {
  380.                                         cout << "读取信息成功!" << endl;
  381.                                 }
  382.                                 else {
  383.                                         cout << "读取文件失败,请重试。" << endl;
  384.                                 }
  385.                                
  386.                                 break;
  387.                                
  388.                         case 8:
  389.                                 cout << "请输入要保存的路径:";
  390.                                 cin >> wfile;
  391.                                
  392.                                 if (writeInfo(wfile.c_str(), account)) {
  393.                                         cout << "保存文件成功!" << endl;
  394.                                         savable = true;
  395.                                 }
  396.                                 else {
  397.                                         cout << "保存文件失败,请重试。" << endl;
  398.                                 }
  399.                                
  400.                                 break;
  401.                                
  402.                         case 9:
  403.                                 cout << "Account Information:" << endl;
  404.                                 cout << "Name(姓名):" << (*account).name << endl;
  405.                                 cout << "Max-Score(最高分数):" << (*account).max_score << endl;
  406.                                 cout << "Score(累计分数):" << (*account).score << endl;
  407.                                 cout << "Level(等级):" << (*account).score << endl;
  408.                                 cout << "Exp(经验):" << (*account).score << endl;
  409.                                 cout << "ATID(账户ID):" << (*account).atid << endl;
  410.                                
  411.                                 break;
  412.                                
  413.                 }
  414.         }
  415.        
  416.         return 0;
  417. }

  418. bool add(uint a, uint b) {
  419.         uint answer = a + b;
  420.         uint userinput;
  421.        
  422.         cout << "%d + %d = " << a, b;
  423.         cin >> userinput;
  424.        
  425.         if (userinput == answer) {
  426.                 cout << "答对啦!" << endl;
  427.                 return true;
  428.         }
  429.         else {
  430.                 cout << "答错啦!" << endl;
  431.                 return false;
  432.         }
  433. }

  434. bool sub(uint a, uint b) {
  435.         uint answer = a - b;
  436.         uint userinput;
  437.        
  438.         cout << "%d - %d = " << a, b;
  439.         cin >> userinput;
  440.        
  441.         if (userinput == answer) {
  442.                 cout << "答对啦!" << endl;
  443.                 return true;
  444.         }
  445.         else {
  446.                 cout << "答错啦!" << endl;
  447.                 return false;
  448.         }
  449. }

  450. bool mcl(uint a, uint b) {
  451.         uint answer = a * b;
  452.         uint userinput;
  453.        
  454.         cout << "%d × %d = " << a, b;
  455.         cin >> userinput;
  456.        
  457.         if (userinput == answer) {
  458.                 cout << "答对啦!" << endl;
  459.                 return true;
  460.         }
  461.         else {
  462.                 cout << "答错啦!" << endl;
  463.                 return false;
  464.         }
  465. }

  466. bool div(uint a, uint b) {
  467.         uint answer = a / b;
  468.         uint userinput;
  469.        
  470.         cout << "%d ÷ %d = " << a, b;
  471.         cin >> userinput;
  472.        
  473.         if (userinput == answer) {
  474.                 cout << "答对啦!" << endl;
  475.                 return true;
  476.         }
  477.         else {
  478.                 cout << "答错啦!" << endl;
  479.                 return false;
  480.         }
  481. }

  482. bool writeInfo(pcchar file, Account *account) {
  483.         FILE *fp = NULL;
  484.        
  485.         if ((fp = fopen(file, "w")) == NULL) {
  486.                 cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
  487.                 return false;
  488.         }
  489.        
  490.         fwrite(account, sizeof(Account), 1, fp);
  491.         return true;
  492. }

  493. bool readInfo(pcchar file, Account *account) {
  494.         FILE *fp = NULL;
  495.         char choose;
  496.        
  497.         if ((fp = fopen(file, "r")) == NULL) {
  498.                 cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
  499.                 return false;
  500.         }
  501.        
  502.         cout << "确定覆盖现在的信息吗?【Y/n】";
  503.         cin >> choose;
  504.        
  505.         switch (choose) {
  506.                 case 'Y':
  507.                 case 'y':
  508.                         break;
  509.                 case 'N':
  510.                 case 'n':
  511.                         cout << "用户取消了操作。" << endl;
  512.                         return true;
  513.                        
  514.                 default:
  515.                         cout << "请认真输入!" << endl;
  516.                         return true;
  517.         }
  518.        
  519.         fread(account, sizeof(Account), 1, fp);
  520.         return true;
  521. }
复制代码


编译结果:
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe        C:\Users\hzk20\AppData\Local\Temp\cc05c8yf.o:口算.cpp:(.text+0x11b9): undefined reference to `readInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'

C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe        C:\Users\hzk20\AppData\Local\Temp\cc05c8yf.o:口算.cpp:(.text+0x128b): undefined reference to `writeInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'

C:\Users\hzk20\Desktop\collect2.exe        [Error] ld returned 1 exit status
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-26 15:50:10 | 显示全部楼层
.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-26 20:35:31 | 显示全部楼层
什么玩意还不能私自编译

不能私自编译还发出来

直接去问某一个人得了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-12-28 08:51:41 | 显示全部楼层
我怎么看都没发现问题,给这个代码做了个语法检查(没编译),没有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-28 09:18:47 | 显示全部楼层
zhangjinxuan 发表于 2022-12-28 08:51
我怎么看都没发现问题,给这个代码做了个语法检查(没编译),没有问题

cout << "%d ÷ %d = " << a, b;    // 这是什么意思呢?我表示看不懂
他代码里面随处可见的都是类似的低级错误,而且就这破玩意,还不让别人编译
他把这代码当宝贝,在别人看来,这破玩意有什么用,谁稀罕要呢
他这哪是在提问题呀

你的语法检查没有检查出问题吗?
把你的语法检查的警告等级开到最大,然后再看看

1.png
2.png
3.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-12-28 09:37:53 | 显示全部楼层
zhangjinxuan 发表于 2022-12-28 08:51
我怎么看都没发现问题,给这个代码做了个语法检查(没编译),没有问题

我又稍微看了一下,我就不知道他这代码怎么写出来的

你看他的 readInfo 函数
31 行声明的 bool readInfo(string file, Account *account);
507 行定义的   bool readInfo(pcchar file, Account *account)

还有36行的这个 Account *account = (Account *)malloc(1024);
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-12-28 09:41:39 | 显示全部楼层
人造人 发表于 2022-12-28 09:37
我又稍微看了一下,我就不知道他这代码怎么写出来的

你看他的 readInfo 函数

我也觉得很奇怪
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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