求助大神,关于C语言为基础的数据结构,动态表达式求值
求助大神,关于C语言为基础的数据结构,动态表达式求值,我遇到难题,求助大神帮忙,时间紧急,在线等,QQ:358791396,微信:xhj368447,电话:13161076219.这是我写的代码,求帮忙
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100;
typedef struct node{
char data;
struct node *lchild,*rchild;
}bitnode,*bitree;
#define chsize 20
#define flsize 30
/*建立字符栈*/
typedef struct
{
char elem[chsize];//存储定义
int top;
}chstack;
void Initchar(chstack *S)//初始化顺序栈
{
S->top=-1;
}
int Pushchar(chstack *S,char x)//进栈
{
if(S->top==-1) return 0;
S->top++;
S->elem[S->top]=x;
return 1;
}
int Popchar(chstack *S,char *x)//出栈
{
if(S->top==-1) return 0;
else
{
*x=S->elem[S->top];
S->top--;
return(1);
}
}
int GetTopchar(chstack S,char*x)// 取栈顶
{
if(S.top==-1) return(0);
else
{
*x=S.elem[S.top];
return(1);
}
}
//建立子树栈
typedef struct//建立
{
bitree elem[flsize];
int top;
}substack;
void InitStree(substack *S)//初始化
{
S->top=-1;
}
int Pushtree(substack *S,bitree e) //进栈
{
if(S->top==-1) return(0);
else
{
S->top++;
S->elem[S->top]=e;
return(1);
}
}
int Poptree(substack *S,bitree *x)//出栈
{
if(S->top==-1) return(0);
else
{
*x=S->elem[S->top];
S->top--;
return(1);
}
}
int GetToptree(substack S,bitree *x)// 取栈顶
{
if(S.top==-1) return(0);
else
{
*x=S.elem[S.top];
return(1);
}
}
void CrtNode(bitree *T,substack *PTR,char ch)
{
if(!(*T=new(bitnode))) exit(0);
(*T)->data=ch;
(*T)->lchild=(*T)->rchild=NULL;
Pushtree(PTR,(*T));
}
void CrtSubTree(bitree *T,substack *PTR,char c)
{
bitree lc,rc;
if(!((*T)=new(bitnode))) exit(0);
(*T)->data=c;
Poptree(PTR,&rc);
(*T)->rchild=rc;
Poptree(PTR,&lc);
(*T)->rchild=lc;
Pushtree(PTR,*T);
}
int IN(char c)
{
if(c=='(' || c==')' || c=='*' || c=='/' || c=='+' || c=='-') return 0;
else return 1;
}
int prior(char a)
{
if(a=='*' || a=='/') return 4;
else if(a=='+' || a=='-') return 3;
else if(a=='(') return 2;
else if(a=='#') return 1;
else return 0;
}
void crtex(bitree *T,char exp[])
{
substack PTR;
chstack PND;
int i,j;
Initchar(&PND); Pushchar(&PND,'#');
InitStree(&PTR);
char *p=exp;char ch,c;ch=*p;
GetTopchar(PND,&c);
while(c!='#' || ch!='#')
{
if(!IN(ch)) CrtNode(T,&PTR,ch);
else{
switch(ch){
case '(':Pushchar(&PND,ch);break;
case ')':Popchar(&PND,&c);
while(c!='(')
{
CrtSubTree(T,&PTR,c);
Popchar(&PND,&c);
}
break;
default:GetTopchar(PND,&c);
i=prior(c);j=prior(ch);
while(c!='#' && i>=j)
{
CrtSubTree(T,&PTR,c);
Popchar(&PND,&c);
i=prior(c);j=prior(ch);
}
if(ch!='#') Pushchar(&PND,ch);
}
if(ch!='#') {p++;ch=*p;}
}
}
Poptree(&PTR,T);
}
double culexp(bitree T)
{
double result,a,b;
if(T){
if(!IN(T->data))
{
printf("请输入%c的值",T->data);
scanf("%lf",&result); return result;
}
a=culexp(T->lchild); b=culexp(T->rchild);
switch(T->data)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':if(b!=0) return a/b;
else {printf("分母为0!!!!\n"); exit(0);}
}
}
}
void main()
{
char ex[100];
bitree t;
printf("请输入字符串,以#结束\n");
gets(ex);
crtex(&t,ex);
culexp(t);
}