<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
/************************************************************************** 文件名:treeexpression.c** 文件描述:本程序给出表达式树的构造算法* 在turboc 2.0 调试通过,注意只能输入个位数,且没有加上* 对括号的处理,读者可以自己将其加上去* 创建人: 颜清国 2006年5月4日* 说明: *************************************************************************/#include "stdio.h"#include "string.h"#include "stdlib.h"#include "conio.h"#define MAX 20#define MAXS 100/********************************** 用来求后缀表达式的栈************************************/typedef struct sstack{ char str[MAXS]; int top;}stack;/************************************** 定义树的结构体***************************************/typedef struct tagtree{ char data; /*数据域*/ int flag; /*非递归操作时用来做标志*/ struct tagtree*lchild; /*指向左孩子的指针域*/ struct tagtree*rchild; /*指向右孩子的指针域*/}tree;/*入栈操作*/void push(stack *ta,char p){ ta->top++; ta->str[ta->top]=p;}/*出栈,返回栈顶的值*/char pop(stack *ta){ char temp; if(ta->top==-1)/*栈已经空*/ { printf("stack is empty!"); return 0; } else { temp=ta->str[ta->top]; ta->top--; } return temp;}/******************************将中缀表达式转化为后缀表达式*******************************/ void trans(char mid[],char last[]){ stack temp; /*临时栈,用来调整成后缀表达式*/ int lm=0,la=0,len=strlen(mid); temp.top=-1; /*初始栈为空*/ push(&temp,'('); /*整个表达式要加上括号*/ while(lm < len) { switch(mid[lm]) { case '-': /*'+''-'转化时,'('前的OP均出栈*/ case '+': /*注意必须先将整个表达式要加上括号*/ while(temp.str[temp.top]!='(') { last[la++]=pop(&temp); } push(&temp,mid[lm]);/*自己入栈*/ break; case '*': case '/': while((temp.str[temp.top]=='*') ||(temp.str[temp.top]=='/')) { last[la++]=pop(&temp);/*栈顶是'*','/'则出栈*/ } push(&temp,mid[lm]); /*自己入栈*/ break; case '(': push(&temp,'('); /*是'('直接入栈*/ break; case ')': while(temp.str[temp.top]!='(') { last[la++]=pop(&temp); /*将'('前所有OP出栈*/ } pop(&temp); /*将'('出栈,自己不入栈*/ break; default: if((mid[lm] >= '0')&&(mid[lm] <= '9'))/*可以屏蔽其它字符*/ { while((mid[lm] >='0')&&(mid[lm] <= '9')) { last[la++]=mid[lm++]; /*是数字保存到字串中*/ } lm--; /*需要退回来*/ } break; } lm++; /*依次扫描待转换的字串*/ } while(temp.top > 0) /*第0个元素为'(',不用保存*/ { last[la++]=pop(&temp); } last[la]='\0'; /*标志后缀表达式结束*/}/**************************************** 根据后缀和中缀表达式构造二叉树*****************************************/tree*PostCreateTree(char *post,char *mid,int n){ tree*root=NULL; int lp=0; if(n==0) return NULL; while(lp<n root-="" root="(tree*)malloc(sizeof(tree));"></n>data = post[n-1]; /*printf("\n%c",root->data); getch();*/ root->lchild = PostCreateTree(post,mid,lp); root->rchild = PostCreateTree(post+lp,mid+lp+1,n-lp-1); return root;}/************************************************ 用树形表示法输出树*************************************************/void DispTree(tree *root,int x,int y,int n) /*n用来控制第一层树的高度*/{ int i=0; if(root !=NULL) { gotoxy(x,y); /*到相应结点输出*/ printf("%c",root->data); if(root->lchild != NULL) /*处理左子树,这里只有第一次N为可变的,*/ { i=1; /*为的是能够输出整棵树,而不会被覆盖,*/ while(ilchild,x-n,y+n,2); /*递归处理左子树*/ } if(root->rchild != NULL) { i=1; while(irchild,x+n,y+n,2); /*递归处理右子树*/ } }}void main(){ char mid[100],post[100]; tree *root; printf("Please input the expression: "); gets(mid); trans(mid,post); root=PostCreateTree(post,mid,strlen(mid)); printf("\nthe ruselt is:"); DispTree(root,10,4,5); getch();}