-
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 <iostream>#include<stack>#include<iomanip>#include<string.h>using namespacestd;Const intmaxn=205;CharBUF[MAXN];BOOLIsnum (Charch) { return '0'<=ch&&ch<='9';}intPriorityCharch) { Switch(CH) { Case '+':return 1; Case '-':return 1; Case '*':return 2; Case '/':return 2; }}DoubleCalCharChDoubleXDoubley) { Switch(CH) { Case '+':returnx+y; Case '-':returnX-y; Case '*':returnx*y; Case '/':returnx/X; }}intMain () { while(Cin.getline (BUF,MAXN)) {if(strlen (buf) = =1&&buf[0]=='0') Break; Stack<Double>it; Stack<Char>op; Doublex=0; for(intI=0; buf[i];i++) { if(buf[i]==' ')Continue; if(Isnum (Buf[i])) {x*=Ten; X+ = (buf[i]-'0'); } Else{it.push (x); X=0; if(Op.empty ()) {Op.push (buf[i]); } Else { Charnow=Buf[i]; Chartp=Op.top (); intpn=Priority (now); intpt=Priority (TP); if(pn>PT) {Op.push (now); } Else { Do{TP=op.top (); Op.pop (); Doubley=it.top (); It.pop (); Doublex=it.top (); It.pop (); Doubleres=cal (Tp,x,y); It.push (RES); if(Op.empty ()) Break; PT=Priority (Op.top ()); } while(pn<=PT); Op.push (now); }}}} It.push (x); while(!Op.empty ()) { Charnow=op.top (); Op.pop (); Doubley=it.top (); It.pop (); Doublex=it.top (); It.pop (); Doubleres=cal (Now,x,y); It.push (RES); } Doubleres=It.top (); /*C + + output two-bit decimals*/COUT.SETF (Ios::showpoint); Cout.precision (2); COUT.SETF (iOS::fixed); cout<<res<<Endl; } return 0;}
2006 Zhejiang University: Simple Calculator