大佬們,我現在在做一個用堆疊實作四則運算求值的程式,但是現在不論我輸什么運算式進去,得出來的答案都是隨機的數字,可以幫我看一下哪里有問題,該怎么改進嗎

#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char SElemType;
typedef int Status;
typedef char OperandType;//運算型別
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack *S)//構造空堆疊
{
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base)exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACKINCREMENT;
return OK;
}
Status Push(SqStack *S,SElemType e)//入堆疊
{
if(S->top-S->base >= S->stacksize)
{
S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S->base)exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
Status Pop(SqStack *S,SElemType *e)//出堆疊
{
if(S->top==S->base)return ERROR;
*e=*--S->top;
return OK;
}
SElemType GetTop(SqStack *S,SElemType *e)//得到堆疊頂元素
{
if(S->top==S->base)
{
return ERROR;
}
*e=*(S->top-1);
return OK;
}
OperandType Easyoperate(OperandType a,OperandType b,OperandType theta)//計算簡單的運算
{
OperandType answer;
switch(theta)
{
case '+':
answer=a+b;
return answer;break;
case '-':
answer=a-b;
return answer;break;
case '*':
answer=a*b;
return answer;break;
case '/':
answer=a/b;
return answer;break;
}
}
char Compare(char a,char b)//判斷算符優先級
{
int i,j;
char prior[7][7]=//顯示算符優先關系的二維陣列
{
/* '+' '-' '*' '/' '(' ')' '#' */
/* '+'*/ '>','>','<','<','<','>','>',
/* '-'*/ '>','>','<','<','<','>','>',
/* '*'*/ '>','>','>','>','<','>','>',
/* '/'*/ '>','>','>','>','<','>','>',
/* '('*/ '<','<','<','<','<','=',' ',
/* ')'*/ '>','>','>','>',' ','>','>',
/* '#'*/ '<','<','<','<','<',' ','=',
};
switch(a)
{
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}
switch(b)
{
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}
return prior[i][j];
}
bool In(OperandType c)//判斷是數字還是運算子
{
switch(c)
{
case '+':return true;
case '-':return true;
case '*':return true;
case '/':return true;
case '(':return true;
case ')':return true;
case '#':return true;
default:return false;
}
}
OperandType Operate()
{
SqStack OPTR,OPND;
OperandType a,b,last,theta,num;
char OP[6]={'+','-','*','/','(',')'};
InitStack(&OPTR);
Push(&OPTR,'#');
InitStack(&OPND);
printf("輸入運算式:");
char equation;
equation=getchar();
GetTop(&OPTR,&last);
while(equation!='#' || last!='#')//輸入算式,以#結尾
{
if(!In(equation))//如果不是運算子,把char型別的數字轉為int型然后進OPND堆疊
{
int data[10];
int i=0;
while(!In(equation))
{
data[i]=equation-'0';
i++;
equation=getchar();
}
for(int j=0;j<i;j++)
{
num=num*10+data[j];
}
Push(&OPND,num);
}
else
{
switch(Compare(last,equation))
{
case '<':
Push(&OPTR,equation);
equation=getchar();
break;
case '=':
Pop(&OPTR,&last);
equation=getchar();
break;
case '>':
Pop(&OPTR,&theta);
Pop(&OPND,&b);
Pop(&OPND,&a);
Push(&OPND,Easyoperate(a,b,theta));
break;
}
}
GetTop(&OPTR,&last);
}
GetTop(&OPND,&last);
return last;
}
int main()
{
printf("%d",Operate());
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/197547.html
標籤:C語言
上一篇:漢字轉UTF-8
下一篇:新手c#問題
