鱼C论坛

 找回密码
 立即注册
查看: 1416|回复: 8

[已解决]24点的题目,答案错了百分之50,但实在不知道错哪了,下面是代码

[复制链接]
发表于 2023-6-13 15:38:28 | 显示全部楼层 |阅读模式
10鱼币
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <iostream>

  6. using namespace std;

  7. typedef struct bitnode
  8. {
  9.     char data[10];
  10.     struct bitnode *lc,*rc;
  11. }bitnode, *bitree;

  12. void create(bitree &t)
  13. {
  14.     char c[10];
  15.     cin>>c;
  16.     if(c[0]=='#')
  17.     {
  18.         t=NULL;
  19.     }
  20.     else
  21.     {
  22.         t=(bitnode *)malloc(sizeof(bitnode));
  23.         strcpy(t->data,c);
  24.         create(t->lc);
  25.         create(t->rc);
  26.     }
  27. }

  28. void inordertraverse(bitree t)
  29. {
  30.     if(t)
  31.     {
  32.         inordertraverse(t->lc);
  33.         printf("%s ",t->data);
  34.         inordertraverse(t->rc);
  35.     }
  36. }

  37. void print(bitree &t)
  38. {
  39.     if(t)
  40.     {
  41.         if((t->data[0]=='+')||(t->data[0]=='-')||(t->data[0]=='*')||(t->data[0]=='/'))
  42.         {
  43.             printf("%s","(");
  44.             print(t->lc);
  45.             printf("%s",t->data);
  46.             print(t->rc);
  47.             printf("%s",")");
  48.         }
  49.         else
  50.         {
  51.             printf("%s",t->data);
  52.         }
  53.     }
  54. }

  55. int cal(bitree &t)
  56. {
  57.     double result=0;
  58.     int i=0;
  59.     int len=0;
  60.     if(t)
  61.     {
  62.         if(t->data[0]=='+')
  63.         {
  64.             return cal(t->lc)+cal(t->rc);
  65.         }
  66.         else if(t->data[0]=='-')
  67.         {
  68.             return cal(t->lc)-cal(t->rc);
  69.         }
  70.         else if(t->data[0]=='*')
  71.         {
  72.             return cal(t->lc)*cal(t->rc);
  73.         }
  74.         else if(t->data[0]=='/')
  75.         {
  76.             if(cal(t->rc)!=0)
  77.             {
  78.                 return cal(t->lc)/cal(t->rc);
  79.             }
  80.         }
  81.         else
  82.         {
  83.                         len=strlen(t->data);
  84.                         for(i=0;i<=len-1;i++){
  85.                                 if(t->data[i]>='0'&&t->data[i]<='9')
  86.                 {
  87.                                         result=result*10.0+t->data[i]-'0'+0.0;
  88.                                 }
  89.                         }
  90.                         return result;
  91.         }
  92.     }
  93. }

  94. int main(void)
  95. {
  96.     char c[10];
  97.     bitree t;
  98.     while(cin>>c)
  99.     {
  100.         t=(bitnode *)malloc(sizeof(bitnode));
  101.         strcpy(t->data,c);
  102.         create(t->lc);
  103.         create(t->rc);
  104.         if(fabs(cal(t)-24)<0.00001)
  105.         {
  106.             print(t);
  107.             printf("%s","=");
  108.             cout<<cal(t);
  109.         }
  110.         else
  111.         {
  112.             printf("%s","NO");
  113.         }
  114.         printf("\n");
  115.     }

  116.     return 0;
  117. }
复制代码
最佳答案
2023-6-13 15:38:29
这道题目可以使用递归的思路,递归地生成二叉树,并计算出表达式的值。对生成的二叉树进行中序遍历,将符合题目要求的输出出来,否则输出 NO。

现在来看代码中存在的问题:

1. 在程序执行完打印相应信息后,没有释放生成树节点所占用的动态内存;

2. 代码中如果遇到除数为零且无法进行运算的情况,则没有进行任何处理,导致程序的计算结果不稳定。

