1. 程式人生 > >利用二叉樹求解表示式的值

利用二叉樹求解表示式的值

#include<stdio.h> #include<stdlib.h> #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef char Elemtype; typedef struct{ Elemtype *elem; int length; int listsize; }SqList; typedef char TElemtype; typedef struct BiTNode{ TElemtype data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; typedef char SElemtype; typedef struct{ SElemtype *base; SElemtype *top; int stacksize; }SqStack; #include"operation.h" main() { SqList L;BiTree T; InitBiTree(T); InitList(L); printf("請輸入要操作的算術表示式:"); while((L.elem[L.length]=getchar())!='\n') { if(L.length+1>=L.listsize) { L.elem=(Elemtype *)realloc(L.elem, (L.listsize+LISTINCREMENT)*sizeof(Elemtype)); if(!L.elem)exit(-2); L.listsize+=LISTINCREMENT; } L.length++; } T=predicate(L,0,L.length); putchar(10); printf("前序遍歷:"); PreOrderTraverse(T); putchar(10); putchar(10); printf("中序遍歷:"); InOrderTraverse(T); putchar(10); putchar(10); printf("後序遍歷:"); PostOrderTraverse(T); putchar(10); SqStack S; InitStack(S); calculate(T,S); putchar(10); int result=*S.base; printf("result=%d",result); return 0; } /*~~~~~~~~~~~~~~~~~~*/ void InitBiTree(BiTree &T) //初始化二叉樹T { T=(BiTree)malloc(sizeof(BiTNode)); if(!T)exit(-2); } BiTNode* predicate(SqList &L,int frist,int last) //還要考慮小括號在最外層的情況 { BiTree T1; if(last-frist==1) { BiTree Ty; InitBiTree(Ty); Ty->data=L.elem[frist]; Ty->lchild=NULL; Ty->rchild=NULL; return Ty; } int flag=0,jt=0,ct=0,t=0; for(int i=frist;i<last;i++) { if(L.elem[i]=='(')flag++; else if(L.elem[i]==')')flag--; if(flag==0){ if(L.elem[i]=='+'||L.elem[i]=='-') jt=i; else if(L.elem[i]=='*'||L.elem[i]=='/') ct=i; } } if((ct==0)&&(jt==0)) T1=predicate(L,frist+1,last-1); else{ if(jt>0)t=jt; else if(ct>0)t=ct; InitBiTree(T1); T1->data=L.elem[t]; T1->lchild=predicate(L,frist,t); T1->rchild=predicate(L,t+1,last); } return T1; } void InitList(SqList &L) { L.elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype)); if(!L.elem)exit(-2); L.length=0; L.listsize=LIST_INIT_SIZE; } void PreOrderTraverse (BiTree T) //先序遍歷二叉樹T { if(T==NULL) { return; } else { printf("%c ",T->data); PreOrderTraverse (T->lchild); PreOrderTraverse (T->rchild); } } void InOrderTraverse (BiTree T) //中序遍歷二叉樹T { if(T==NULL) { return; } else { InOrderTraverse (T->lchild); printf("%c ",T->data); InOrderTraverse (T->rchild); } } void PostOrderTraverse (BiTree T) //後序遍歷二叉樹T { if(T==NULL) { return; } else { PostOrderTraverse (T->lchild); PostOrderTraverse (T->rchild); printf("%c ",T->data); } } void InitStack(SqStack &S){ //棧的初始化 S.base = (SElemtype *)malloc(STACK_INIT_SIZE*sizeof(SElemtype)); if(!S.base)exit(-2); S.top=S.base; S.stacksize=STACK_INIT_SIZE; } void Push(SqStack &S,int e) //入棧 { if(S.top-S.base>=S.stacksize) {S.base=(SElemtype *)realloc(S.base, (S.stacksize+STACKINCREMENT)*sizeof(SElemtype)); if(!S.base)exit(-2); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; } int Pop(SqStack &S) //出棧 { int e; if(S.top==S.base)return 0; S.top--; e=*S.top; return e; } void calculate(BiTree T,SqStack &S) { if(T==NULL) { return; } else { calculate(T->lchild,S); calculate(T->rchild,S); if(T->data!='+'&&T->data!='-'&&T->data!='/'&&T->data!='*') { int n=T->data-'0'; Push(S,n); } else { int a=Pop(S); int b=Pop(S); int result=0; if(T->data=='+')result=b+a; else if(T->data=='-')result=b-a; else if(T->data=='/')result=b/a; else if(T->data=='*')result=b*a; Push(S,result); } } }