Problem Description reads a non-negative integer that contains only +,-, *,/, evaluates the expression, and computes the value of the formula.
The input test inputs contain several test cases, one row for each test case, and no more than 200 characters per line, separated by a space between integers and operators. There is no illegal expression. When only 0 o'clock input is completed in a row, the corresponding result is not output.
Output outputs 1 rows for each test case, that is, the value of the expression, exactly 2 digits after the decimal point.
Sample Input
1 + 2 4 + 2 * 5-7/11 0
Sample Output
3.00 13.36
Continue stack
#include <stdio.h> #include <string.h> #include <stack> using namespace std;
int main () {int i;
Double A, B;
Char s[250],c;
while (gets (s), strcmp (S, "0")!=0) {stack<char>s1;
stack<double>s2;
for (i=0; s[i]; i++) {if (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {a=0;
while (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {a=a*10+s[i]-' 0 ';
i++;
} i--;
S2.push (a); } else if (s[i]== '-' | | |
s[i]== ' + ') {if (!s1.empty ()) {c=s1.top ();
S1.pop ();
A=s2.top ();
S2.pop ();
B=s2.top ();
S2.pop ();
if (c== ' + ') a+=b; else a=B-a;
S2.push (a);
S1.push (S[i]);
} else S1.push (S[i]);
} else if (s[i]== '/' | | s[i] = = ' * ') {char ch = s[i];
b=0;
i+=2;
while (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {b=b*10+s[i]-' 0 ';
i++;
} i--;
A=s2.top ();
S2.pop ();
if (ch = = '/') a=a/b;
else A = a*b;
S2.push (a);
}} while (!s1.empty ()) {c=s1.top ();
S1.pop ();
A=s2.top ();
S2.pop ();
B=s2.top ();
S2.pop ();
if (c== ' + ') a+=b;
else a=b-a;
S2.push (a); } printf ("%.2f\n", s2.toP ());
} return 0;
}