#include
#include
#define OK 1
#define ERROR 0
#define MAXSIZE 100
#define OVERFLOW -2
const char oper[7]={'+','-','*','/','(',')','#'};
using namespace std;
typedef char SElemType;//定义字符类型
typedef int NElemType;//定义int类型
typedef int Status;
typedef struct//字符型栈的存储结构
{
SElemType *base;
SElemType *top;
int stackSize;
}SqStack_optr;
typedef struct//int型栈的存储结构
{
NElemType *base;
NElemType *top;
int stackSize;
}SqStack_opnd;
Status InitStackS(SqStack_optr &S)//字符型栈的初始化
{
S.base = new SElemType[MAXSIZE];
if(!S.base) return OVERFLOW;
S.top=S.base;
S.stackSize = MAXSIZE;
return OK;
}
Status InitStackN(SqStack_opnd &S)//int型栈的初始化
{
S.base = new NElemType[MAXSIZE];
if(!S.base) return OVERFLOW;
S.top=S.base;
S.stackSize = MAXSIZE;
return OK;
}
Status PushS (SqStack_optr &S,SElemType e)//字符型进栈
{
if(S.top-S.base==S.stackSize) return ERROR;
*S.top++=e;
return OK;
}
Status PushN (SqStack_opnd &S,NElemType e)//int型进栈
{
if(S.top-S.base==S.stackSize) return ERROR;
*S.top++=e;
return OK;
}
Status PopS ( SqStack_optr &S, SElemType &e )//字符型栈顶元素退栈
{ if ( S.top==S.base )
return ERROR; // 栈空
e = * -- S.top; // --S.top; e=*S.top;
return OK;
} //Pop
Status PopN ( SqStack_opnd &S, NElemType &e )//int型栈顶元素退栈
{ if ( S.top==S.base )
return ERROR; // 栈空
e = * -- S.top; // --S.top; e=*S.top;
return OK;
} //Pop
SElemType GetTopS ( SqStack_optr S)//字符型取栈顶
{
SElemType e;
if ( S.top==S.base )
return ERROR; //栈空
e = *(S.top-1);
return e;
}
NElemType GetTopN ( SqStack_opnd S)//int型取栈顶
{
NElemType e;
if ( S.top==S.base )
return ERROR; //栈空
e = *(S.top-1);
return e;
}
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 '>';
}
int Operate(int first, char theta, int second)
{//计算两数运算结果
switch (theta)
{
case '+':
return first + second;
case '-':
return first - second;
case '*':
return first * second;
case '/':
return first / second;
}
return 0;
}
EvaluateExpression()
{
char c,theta,x;//c为输入的字符,theta为运算符
int a,b,num;
SqStack_optr OPTR;
SqStack_opnd OPND;
InitStackS(OPTR);//初始化OPTR栈
PushS (OPTR, '#');//将表达式起始符“#”压入OPTR栈
InitStackN(OPND);//初始化OPND栈
cin>>c;
while ( c!='#' || GetTopS(OPTR)!='#' )
{
if (! In (c)) // In(c, OP)判断c是否为运算符
{
num=c-'0';
PushN(OPND, num);
cin>>c;
} //不是运算符则进栈
else
{
switch (Precede(GetTopS(OPTR),c))//判定OPTR的栈顶运算符1与读入的运算符2间的优先关系
{ case '<': // 新输入的算符c优先级高,c进栈
PushS(OPTR, c);
cin>>c;
break;
case '=': // 脱括号并接收下一字符
PopS(OPTR, x);
cin>>c;
break;
case '>': //新输入的算符c优先级低,即栈顶算符优先权高
//出栈并将运算结果入栈OPND
PopS ( OPTR, theta); //弹出OPTR栈顶的运算符
PopN ( OPND, b);
PopN ( OPND, a); //弹出OPND栈顶的两个运算数
PushN ( OPND, Operate(a, theta, b) ); //进行二元运算ab,将运算结果压入OPND栈
break;
} //switch
}//else
} //while
return(GetTopN(OPND));
} //EvaluateExpression
int main()
{
int result;
result=EvaluateExpression();
cout<<resu<<endl;
system("pause");
return 0;
}