-
Title Description:
-
reads a non-negative integer that contains only a +,-, *,/, evaluates the expression, and computes the value of the expression.
-
Input:
The
-
test input contains several test cases, one row per 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:
-
Output 1 rows for each test case, that is, the value of the expression, exactly 2 digits after the decimal point.
-
Sample input:
-
1 + 24 + 2 * 5-7/110
-
-
Sample output:
-
3.0013.36
#include <cstdio>#include<cstring>#include<algorithm>#include<stack>using namespacestd;Const intmaxn=1005;CharS[MAXN];BOOLIsOp (Charop) { if(op=='+'|| op=='-'|| op=='*'|| op=='/') return true; Else return false;}intPriority (Charop) { Switch(OP) { Case '+':return 1; Case '-':return 1; Case '*':return 2; Case '/':return 2; }}DoubleCalDoubleXDoubleYCharop) { Switch(OP) { Case '+':returnx+y; Case '-':returnX-y; Case '*':returnx*y; Case '/':returnx/y; }}CharSS[MAXN];inttop;intMain () { while(Gets (s) &&strcmp (s),"0")!=0) {Top=0; Stack<Char>op; Stack<Double>it; intlen=strlen (s); for(intI=0; i<len;i++) { if(s[i]==' ')Continue; Ss[top++]=S[i]; } intE=0; for(intI=0; i<top;i++) { if(IsOp (Ss[i])) {It.push (Double(e)); E=0; if(Op.empty ()) {Op.push (ss[i]); } Else { intpri1=Priority (Ss[i]); intPri2=Priority (Op.top ()); if(pri1>pri2) {Op.push (ss[i]); } Else { Do{ Doublex=it.top (); It.pop (); Doubley=it.top (); It.pop (); Doubleres=Cal (Y,x,op.top ()); Op.pop (); It.push (RES); } while(!op.empty () &&pri1<=Priority (Op.top ())); Op.push (Ss[i]); } } } Else{e*=Ten; E=e+ss[i]-'0'; }} it.push (Double(e)); while(It.size () >1) { Doublex=it.top (); It.pop (); Doubley=it.top (); It.pop (); Doubleres=Cal (Y,x,op.top ()); Op.pop (); It.push (RES); } Doubleres=It.top (); printf ("%.2lf\n", RES); } return 0;}
2006 Zhejiang University: Simple Calculator