栈的表达式求值问题
#include<stdio.h>#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIT_SIZE1 100
#define STACKINCREMENT1 10
#define STACK_INIT_SIZE2 100
#define STACKINCREMENT2 10
typedef char SElemType;
typedef int Status;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;}SqStacks;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;}SqStackd;
Status InitStack(SqStacks &S){
S.base=(SElemType *)malloc(STACK_INIT_SIZE1 * sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE1;
return OK;}
Status InitStack(SqStackd &D){
D.base=(int *)malloc(STACK_INIT_SIZE2 * sizeof(int));
if(!D.base) exit(OVERFLOW);
D.top=D.base;
D.stacksize=STACK_INIT_SIZE2;
return OK;}
Status GetTop(SqStacks S){char e;
if(S.top==S.base) return ERROR;
e=*(S.top-1);
return e;
}
Status GetTop(SqStacks D){int e;
if(D.top==D.base) return ERROR;
e=*(D.top-1);
return e;
}
Status Push(SqStacks &S,SElemType e){
if(S.top-S.base>=S.stacksize){
S.base=(SElemType *)realloc(S.base,
(S.stacksize+STACKINCREMENT1)*sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT1;}
*S.top++=e;
return OK;
}
Status Push(SqStackd &D,int e){
if(D.top-D.base>=D.stacksize){
D.base=(int *)realloc(D.base,
(D.stacksize+STACKINCREMENT2)*sizeof(int));
if(!D.base) exit(OVERFLOW);
D.top=D.base+D.stacksize;
D.stacksize+=STACKINCREMENT2;}
*D.top++=e;
return OK;
}
Status Pop(SqStacks &S,SElemType &e){
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
Status Pop(SqStackd &D,int e){
if(D.top==D.base) return ERROR;
e=*--D.top;
return OK;
}
int In(char c,char OP[7])
{
char OP[7]={'+','-','*','/','#','(',')'};
int i;
for(i = 0; i < 7; i++)
if(c == OP[i])
return 1;
return 0;
}
char Precede(char op,char c)
{
char ch;
if(op=='(' && c==')' || op=='#' && c=='#' )
ch = '=';
else
if(op=='+' || op=='-')
switch(c)
{
case '+':
case '-':
case ')':
case '#': ch = '>'; break;
case '*':
case '/':
case '(': ch = '<';
}
else
if(op=='*' || op=='/')
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case ')':
case '#': ch = '>'; break;
case '(': ch = '<';
}
else
if(op=='(')
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '(': ch = '<'; break;
case '#': printf("Error!\n"); exit(0);
}
else
if(op==')')
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '#': ch = '>'; break;
case '(': printf("Error!\n"); exit(0);
}
else
if(op=='#')
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '(': ch = '<'; break;
case ')': printf("Error!\n"); exit(0);
}
return ch;
}
int Operate(int a,char r,int b)
{
int s;
int d1 = a;
int d2 = b;
switch(r)
{
case '+': s = d1+d2; break;
case '-': s = d2-d1; break;
case '*': s = d1*d2; break;
case '/': s = d2/d1;
}
return s;
}
int EvalExpres()
{ int a,b;char x,theta,c;SqStacks OPTR;SqStackd OPND;
char OP[7]={'+','-','*','/','#','(',')'};
InitStack(OPTR);InitStack(OPND);
Push(OPTR,'#');
c=getchar();
while(c!='#' || GetTop(OPTR)!='#')
{
if(!In(c,OP)) {Push(OPND,c);c=getchar();}
else
switch(Precede(GetTop(OPTR),c))
{
case '<':
Push(OPTR,c);
c=getchar();
break;
case '=':
Pop(OPTR,x);
c=getchar();
break;
case '>':
Pop(OPTR,theta);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,theta,b)) ;
break;
}
}
return (GetTop(OPND));
}
void main(){
EvalExpres() ;
}
本人初学数据结构 很多不懂的地方 实属菜鸟 望哪位大侠能帮我解决这个问题 希望能在我的代码上找错并运行 c++6.0运行环境