算术表达式求值
#include"stdio.h"
#include"stdlib.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define ERROR 0
typedef int ElemType;
typedef int OpersndType;
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}Stack;
void InitStack(Stack &S)
{
S.base=(ElemType *)malloc(STACK_INIT_SIZE *sizeof(ElemType));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return;
}
void Push(Stack &S,ElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return;
}
int Pop(Stack &S,ElemType &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return TRUE;
}
ElemType GetTop(Stack &S)
{
if(S.top==S.base) return 0;
return *(S.top-1);
}
void ShowStack(Stack S)
{
ElemType *p=S.base;
while(p!=S.top)
printf("%d",*p++);
return;
}
char Precede(char c1,char c2)
{
int i=0,j=0,t,k;
int a[7][7]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}};
switch(c1)
{
case '+': t=0;break;
case '-': t=1;break;
case '*': t=2;break;
case '/': t=3;break;
case '(': t=4;break;
case ')': t=5;break;
case '#': t=6;break;
}
switch(c2)
{
case '+': k=0;break;
case '-': k=1;break;
case '*': k=2;break;
case '/': k=3;break;
case '(': k=4;break;
case ')': k=5;break;
case '#': k=6;break;
}
return a[t][k];
}
int Operate(int a,char m,int b)
{
switch(m)
{
case '+': return a+b;break;
case '-': return a-b;break;
case '*': return a*b;break;
case '/': return a/b;
}
return 0;
}
OpersndType EvaluateExpression()
{
char c,s[100];
int a,b,i,sum,m;
Stack OPTR,OPND;
InitStack(OPTR);
InitStack(OPND);
Push(OPTR,'#');
c=getchar();
while(c!='#'||GetTop(OPTR)!='#')
{
if(c>='0'&&c<='9')
{
for(i=0;c>='0'&&c<='9';i++)
{
s[i]=c;
c=getchar();
}
s[i]='"0';
sum=atoi(s);
Push(OPND,sum);
}
else
switch(Precede(GetTop(OPTR),c))
{
case '<': Push(OPTR,c);
c=getchar();
break;
case '=': Pop(OPTR,m);
c=getchar();
break;
case '>': Pop(OPTR,m);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,m,b));
break;
}
}
return GetTop(OPND);
}
void main()
{
printf("请输入算术表达式,以#号结束:");
while(1)
{
printf("表达式的值为:%d"n"n",EvaluateExpression());
getchar();
printf("请输入算术表达式,以#号结束:");
}
}
#include"stdio.h"
#include"stdlib.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define ERROR 0
typedef int ElemType;
typedef int OpersndType;
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}Stack;
void InitStack(Stack &S)
{
S.base=(ElemType *)malloc(STACK_INIT_SIZE *sizeof(ElemType));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return;
}
void Push(Stack &S,ElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return;
}
int Pop(Stack &S,ElemType &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return TRUE;
}
ElemType GetTop(Stack &S)
{
if(S.top==S.base) return 0;
return *(S.top-1);
}
void ShowStack(Stack S)
{
ElemType *p=S.base;
while(p!=S.top)
printf("%d",*p++);
return;
}
char Precede(char c1,char c2)
{
int i=0,j=0,t,k;
int a[7][7]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}};
switch(c1)
{
case '+': t=0;break;
case '-': t=1;break;
case '*': t=2;break;
case '/': t=3;break;
case '(': t=4;break;
case ')': t=5;break;
case '#': t=6;break;
}
switch(c2)
{
case '+': k=0;break;
case '-': k=1;break;
case '*': k=2;break;
case '/': k=3;break;
case '(': k=4;break;
case ')': k=5;break;
case '#': k=6;break;
}
return a[t][k];
}
int Operate(int a,char m,int b)
{
switch(m)
{
case '+': return a+b;break;
case '-': return a-b;break;
case '*': return a*b;break;
case '/': return a/b;
}
return 0;
}
OpersndType EvaluateExpression()
{
char c,s[100];
int a,b,i,sum,m;
Stack OPTR,OPND;
InitStack(OPTR);
InitStack(OPND);
Push(OPTR,'#');
c=getchar();
while(c!='#'||GetTop(OPTR)!='#')
{
if(c>='0'&&c<='9')
{
for(i=0;c>='0'&&c<='9';i++)
{
s[i]=c;
c=getchar();
}
s[i]='\0';
sum=atoi(s);
Push(OPND,sum);
}
else
switch(Precede(GetTop(OPTR),c))
{
case '<': Push(OPTR,c);
c=getchar();
break;
case '=': Pop(OPTR,m);
c=getchar();
break;
case '>': Pop(OPTR,m);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,m,b));
break;
}
}
return GetTop(OPND);
}
void main()
{
printf("请输入算术表达式,以#号结束:");
while(1)
{
printf("表达式的值为:%d\n\n",EvaluateExpression());
getchar();
printf("请输入算术表达式,以#号结束:");
}
}

浙公网安备 33010602011771号