|
10鱼币
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <iostream>
- using namespace std;
- typedef struct bitnode
- {
- char data[10];
- struct bitnode *lc,*rc;
- }bitnode, *bitree;
- void create(bitree &t)
- {
- char c[10];
- cin>>c;
- if(c[0]=='#')
- {
- t=NULL;
- }
- else
- {
- t=(bitnode *)malloc(sizeof(bitnode));
- strcpy(t->data,c);
- create(t->lc);
- create(t->rc);
- }
- }
- void inordertraverse(bitree t)
- {
- if(t)
- {
- inordertraverse(t->lc);
- printf("%s ",t->data);
- inordertraverse(t->rc);
- }
- }
- void print(bitree &t)
- {
- if(t)
- {
- if((t->data[0]=='+')||(t->data[0]=='-')||(t->data[0]=='*')||(t->data[0]=='/'))
- {
- printf("%s","(");
- print(t->lc);
- printf("%s",t->data);
- print(t->rc);
- printf("%s",")");
- }
- else
- {
- printf("%s",t->data);
- }
- }
- }
- int cal(bitree &t)
- {
- double result=0;
- int i=0;
- int len=0;
- if(t)
- {
- if(t->data[0]=='+')
- {
- return cal(t->lc)+cal(t->rc);
- }
- else if(t->data[0]=='-')
- {
- return cal(t->lc)-cal(t->rc);
- }
- else if(t->data[0]=='*')
- {
- return cal(t->lc)*cal(t->rc);
- }
- else if(t->data[0]=='/')
- {
- if(cal(t->rc)!=0)
- {
- return cal(t->lc)/cal(t->rc);
- }
- }
- else
- {
- len=strlen(t->data);
- for(i=0;i<=len-1;i++){
- if(t->data[i]>='0'&&t->data[i]<='9')
- {
- result=result*10.0+t->data[i]-'0'+0.0;
- }
- }
- return result;
- }
- }
- }
- int main(void)
- {
- char c[10];
- bitree t;
- while(cin>>c)
- {
- t=(bitnode *)malloc(sizeof(bitnode));
- strcpy(t->data,c);
- create(t->lc);
- create(t->rc);
- if(fabs(cal(t)-24)<0.00001)
- {
- print(t);
- printf("%s","=");
- cout<<cal(t);
- }
- else
- {
- printf("%s","NO");
- }
- printf("\n");
- }
- return 0;
- }
复制代码
这道题目可以使用递归的思路,递归地生成二叉树,并计算出表达式的值。对生成的二叉树进行中序遍历,将符合题目要求的输出出来,否则输出 NO。
现在来看代码中存在的问题:
1. 在程序执行完打印相应信息后,没有释放生成树节点所占用的动态内存;
2. 代码中如果遇到除数为零且无法进行运算的情况,则没有进行任何处理,导致程序的计算结果不稳定。
下面是修改过后的代码,仅供参考:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <iostream>
- using namespace std;
- typedef struct bitnode
- {
- char data[10];
- struct bitnode *lc, *rc;
- } bitnode, *bitree;
- void create(bitree& t)
- {
- char c[10];
- cin >> c;
- if (c[0] == '#')
- {
- t = NULL;
- }
- else
- {
- t = (bitnode*)malloc(sizeof(bitnode));
- strcpy(t->data, c);
- create(t->lc);
- create(t->rc);
- }
- }
- void inordertraverse(bitree t)
- {
- if (t)
- {
- inordertraverse(t->lc);
- printf("%s ", t->data);
- inordertraverse(t->rc);
- }
- }
- void print(bitree& t)
- {
- if (t)
- {
- if ((t->data[0] == '+') || (t->data[0] == '-') || (t->data[0] == '*') || (t->data[0] == '/'))
- {
- printf("%s", "(");
- print(t->lc);
- printf("%s", t->data);
- print(t->rc);
- printf("%s", ")");
- }
- else
- {
- printf("%s", t->data);
- }
- }
- }
- double cal(bitree& t)
- {
- double result = 0;
- int i = 0;
- int len = 0;
- if (t)
- {
- if (t->data[0] == '+')
- {
- return cal(t->lc) + cal(t->rc);
- }
- else if (t->data[0] == '-')
- {
- return cal(t->lc) - cal(t->rc);
- }
- else if (t->data[0] == '*')
- {
- return cal(t->lc) * cal(t->rc);
- }
- else if (t->data[0] == '/')
- {
- double rc = cal(t->rc);
- if (rc != 0)
- {
- return cal(t->lc) / cal(t->rc);
- }
- else
- {
- return 0;
- }
- }
- else
- {
- len = strlen(t->data);
- for (i = 0; i <= len - 1; i++) {
- if (t->data[i] >= '0' && t->data[i] <= '9')
- {
- result = result * 10.0 + t->data[i] - '0' + 0.0;
- }
- }
- return result;
- }
- }
- else
- {
- return 0;
- }
- }
- void destory(bitree& t)
- {
- if (t)
- {
- destory(t->lc);
- destory(t->rc);
- free(t);
- }
- }
- int main(void)
- {
- char c[10];
- bitree t;
- while (cin >> c)
- {
- t = (bitnode*)malloc(sizeof(bitnode));
- strcpy(t->data, c);
- create(t->lc);
- create(t->rc);
- if (fabs(cal(t) - 24) < 0.00001)
- {
- print(t);
- printf("%s", "=");
- cout << cal(t);
- }
- else
- {
- printf("%s", "NO");
- }
- destory(t);//释放创建计算树时所有节点占用的内存
- printf("\n");
- }
- return 0;
- }
复制代码
希望对您有所帮助。
|
最佳答案
查看完整内容
这道题目可以使用递归的思路,递归地生成二叉树,并计算出表达式的值。对生成的二叉树进行中序遍历,将符合题目要求的输出出来,否则输出 NO。
现在来看代码中存在的问题:
1. 在程序执行完打印相应信息后,没有释放生成树节点所占用的动态内存;
2. 代码中如果遇到除数为零且无法进行运算的情况,则没有进行任何处理,导致程序的计算结果不稳定。
下面是修改过后的代码,仅供参考:
希望对您有所帮助。
|