下面是修改过后的代码,仅供参考:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <iostream>

  6. using namespace std;

  7. typedef struct bitnode
  8. {
  9.     char data[10];
  10.     struct bitnode *lc, *rc;
  11. } bitnode, *bitree;

  12. void create(bitree& t)
  13. {
  14.     char c[10];
  15.     cin >> c;
  16.     if (c[0] == '#')
  17.     {
  18.         t = NULL;
  19.     }
  20.     else
  21.     {
  22.         t = (bitnode*)malloc(sizeof(bitnode));
  23.         strcpy(t->data, c);
  24.         create(t->lc);
  25.         create(t->rc);
  26.     }
  27. }

  28. void inordertraverse(bitree t)
  29. {
  30.     if (t)
  31.     {
  32.         inordertraverse(t->lc);
  33.         printf("%s ", t->data);
  34.         inordertraverse(t->rc);
  35.     }
  36. }

  37. void print(bitree& t)
  38. {
  39.     if (t)
  40.     {
  41.         if ((t->data[0] == '+') || (t->data[0] == '-') || (t->data[0] == '*') || (t->data[0] == '/'))
  42.         {
  43.             printf("%s", "(");
  44.             print(t->lc);
  45.             printf("%s", t->data);
  46.             print(t->rc);
  47.             printf("%s", ")");
  48.         }
  49.         else
  50.         {
  51.             printf("%s", t->data);
  52.         }
  53.     }
  54. }

  55. double cal(bitree& t)
  56. {
  57.     double result = 0;
  58.     int i = 0;
  59.     int len = 0;
  60.     if (t)
  61.     {
  62.         if (t->data[0] == '+')
  63.         {
  64.             return cal(t->lc) + cal(t->rc);
  65.         }
  66.         else if (t->data[0] == '-')
  67.         {
  68.             return cal(t->lc) - cal(t->rc);
  69.         }
  70.         else if (t->data[0] == '*')
  71.         {
  72.             return cal(t->lc) * cal(t->rc);
  73.         }
  74.         else if (t->data[0] == '/')
  75.         {
  76.             double rc = cal(t->rc);
  77.             if (rc != 0)
  78.             {
  79.                 return cal(t->lc) / cal(t->rc);
  80.             }
  81.             else
  82.             {
  83.                 return 0;
  84.             }
  85.         }
  86.         else
  87.         {
  88.             len = strlen(t->data);
  89.             for (i = 0; i <= len - 1; i++) {
  90.                 if (t->data[i] >= '0' && t->data[i] <= '9')
  91.                 {
  92.                     result = result * 10.0 + t->data[i] - '0' + 0.0;
  93.                 }
  94.             }
  95.             return result;
  96.         }
  97.     }
  98.     else
  99.     {
  100.         return 0;
  101.     }
  102. }

  103. void destory(bitree& t)
  104. {
  105.     if (t)
  106.     {
  107.         destory(t->lc);
  108.         destory(t->rc);
  109.         free(t);
  110.     }
  111. }

  112. int main(void)
  113. {
  114.     char c[10];
  115.     bitree t;
  116.     while (cin >> c)
  117.     {
  118.         t = (bitnode*)malloc(sizeof(bitnode));
  119.         strcpy(t->data, c);
  120.         create(t->lc);
  121.         create(t->rc);
  122.         if (fabs(cal(t) - 24) < 0.00001)
  123.         {
  124.             print(t);
  125.             printf("%s", "=");
  126.             cout << cal(t);
  127.         }
  128.         else
  129.         {
  130.             printf("%s", "NO");
  131.         }
  132.         destory(t);//释放创建计算树时所有节点占用的内存
  133.         printf("\n");
  134.     }

  135.     return 0;
  136. }
复制代码


希望对您有所帮助。

最佳答案

查看完整内容

这道题目可以使用递归的思路,递归地生成二叉树,并计算出表达式的值。对生成的二叉树进行中序遍历,将符合题目要求的输出出来,否则输出 NO。 现在来看代码中存在的问题: 1. 在程序执行完打印相应信息后,没有释放生成树节点所占用的动态内存; 2. 代码中如果遇到除数为零且无法进行运算的情况,则没有进行任何处理,导致程序的计算结果不稳定。 下面是修改过后的代码,仅供参考: 希望对您有所帮助。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-13 15:38:29 | 显示全部楼层    本楼为最佳答案   
这道题目可以使用递归的思路,递归地生成二叉树,并计算出表达式的值。对生成的二叉树进行中序遍历,将符合题目要求的输出出来,否则输出 NO。

现在来看代码中存在的问题:

1. 在程序执行完打印相应信息后,没有释放生成树节点所占用的动态内存;

2. 代码中如果遇到除数为零且无法进行运算的情况,则没有进行任何处理,导致程序的计算结果不稳定。

