#include <iostream>
#include <math.h>
#define maxSize 100
#define MIN 0.000001
using namespace std;
int calsub(float opond1,char op,float opond2,float &result)
{
if(op=='+') result=opond1+opond2;
else if(op=='-') result=opond1-opond2;
else if(op=='*') result=opond1*opond2;
else if(op=='/')
{
if(fabs(opond2)<MIN)
return 0;
else
result=opond1/opond2;
}
return 1;
}
int getpriority(char op)
{
if(op=='+' || op=='-')
return 0;
else
return 1;
}
int calStackTopTwo(float s1[],int &top1,char s2[],int &top2)
{
float opnd1;
float opnd2;
float result;
char op;
int flag;
opnd2=s1[top1--];
opnd1=s1[top1--];
op=s2[top2--];
flag=calsub(opnd1,op,opnd2,result);
if(flag==0)
cout<<"error";
else
s1[++top1]=result;
}
float calinfix(char infix[])
{
float s1[maxSize]; int top1=-1;
char s2[maxSize]; int top2=-1;
int i=0;
while(infix[i]!='\0')
{
if('0'<=infix[i]&&infix[i]<='9')
{
s1[++top1]=infix[i]-'0';
++i;
}
else if(infix[i]=='(')
{
s2[++top2]='(';
++i;
}
else if(infix[i]=='+' ||
infix[i]=='-' ||
infix[i]=='*' ||
infix[i]=='/')
{
if(top2==-1 || s2[top2]=='(' || getpriority(infix[i])>getpriority(s2[top2]))
{
s2[++top2]=infix[i];
++i;
}
else
{
calStackTopTwo(s1,top1,s2,top2);
}
}
else if(infix[i]==')')
{
while(s2[top2]!='(')
{
calStackTopTwo(s1,top1,s2,top2);
}
}
}
while(top2!='0')
{
calStackTopTwo(s1,top1,s2,top2);
}
return s1[top1];
}
int main()
{
char test[maxSize]={'(','1','+','1',')','/','3','\0'};
cout<<calinfix(test);
return 0;
}