| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2119 人关注过本帖
标题:[求助]期末作业折腾了三天,我还是不会 (表达式求值和二叉树)
只看楼主 加入收藏
sakoda
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-6-4
收藏
 问题点数:0 回复次数:6 
[求助]期末作业折腾了三天,我还是不会 (表达式求值和二叉树)

哪位高手帮帮我 我憋了三天 还是大家帮我把 越完全越好 在下实在是学问太少了~~~ 1. 表达式求值。 基本要求 1)读入原表达式(括号),创建对应二叉树叉树。 2)对二叉树进行前序遍历、中序遍历、后续遍历(非递归)。 3)后缀表达式和前缀表达式求解原表达式的值。 对非法表达式格式能予以判断。 【选作内容 原表达式中有三种括号,先作(),再作[],最后是{}。判断括号的优先级别建立二叉树。

搜索更多相关主题的帖子: 二叉树 求值 作业 折腾 表达 
2005-06-04 18:32
tary
Rank: 1
等 级:新手上路
帖 子:780
专家分:0
注 册:2004-10-5
收藏
得分:0 
你折腾了三天..  

那你的思路呢??三天的时间啊, 说来也不短了.. 你总要有点思想吧.

说出来听听..

┌→¨ ≮我可以学会对你很冷落≯¨←┐ │  <却学不╓══╦══╖会将爱> │ │¨←┐ ╭╩╮哭‖哭╭╩╮ ┌→¨│ └──┘收 ╲╱ ◇‖◇ ╲╱回└──┘
2005-06-04 21:48
sakoda
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-6-4
收藏
得分:0 

怎么说??? 我编了一点点 可是建立树的时候不能够进行括号的判断 就是如果表达式里面有括号就不行了 还有 就是说 不能区分数字和字符呀 怎么才能求值呢??? 要怎么弄呢????? //定义含有三个指针的结点 typedef struct BiTNode{ char data; int op; struct BiTNode *lchild, *rchild, *parent; }BiTNode, *BiTree;

int length; BiTNode *CreatTree(char a[]) { //建立二叉树 BiTNode *t; BiTNode *p; BiTNode *s; BiTNode *q; BiTNode *T; bool cre; cre=false;

/*t=(BiTNode *)malloc(sizeof(BiTNode)); t->lchild=NULL; t->rchild=NULL; t->parent=NULL; */ t=(BiTNode *)malloc(sizeof(BiTNode)); t->lchild=NULL; t->rchild=NULL; t->parent=NULL; t->data=a[0]; t->op=0; p=t; char in; for(int i=1;i<length;i++) { in=a[i]; switch(in) { case '+': t=(BiTNode *)malloc(sizeof(BiTNode)); t->parent =NULL; t->rchild=NULL; t->lchild =NULL; t->data=a[i]; t->op=1; s=p; while(s!=NULL) { q=s; s=s->parent; } q->parent=t; t->lchild=q; //q->rchild =NULL; t->parent =NULL; p=t; break; case '-': t=(BiTNode *)malloc(sizeof(BiTNode)); t->rchild=NULL; t->data=a[i]; t->op=1; s=p; while(s!=NULL) { q=s; s=s->parent; } q->parent=t; t->lchild=q; // q->rchild =NULL; t->parent =NULL; p=t; break;

case '*': t=(BiTNode *)malloc(sizeof(BiTNode)); t->lchild=NULL; t->rchild=NULL; t->data=a[i]; t->op=-1; t->parent =NULL;

if(p->parent ==NULL) { p->parent =t; t->lchild =p; }

else { while(p->parent !=NULL&&p->parent ->op !=1) p=p->parent ; if(p->parent ==NULL) { p->parent =t; t->lchild =p;

} else { p->parent ->rchild =t; t->parent =p->parent ; t->lchild =p; p->parent =t; }

}

p=t; break;

case '/': t=(BiTNode *)malloc(sizeof(BiTNode)); t->lchild=NULL; t->rchild=NULL; t->data=a[i]; t->op=-1; t->parent =NULL;

if(p->parent ==NULL) { p->parent =t; t->lchild =p; }

else { while(p->parent !=NULL&&p->parent ->op !=1) p=p->parent ; if(p->parent ==NULL) { p->parent =t; t->lchild =p;

} else { p->parent ->rchild =t; t->parent =p->parent ; t->lchild =p; p->parent =t; }

}

p=t; break; default:

t=(BiTNode *)malloc(sizeof(BiTNode)); t->lchild=NULL; t->rchild=NULL; t->parent=p; t->data=a[i]; t->op=0; p->rchild=t; p=t; } }

while(p) { T=p; p=p->parent ; } cout<<T->data <<endl; return T; } //CreatTree

2005-06-04 21:58
david2234
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2005-6-1
收藏
得分:0 
偶也为表达式求值头痛呢

2005-06-08 00:03
tary
Rank: 1
等 级:新手上路
帖 子:780
专家分:0
注 册:2004-10-5
收藏
得分:0 

偶记得表达式的求值要用到栈,,

不过,偶没去做,, 书上有就是了/


┌→¨ ≮我可以学会对你很冷落≯¨←┐ │  <却学不╓══╦══╖会将爱> │ │¨←┐ ╭╩╮哭‖哭╭╩╮ ┌→¨│ └──┘收 ╲╱ ◇‖◇ ╲╱回└──┘
2005-06-08 06:52
stnlcd
Rank: 1
等 级:新手上路
帖 子:177
专家分:1
注 册:2004-11-21
收藏
得分:0 

以下是1996年高级程序员考试的下午第5题(计算器模拟程序),写的非常的经典,但只有+,-,*和/四个运算,但可以在它的基础上修改获得其他的运算: #include <stdio.h> int add(int x,int y) {return x+y;} int sub(int x,int y) {return x-y;} int mul(int x,int y) {return x*y;} int div(int x,int y) {return x/y;} int (*func[])()={add,sub,mul,div}; int num,curch; char chtbl[]="+-*/()="; char corch[]="+-*/()=0123456789"; int getach() { int i; while(1) { curch=getchar(); if(curch==EOF) return -1; for(i=0;corch[i]&&curch!=corch[i];i++); if(i<strlen(corch)) break; } return curch; }

int getid() { int i; if(curch>='0'&&curch<='9') { for(num=0;curch>='0'&&curch<='9';getach()) num=10*num+curch-'0'; return -1; } else { for(i=0;chtbl[i];i++) if(chtbl[i]==curch) break; if(i<=5) getach(); return i; } }

int cal() { int x1,x2,x3,op1,op2,i; i=getid(); if(i==4) x1=cal(); else x1=num; op1=getid(); if(op1>=5) return x1; i=getid(); if(i==4) x2=cal(); else x2=num; op2=getid(); while(op2<=4) { i=getid(); if(i==4) x3=cal(); else x3=num; if((op1/2==0)&&(op2/2==1)) x2=(*func[op2])(x2,x3); else { x1=(*func[op1])(x1,x2); x2=x3; op1=op2; } op2=getid(); } return (*func[op1])(x1,x2); }

void main(void) { int value; printf("Please input an expression:\n"); getach(); while(curch!='=') { value=cal(); printf("The result is:%d\n",value); printf("Please input an expression:\n"); getach(); } } 不要忘了‘=’号!


要让一个男人破产,请给他一架相机,要让一个男人倾家荡产,请给他一架望远镜。
2005-06-08 12:17
悠然随风
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2005-12-16
收藏
得分:0 
2005-12-16 23:04
快速回复:[求助]期末作业折腾了三天,我还是不会 (表达式求值和二叉树)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016071 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved