#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
#define MAXSIZE 100
const char oper[7]={'+','-','*','/','(',')','#'};
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S){ //栈的初始化
S.base=new ElemType[MAXSIZE];
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
Status Push(SqStack &S,ElemType e){
if(S.top-S.base==S.stacksize) return ERROR;
*S.top++=e;
return 0;
}
ElemType GetTop (SqStack &S){
if(S.top!=S.base)
return *(S.top-1);
}
Status Pop(SqStack &S,int &e){
if(S.base==S.top) return ERROR;
e=*--S.top;
return OK;
}
bool In(char ch){
for(int i=0;i<7;i++){
if(ch == oper[i])
return true;
}
return false;
}
char Precede(char theta1,char theta2){
if ((theta1 == '(' && theta2 == ')') || (theta1 == '#' && theta2 == '#')) {
return '=';
} else if (theta1 == '(' || theta1 == '#' || theta2 == '(' || (theta1
== '+' || theta1 == '-') && (theta2 == '*' || theta2 == '/')) {
return '<';
} else
return '>';
}
char Operate(int first,char Symbol,int second){
switch(Symbol){
case '+':
return (first)+(second)+48;
case '-':
return (first)-(second)+48;
case '*':
return (first)*(second)+48;
case '/':
return (first)/(second)+48;
}
}
char EvaluateExpression(){
int x,num=0;
char c;
SqStack OPND,OPTR;
if(InitStack(OPND) && InitStack(OPTR))
cout<<"初始化成功!";
Push(OPTR,'#');
cout<<"请输入表达式并用#结尾";
c=getchar();
while(c!='#' || GetTop(OPTR)!='#'){
if(c>='0' && c<='9'){
num=num*10+(c-'0');
c=getchar();
}
else if(In(c)){
if(num!=0){
Push(OPND,num);
num=0;
}
switch(Precede(GetTop(OPTR),c)){
case'<':
Push(OPTR,c);
c=getchar();
break;
case '=':
Pop(OPTR,x);
c=getchar();
break;
case'>':
Pop(OPTR,x);
int a,b;
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,x,b));
break;
}
}
}
return GetTop(OPND);
}
int main(){
int s;
s=EvaluateExpression();
cout<<s-'0';
system("pause");
return 0;
}