下面是修改过后的代码,仅供参考:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <iostream>

  6. using namespace std;

  7. typedef struct bitnode
  8. {
  9.     char data[10];
  10.     struct bitnode *lc, *rc;
  11. } bitnode, *bitree;

  12. void create(bitree& t)
  13. {
  14.     char c[10];
  15.     cin >> c;
  16.     if (c[0] == '#')
  17.     {
  18.         t = NULL;
  19.     }
  20.     else
  21.     {
  22.         t = (bitnode*)malloc(sizeof(bitnode));
  23.         strcpy(t->data, c);
  24.         create(t->lc);
  25.         create(t->rc);
  26.     }
  27. }

  28. void inordertraverse(bitree t)
  29. {
  30.     if (t)
  31.     {
  32.         inordertraverse(t->lc);
  33.         printf("%s ", t->data);
  34.         inordertraverse(t->rc);
  35.     }
  36. }

  37. void print(bitree& t)
  38. {
  39.     if (t)
  40.     {
  41.         if ((t->data[0] == '+') || (t->data[0] == '-') || (t->data[0] == '*') || (t->data[0] == '/'))
  42.         {
  43.             printf("%s", "(");
  44.             print(t->lc);
  45.             printf("%s", t->data);
  46.             print(t->rc);
  47.             printf("%s", ")");
  48.         }
  49.         else
  50.         {
  51.             printf("%s", t->data);
  52.         }
  53.     }
  54. }

  55. double cal(bitree& t)
  56. {
  57.     double result = 0;
  58.     int i = 0;
  59.     int len = 0;
  60.     if (t)
  61.     {
  62.         if (t->data[0] == '+')
  63.         {
  64.             return cal(t->lc) + cal(t->rc);
  65.         }
  66.         else if (t->data[0] == '-')
  67.         {
  68.             return cal(t->lc) - cal(t->rc);
  69.         }
  70.         else if (t->data[0] == '*')
  71.         {
  72.             return cal(t->lc) * cal(t->rc);
  73.         }
  74.         else if (t->data[0] == '/')
  75.         {
  76.             double rc = cal(t->rc);
  77.             if (rc != 0)
  78.             {
  79.                 return cal(t->lc) / cal(t->rc);
  80.             }
  81.             else
  82.             {
  83.                 return 0;
  84.             }
  85.         }
  86.         else
  87.         {
  88.             len = strlen(t->data);
  89.             for (i = 0; i <= len - 1; i++) {
  90.                 if (t->data[i] >= '0' && t->data[i] <= '9')
  91.                 {
  92.                     result = result * 10.0 + t->data[i] - '0' + 0.0;
  93.                 }
  94.             }
  95.             return result;
  96.         }
  97.     }
  98.     else
  99.     {
  100.         return 0;
  101.     }
  102. }

  103. void destory(bitree& t)
  104. {
  105.     if (t)
  106.     {
  107.         destory(t->lc);
  108.         destory(t->rc);
  109.         free(t);
  110.     }
  111. }

  112. int main(void)
  113. {
  114.     char c[10];
  115.     bitree t;
  116.     while (cin >> c)
  117.     {
  118.         t = (bitnode*)malloc(sizeof(bitnode));
  119.         strcpy(t->data, c);
  120.         create(t->lc);
  121.         create(t->rc);
  122.         if (fabs(cal(t) - 24) < 0.00001)
  123.         {
  124.             print(t);
  125.             printf("%s", "=");
  126.             cout << cal(t);
  127.         }
  128.         else
  129.         {
  130.             printf("%s", "NO");
  131.         }
  132.         destory(t);//释放创建计算树时所有节点占用的内存
  133.         printf("\n");
  134.     }

  135.     return 0;
  136. }
复制代码


希望对您有所帮助。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-14 18:47:40 | 显示全部楼层
sfqxx 发表于 2023-6-14 16:39
这道题目可以使用递归的思路,递归地生成二叉树,并计算出表达式的值。对生成的二叉树进行中序遍历,将符合 ...


自愧弗如

(bushi抵制gpt么)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-14 18:49:19 | 显示全部楼层
zhangjinxuan 发表于 2023-6-14 18:47
自愧弗如

(bushi抵制gpt么)

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

使用道具 举报

发表于 2023-6-14 18:50:07 | 显示全部楼层
zhangjinxuan 发表于 2023-6-14 18:47
自愧弗如

(bushi抵制gpt么)

你那个设置楼层奖励有问题,应该是2#

还有,能不能正常一点?7:05吧。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-14 18:53:30 | 显示全部楼层
sfqxx 发表于 2023-6-14 18:50
你那个设置楼层奖励有问题,应该是2#

还有,能不能正常一点?7:05吧。

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

使用道具 举报

发表于 2023-6-14 19:00:02 | 显示全部楼层
sfqxx 发表于 2023-6-14 18:50
你那个设置楼层奖励有问题,应该是2#

还有,能不能正常一点?7:05吧。

7:05谁都能起得来

这样,你在深夜的时候这么说:

你:啊,我作业没做!
家长:什么?现在去补!
你:啊,今天太晚了,明天 5 点起补作业吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-14 19:49:56 | 显示全部楼层
zhangjinxuan 发表于 2023-6-14 19:00
7:05谁都能起得来

这样,你在深夜的时候这么说:

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

使用道具 举报

发表于 2023-6-14 20:27:22 | 显示全部楼层
zhangjinxuan 发表于 2023-6-14 19:00
7:05谁都能起得来

这样,你在深夜的时候这么说:

太好了我还一直在想理由呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 02:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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