#include
#include
#define maxlen 40
using namespace std;
enum error_code{succes,underflow,overflow};
char opr[]={'+','-','*','/','(',')','#'};
int comp[7][7]=
{
{2,2,1,1,1,2,2},
{2,2,1,1,1,2,2},
{2,2,2,2,1,2,2},
{2,2,2,2,1,2,2},
{1,1,1,1,1,3,0},
{2,2,2,2,0,2,2},
{1,1,1,1,1,0,3}};
class stack{
public:
stack();
bool empty() const;
bool full() const;
error_code get_top0(char &x);
error_code push0(const char x);
error_code get_top1(int &x);
error_code push1(const int x);
error_code pop0(char &op);
error_code pop1(int &a);
private:
int count;
char data0[maxlen];
int data1[maxlen];
};
stack::stack() {count=0;}
bool stack::empty()const{
if(count==0) return true;
else return false;
}
bool stack::full() const{
if(count==maxlen) return true;
else return false;
}
error_code stack::get_top0(char &x) {
if(empty()) return underflow;
else{
x=data0[count-1];
return succes;
}
}
error_code stack::get_top1(int &x) {
if(empty()) return underflow;
else{
x=data1[count-1];
return succes;
}
}
error_code stack::push0(const char x){
if(full()) return overflow;
else{
data0[count]=x;
count++;
return succes;
}
}
error_code stack::push1(const int x){
if(full()) return overflow;
else{
data1[count]=x;
count++;
return succes;
}
}
error_code stack::pop0(char&op){
if(empty()) return underflow;
else{
op=data0[count-1];
count--;
return succes;}
}
error_code stack::pop1(int&a){
if(empty()) return underflow;
else{
a=data1[count-1];
count--;
return succes;}
}
char cp(char ch1,char ch2)
{
int i,m,n;
char prv;
int prio;
for(i=0;i
{
if(ch1==opr[i])
m=i;
if(ch2==opr[i])
n=i;
}
prio=comp[m][n];
switch(prio){
case 2:
prv='>';
break;
case 3:
prv='=';
break;
case 1:
prv='<';
break;
case 0:
prv='@';
cout<<"wrong."<<endl;
break;
}
return prv;
}
int js(int a,char op,int b){
int result;
switch(op){
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
}
return result;
}
bool ifopt(char ch)
{
for(int i=0;i<7;i++)
{
if(ch==opr[i])
return true;
}
return false;
}
int jsq()
{
char op,i=0,*str,x=',';
int a,b,temp,num;
stack operater,object;
operater.push0('#');
cout<<"input and end with the'#'"<<endl;
str=(char*)malloc(40*sizeof(char));
gets(str);
while(str[i]!='#'||x!='#'){
operater.get_top0(x);
if(!ifopt(str[i])){
num=str[i]-'0';
i++;
while(!ifopt(str[i])){
num=num*10+str[i]-'0';
i++;
}
object.push1(num);
}
else{
switch(cp(x,str[i]))
{
case '<':
operater.push0(str[i]);
i++;
break;
case '=':
object.pop0(op);i++;
break;
case '>':
operater.pop0(op);
object.pop1(a);
object.pop1(b);
temp=js(a,op,b);
object.push0(temp);
break;
}
}
}operater.get_top0(x);
object.get_top1(temp);
return temp;
}
int main()
{
int result=jsq();
cout<<result<<endl;
return 0;
}