#include <iostream> #include<bits/stdc++.h> #define max 100 using namespace std; typedef struct{ char *top; char *base; int size; } node;
int input(char ch); char operate(char a,char fu,char b); char precede(char ch1,char ch2); char gettop(node s); void pop(node &s); void push(node &s,char ch); void initstack(node &s); int main() { node shu,fuhao; char ch; initstack(shu); initstack(fuhao); push(fuhao,'#'); //cin>>ch; ch=getchar(); while(ch!='#'||gettop(fuhao)!='#'){ if(input(ch)) {push(shu,ch); //cout<<gettop(shu); // cin>>ch; ch=getchar(); } else{ switch(precede(gettop(fuhao),ch)){ case '<':push(fuhao,ch); //cin>>ch; ch=getchar(); break;
case '=':pop(fuhao); //cin>>ch; ch=getchar(); break;
case '>': char ch1=gettop(shu);pop(shu); char ch2=gettop(fuhao);pop(fuhao); char ch3=gettop(shu);pop(shu); char ch4=operate(ch3,ch2,ch1);//here push(shu,ch4); break; } } } cout << (int)(gettop(shu)-48) << endl; return 0; } int input(char ch){//判断输入的是数字还是运算符 if(ch>='0'&&ch<='9') return 1; else return 0;
}
char operate(char a,char fu,char b){ int a1,b1; int result; a1=a-48; b1=b-48;
switch (fu){
case '+':result=a1+b1;break; case '-':result=a1-b1;break; case '*':result=a1*b1;break; case '/':result=a1/b1;break; }
return result+48;
}
char precede(char ch1,char ch2){//运算符优先级比较 int i,j; char a[7][7]= { {'>','>','<','<','<','>','>'}, {'>','>','<','<','<','>','>'}, {'>','>','>','>','<','>','>'}, {'>','>','>','>','<','>','>'}, {'<','<','<','<','<','=','0'}, {'>','>','>','>','0','>','>'}, {'<','<','<','<','<','0','='}
};
switch(ch1){ case '+':i=0;break; case '-':i=1;break; case '*':i=2;break; case '/':i=3;break; case '(':i=4;break; case ')':i=5;break; case '#':i=6;break; }
switch(ch2){ case '+':j=0;break; case '-':j=1;break; case '*':j=2;break; case '/':j=3;break; case '(':j=4;break; case ')':j=5;break; case '#':j=6;break; }
return a[i][j];
}
//void isempty(node &s){} char gettop(node s){ //s.top--; //return *s.top ; return *(s.top-1); } void pop(node &s){ if(s.top==s.base) exit(0); s.top--; }
void push(node &s,char ch){ if(s.top-s.base==s.size) exit(0); *s.top=ch; s.top++; } void initstack(node &s){
s.base=new char[max]; s.top=s.base; s.size=max;
}