数据结构栈的应用
本帖最后由 cddyy2366 于 2020-11-10 20:58 编辑大佬们我现在在用栈做一个实现四则运算(加减乘除)的程序,但是现在不论我输什么算式进去他的结果都是随机的数字,我感觉是while(equation!='#' || last!='#')这个while循环有问题但是又不知道具体是什么问题,可以麻烦大家帮我看一下该怎么改吗,谢谢各位了!{:10_286:}
源代码如下:
#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=//显示算符优先关系的二维数组
{
/* '+' '-' '*' '/' '(' ')' '#' */
/* '+'*/ '>','>','<','<','<','>','>',
/* '-'*/ '>','>','<','<','<','>','>',
/* '*'*/ '>','>','>','>','<','>','>',
/* '/'*/ '>','>','>','>','<','>','>',
/* '('*/ '<','<','<','<','<','=',' ',
/* ')'*/ '>','>','>','>',' ','>','>',
/* '#'*/ '<','<','<','<','<',' ','=',
};
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;
}
Status In(OperandType c)//判断是数字还是运算符
{
switch(c)
{
case '+':return OK;
case '-':return OK;
case '*':return OK;
case '/':return OK;
case '(':return OK;
case ')':return OK;
case '#':return OK;
default:return ERROR;
}
}
OperandType Operate()
{
SqStack OPTR,OPND;
OperandType a,b,last,theta,num;
char OP={'+','-','*','/','(',')'};
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;
int i=0;
while(!In(equation))
{
data=equation-'0';
i++;
equation=getchar();
}
for(int j=0;j<i;j++)
{
num=num*10+data;
}
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;
}
页:
[1]