#include <iostream>
#include <queue>
#include <stack>
#include <string>
using namespace Std;
string input;
Queue<string> Q;
stack<int> operand;
Stack<string> symbol;
BOOL Issymbol (string s)
{
if (s== "+" | | s== "-" | | s== "*" | | s== "/" | | s== "(" | | s== ")")
return true;
Else
return false;
}
int utility (int a,int b,string t)
{
if (t== "+")
return a+b;
else if (t== "-")
return a-b;
else if (t== "*")
return a*b;
else if (t== "/")
return a/b;
}
BOOL Ismorepriority (string left,string right)
{
if (left== "(")
return false;
else if (right== "*" | | right== "/") && (left== "-" | | left== "+"))
return false;
Else
return true;
}
void Split ()
{
int s=input.size (), t=s;
do{
S=input.size ();
for (int i=0;i!=input.size (); ++i)
{
if (input[i]== ')
{
S=i;
while (I<input.size () &&input[i]== ') i++;
t=i;
Break
}
}
if (S<input.size () &&t<input.size ())
Input=input.substr (0,s) +input.substr (T,input.size ()-T);
else if (s<input.size ())
{
Input=input.substr (0,s);
}
}
while (S<input.size () &&t<input.size ());
s=0;
while (S<input.size ())
{
T=s;
while (S<input.size () &&! Issymbol (String (1,input[s]))
s++;
if (S<input.size ())
{
if (s!=t)
{
String Temp=input.substr (T,S-T);
Q.push (temp);
}
Q.push (String (1,input[s]));
s++;
}
Else
{
Q.push (Input.substr (t,s-t));
}
}
}
int calculate ()
{
while (!q.empty ())
{
String T=q.front (); Q.pop ();
if (! Issymbol (t))
{
Operand.push (Atoi (T.c_str ()));
}
Else
{
if (Symbol.empty () | | t== "(")
Symbol.push (t);
Else
{
if (t!= ")")
{
while (!symbol.empty () && ismorepriority (Symbol.top (), T))
{
int t1=operand.top (); Operand.pop ();
int t2=operand.top (); Operand.pop ();
String Temp=symbol.top (); Symbol.pop ();
T1=utility (t2,t1,temp);
Operand.push (t1);
}
Symbol.push (t);
}
Else
{
while (Symbol.top ()!= "(")
{
int t1=operand.top (); Operand.pop ();
int t2=operand.top (); Operand.pop ();
String Temp=symbol.top (); Symbol.pop ();
T1=utility (t2,t1,temp);
Operand.push (t1);
}
Symbol.pop ();
}
}
}
}
while (!symbol.empty ())
{
int t1=operand.top (); Operand.pop ();
int t2=operand.top (); Operand.pop ();
String Temp=symbol.top (); Symbol.pop ();
T1=utility (t2,t1,temp);
Operand.push (t1);
}
return Operand.top ();
}
void Main ()
{
Label
cout<< "Please enter an expression: \ n";
Getline (Cin,input);
Split ();
Cout<<calculate ();
Goto label;
:: System ("pause");
}