请各位大佬帮忙看一下我的这个无括号算术表达式求值哪里出错了
#include <stdio.h>#include <stdlib.h>
#include <math.h>
#define false 0
#define true 1
#define Max 50
//^:幂运算
typedef struct node
{
char data;
struct node *next;
}Stack;
Stack *InitStack(Stack *Top)
{
Top=(Stack *)malloc(sizeof(Stack));
Top->next=NULL;
return Top;
}
int Push(Stack *Top,char ch)
{
Stack *p=(Stack *)malloc(sizeof(Stack));
if(p==NULL)
return false;
p->data=ch;
p->next=Top->next;
Top->next=p;
// p->next=Top;
// Top=p;
return true;
}
int Pop(Stack *Top,char ch)
{
Stack *temp=(Stack *)malloc(sizeof(Stack));
if(Top->next==NULL)
return false;
ch=Top->next->data;
temp=Top;
Top=Top->next;
free(temp);
return ch;
}
int In(char ch)//不是操作符,是操作数
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
return true;
else
return false;
}
int GetNumber(char ch)
{
int N=ch-48;
return N;
}
char GetTop(Stack *Top)
{
return Top->next->data;
}char Compare(char ch1,char ch2)//ch1为读入的字符,ch2为OP栈元素
{
char ch;
switch(ch2)
{
case '+':
case '-': if(ch1=='('||ch1=='#')
ch='<';
else
ch='>';
break;
case '/':
if(ch1=='*'||ch1=='/'||ch1==')')
ch='>';
else
ch='<';
break;
case '(':
if(ch1==')')
{
printf("ERROR1\n");
exit(false);
}
else
ch='<';
break;
case ')':
switch(ch1)
{
case '(':
ch='=';break;
case '#':
printf("ERROR2\n");
exit(false);
default :ch='>';
}
break;
case '#':switch(ch1)
{
case '#':
ch='=';
break;
case '(':
exit(false);
default : ch='>';
}break;
}
return ch;
}
void Input(Stack *Top)
{
Stack *p=Top->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}
int EvpEvaluation()
{
int n,v;
char op,a,b;
int result;
Stack *OPTR,*OVS;
OPTR=InitStack(OPTR);
OVS=InitStack(OVS);
Push(OPTR,'#');
printf("\n\nPlease input an expression (Ending with '#':");
char ch;
ch=getchar();
getchar();
while(ch!='#'&&GetTop(OPTR)!='#')//得到栈顶元素函数
{
if(!In(ch))
{
n=GetNumber(ch);//字符转化为整型数函数
Push(OVS,n);
ch=getchar();
getchar();
}
else
{
switch(Compare(ch,GetTop(OPTR)))//操作符优先级比较
{
case '>':Push(OPTR,ch);//形成运算
ch=getchar();
getchar();
break;
case '=':
case '<':
op=Pop(OVS,op);
b=Pop(OVS,b);
a=Pop(OVS,a);
v=Execute(a,op,b);
Push(OVS,v);
break;
}
}
}
result=GetTop(OVS);
return result;
}
int Execute(int a,char op,int b)
{
switch(op)
{
case '+':return a+b;break;
case '-':return a-b;break;
case '*':return a*b;break;
case '/':return a/b;break;
case '^':return pow(a,b);break;
default :printf("输入有误!");
exit(false);
}
}
int main()
{
int flag=1;
char yesorno;
while(flag)
{
printf("输入你想要计算的算数表达式(无括号):");
printf("%d",EvpEvaluation());
printf("你是否还要计算下一个算术表达式(y/n):");
scanf("%c\n",&yesorno);
if(yesorno=='y'||yesorno=='Y')
;
else flag=0;
}
return 0;
}