利用二叉树求解表达式的值

版权声明:欢迎分享(指明出处),若有错误还请指正!!! https://blog.csdn.net/zj19941201/article/details/72833138
#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);	
		}	
	}	
}


猜你喜欢

转载自blog.csdn.net/zj19941201/article/details/72833138