支持 + - * / ( )符号,每个数必须是0~9,输入时以#符号结尾
#include <iostream>
#include <stdio.h>
#include <string>
#include <stack>
using namespace std;
int main()
{
string ss;
while(cin>>ss)
{
stack<char> s1;
stack<char> s2;
stack<int> s3;
stack<char> temp;
temp.push('#');
int j, i;
j = ss.length();
for (i = 0; i < j; i++)
{
char c;
c = ss[i];
if ((c >= '0') && (c <= '9'))
s1.push(c);
else
{
if ((temp.top() == '#') || (c == '(') || (temp.top() == '('))
{
temp.push(c);
continue;
}
if (((temp.top() == '+') || (temp.top() == '-')) && ((c == '*') || (c == '/')))
{
temp.push(c);
continue;
}
if (((temp.top() == '+') || (temp.top() == '-')) && ((c == '+') || (c == '-')))
{
s1.push(temp.top());
temp.pop();
temp.push(c);
continue;
}
if (((temp.top() == '*') || (temp.top() == '/')) && ((c == '+') || (c == '-')))
{
s1.push(temp.top());
temp.pop();
temp.push(c);
continue;
}
if (((temp.top() == '*') || (temp.top() == '/')) && ((c == '*') || (c == '/')))
{
s1.push(temp.top());
temp.pop();
temp.push(c);
continue;
}
if (c == ')')
{
while (1)
{
s1.push(temp.top());
temp.pop();
if (temp.top() == '(')
{
temp.pop();
break;
}
}
continue;
}
if (c == '#')
{
while (1)
{
//printf("hello");
s1.push(temp.top());
temp.pop();
if (temp.top() == '#')
{
break;
}
}
break;
}
}
}
j = s1.size();
for (i = 0; i < j; i++)
{
char cc;
cc = s1.top();
s2.push(cc);
s1.pop();
}
j = s2.size();
for (i = 0; i < j; i++)
{
char cc;
cc = s2.top();
s2.pop();
if ((cc >= '0') && (cc <= '9'))
{
int a;
a = cc - 48;
s3.push(a);
}
else
{
int b, c, d;
c = s3.top();
s3.pop();
b = s3.top();
s3.pop();
if (cc == '+')
d = b + c;
if (cc == '-')
d = b - c;
if (cc == '*')
d = b * c;
if (cc == '/')
d = b / c;
s3.push(d);
}
}
printf("%d\n", s3.top());
}
return 0;
}