|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 cddyy2366 于 2020-11-10 20:58 编辑
大佬们我现在在用栈做一个实现四则运算(加减乘除)的程序,但是现在不论我输什么算式进去他的结果都是随机的数字,我感觉是while(equation!='#' || last!='#')这个while循环有问题但是又不知道具体是什么问题,可以麻烦大家帮我看一下该怎么改吗,谢谢各位了!
源代码如下:
- #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];
- }
- 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[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;
- }
复制代码 |
|