Simple expression evaluation, a a long time, pit.
The AC code:
#include <iostream> #include <cstdio> #include <cstring> #include <stack>using namespace std; int main () {char str, c;double A, b;stack <double> num;while (scanf ("%lf", &a)! = EOF) {while (!num.empty ()) Num.pop (); c = GetChar (); if (c = = ' \ n ' && a = = 0) Break;num.push (a); scanf ("%c", &c); while (scanf ("%lf", &b)) {if (c = = ' * ') //Priority high, take number, calculate {a = Num.top (); Num.pop (); Num.push (A * b);} else if (c = = '/') //Ibid. {a = Num.top (); Num.pop (); Num.push (A/b);} else if (c = = ' + ') //Low priority, first deposit to stack Num.push (b); else if (c = = '-') //subtraction to add that number of negative num.push (-b); c = GetChar (); if (c = = ' \ n ') //End break;scanf ("%c", &c); GetChar ();} Double ans = 0.0;while (!num.empty ()) {ans + = num.top (); Num.pop (); printf ("%.2lf\n", ans);} return 0;}
Hangzhou Electric acm1237--Simple Calculator