写了个求表达式的程序但是不能编译请求答案
#include<stdio.h>#define max 100
typedef struct
{
int top;
char b[max];
}node;
void chushihua(node * s)//初始化堆栈
{
s->top=0;
}
int ruzhan(node * s,char a)//入栈
{
if(s->top>=max)
return 0;
else
{
s->b[s->top]=a;
s->top++;
return 1;
}
}
int chuzhan(node * s,char * d)//出栈
{
if(s->top<=0)
return 0;
else
{
s->top--;
* d=s->b[s->top];
return 1 ;
}
}
int qu(node s,char * d)//取头结点
{
if(s.top<=0)
return 0;
else
{
* d=s.b[s.top];
return 1 ;
}
}
int panduan(char c)//判断c是否是计算符号
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='=')
return 1;
else
return 0;
}
int calcute(int x,int y,char c)//计算x与y通过c运算符求出的结果
{
int result;
switch(c)
{
case'+': result=x+y;break;
case'-': result=x-y;break;
case '*': result = x * y;break;
case '/':
if (y == 0)
{
printf("Divided by zero!\n");
return 0;
}
else
{
result = x / y;
break;
}
default:
printf("Bad Input.\n");
return 0;
}
return result;
}
int daxiao(char x1,char x2)//比较运算符x1与x2的优先级
{
switch(x1)
{
case'+':
case'-':
if(x2=='+'||x2=='-')
return 1;
else if(x2=='*'||x2=='/')
return 0;
else if(x2=='(')
return 0;
else if(x2==')')
return 1;
else
return 1;break;
case'*':
case'/':
if(x2=='+'||x2=='-')
return 1;
else if(x2=='*'||x2=='/')
return 1;
else if(x2=='(')
return 0;
else if(x2==')')
return 1;
else
return 1;break;
case'(':
if(x2=='+'||x2=='-')
return 0;
else if(x2=='*'||x2=='/')
return 0;
else if(x2=='(')
return 0;
else if(x2==')')
return 2;
else
return 1;break;
case')':
if(x2=='+'||x2=='-')
return 1;
else if(x2=='*'||x2=='/')
return 1;
else if(x2==')')
return 1;
else
return 1;break;
case'#':
if(x2=='+'||x2=='-')
return 0;
else if(x2=='*'||x2=='/')
return 0;
else if(x2=='(')
return 0;
else
return 0;break;
default:break;
}
}
void jisuan(node * s1,node * s2,node * s3)//把s1的中缀表达式借助s2转换为s3的后缀表达式
{
char c,d;
char abc[max];
int i=0;
scanf("%s",abc);
while(abc[i]!='\0')
{
ruzhan(s1,abc[i]);
i++;
}
ruzhan(s1,'#');
ruzhan(s2,'#');
while(s1->top>=0)
{
chuzhan(s1,&c);
if((panduan(c))==0)
ruzhan(s3,c);
else
{
qu(*s2,&d);
if((daxiao(d,c))==0)
ruzhan(s2,c);
else if((daxiao(d,c))==1)
{
chuzhan(s2,&d);
ruzhan(s3,d);
}
else
{
chuzhan(s2,&d);
}
}
}
}
char zhuji(node * s3)//用堆栈计算后缀表达式
{
char d,a,b,,f;
char e;
node s4;
char c;
qu(*s3,&f);
while((panduan(f))==0)
{
chuzhan(s3,&d);
ruzhan(&s4,d);
}
while(s4.top>=0)
{
chuzhan(s3,&d);
chuzhan(&s4,&a);
chuzhan(&s4,&b);
c=calcute(a,b,d);
ruzhan(&s4,c);
}
qu(s4,&e);
return e;
}
void main()
{
node s1,s2,s3,s4;
chushihua(&s1);
chushihua(&s2);
chushihua(&s3);
chushihua(&s4);
jisuan(&s1,&s2,&s3);
printf("%d",zhuji(&s3));
}