主题:关于表达式求值的问题,各位一定要帮忙啊
是一个用栈解决表达式求职的问题,用两个栈存储运算符合运算数,输入一个表达式,比如:2+3*4-1,则输出13,我编的这个总是出错,各位帮忙看一下啊
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#define STACK_SIZE 40
#define STACKINCREMENT 10
typedef struct
{
char *base;
char *top;
int stacksize;
}SqStack;
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack_Num;
void InitStack(SqStack *s)
{
s->base=(char *)malloc(STACK_SIZE*sizeof(char));
if(!s->base) exit(0);
s->top=s->base; /*栈顶指向空*/
s->stacksize=STACK_SIZE;
}
void InitStack_Num(SqStack_Num *s)
{
s->base=(int *)malloc(STACK_SIZE*sizeof(int));
if(!s->base) exit(0);
s->top=s->base; /*栈顶指向空*/
s->stacksize=STACK_SIZE;
}
char GetTop(SqStack *s)
{
if(s->top==s->base) exit(0);
return *(s->top-1);
}
int GetTop_Num(SqStack_Num *s)
{
if(s->top==s->base) exit(0);
return *(s->top-1);
}
void Push(SqStack *s, char e)
{
*(s->top)=e;
s->top++;
}
void Push_Num(SqStack_Num *s, int e)
{
*(s->top)=e;
s->top++;
}
char Pop(SqStack *s)
{
if(s->base==s->top) exit(0);
s->top--;
return *(s->top);
}
int Pop_Num(SqStack_Num *s)
{
if(s->base==s->top) exit(0);
s->top--;
return *(s->top);
}
int In(char e)
{
if(e>=48&&e<=57) return 0;
else return 1;
}
char Precede(char c,char d)
{
int i,j;
char a[7]={'+','-','*','/','(',')','#'};
char b[7][7]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}};
for(i=0;c!=a[i];i++);
for(j=0;d!=a[j];j++);
return (b[i][j]);
}
int Operate(int x,char c,int y)
{
switch(c)
{
case'+':return (x+y);
case'-':return (x-y);
case'*':return (x*y);
case'/':return (x/y);
}
}
char EvaluateExpression()
{
SqStack *OPTR;
SqStack_Num *OPND;
char c,theta;
char letter[20];
int x,y;
InitStack(OPTR);
Push(OPTR,'#');
InitStack_Num(OPND);
c=getchar();
while(c!='#'||GetTop(OPTR)!='#')
{
if(!In(c))
{
Push(OPND,(int)(c-48));
c=getchar();
}
else
switch(Precede(GetTop(OPTR),c))
{
case'<':Push(OPTR,c);c=getchar();break;
case'=':Pop(OPTR);c=getchar();break;
case'>':theta=Pop(OPTR);
y=Pop_Num(OPND);x=Pop_Num(OPND);
Push_Num(OPND,Operate(x,theta,y));
if(c=='#') break;
else {Push(OPTR,c);
c=getchar();break;}
}
}
return(GetTop_Num(OPND));
}
main()
{
printf("%d",EvaluateExpression());
}
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#define STACK_SIZE 40
#define STACKINCREMENT 10
typedef struct
{
char *base;
char *top;
int stacksize;
}SqStack;
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack_Num;
void InitStack(SqStack *s)
{
s->base=(char *)malloc(STACK_SIZE*sizeof(char));
if(!s->base) exit(0);
s->top=s->base; /*栈顶指向空*/
s->stacksize=STACK_SIZE;
}
void InitStack_Num(SqStack_Num *s)
{
s->base=(int *)malloc(STACK_SIZE*sizeof(int));
if(!s->base) exit(0);
s->top=s->base; /*栈顶指向空*/
s->stacksize=STACK_SIZE;
}
char GetTop(SqStack *s)
{
if(s->top==s->base) exit(0);
return *(s->top-1);
}
int GetTop_Num(SqStack_Num *s)
{
if(s->top==s->base) exit(0);
return *(s->top-1);
}
void Push(SqStack *s, char e)
{
*(s->top)=e;
s->top++;
}
void Push_Num(SqStack_Num *s, int e)
{
*(s->top)=e;
s->top++;
}
char Pop(SqStack *s)
{
if(s->base==s->top) exit(0);
s->top--;
return *(s->top);
}
int Pop_Num(SqStack_Num *s)
{
if(s->base==s->top) exit(0);
s->top--;
return *(s->top);
}
int In(char e)
{
if(e>=48&&e<=57) return 0;
else return 1;
}
char Precede(char c,char d)
{
int i,j;
char a[7]={'+','-','*','/','(',')','#'};
char b[7][7]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}};
for(i=0;c!=a[i];i++);
for(j=0;d!=a[j];j++);
return (b[i][j]);
}
int Operate(int x,char c,int y)
{
switch(c)
{
case'+':return (x+y);
case'-':return (x-y);
case'*':return (x*y);
case'/':return (x/y);
}
}
char EvaluateExpression()
{
SqStack *OPTR;
SqStack_Num *OPND;
char c,theta;
char letter[20];
int x,y;
InitStack(OPTR);
Push(OPTR,'#');
InitStack_Num(OPND);
c=getchar();
while(c!='#'||GetTop(OPTR)!='#')
{
if(!In(c))
{
Push(OPND,(int)(c-48));
c=getchar();
}
else
switch(Precede(GetTop(OPTR),c))
{
case'<':Push(OPTR,c);c=getchar();break;
case'=':Pop(OPTR);c=getchar();break;
case'>':theta=Pop(OPTR);
y=Pop_Num(OPND);x=Pop_Num(OPND);
Push_Num(OPND,Operate(x,theta,y));
if(c=='#') break;
else {Push(OPTR,c);
c=getchar();break;}
}
}
return(GetTop_Num(OPND));
}
main()
{
printf("%d",EvaluateExpression());
}