标签:
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <cstring> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <map> #define PI acos(-1.0) #define ms(a) memset(a,0,sizeof(a)) #define msp memset(mp,0,sizeof(mp)) #define msv memset(vis,0,sizeof(vis)) using namespace std; //#define LOCAL int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL ios::sync_with_stdio(false); string s; bool flag; while(getline(cin,s)) { if(s.size()==1)break; flag=1;//numric stringstream ss(s); double num; char op=‘#‘; stack<char> sop; stack<double> snum; while(!sop.empty())sop.pop(); while(!snum.empty())snum.pop(); while(1) { if(flag) { ss>>num; if(op==‘*‘||op==‘/‘) { double num1=snum.top(); snum.pop(); if(op==‘*‘)num1=num*num1; else num1=num1/num; snum.push(num1); } else snum.push(num); flag=0; } else { if(ss>>op); else break; if(op==‘+‘||op==‘-‘) { double num1,num2; char c; if(!sop.empty()) { c=sop.top(),sop.pop(); num2=snum.top(),snum.pop(); num1=snum.top(),snum.pop(); if(c==‘+‘)num1+=num2; else num1-=num2; snum.push(num1); } sop.push(op); } flag=1; } } while(!sop.empty()) { op=sop.top(),sop.pop(); double num1,num2; num2=snum.top(),snum.pop(); num1=snum.top(),snum.pop(); if(op==‘+‘)num1+=num2; else num1-=num2; snum.push(num1); } printf("%.2lf\n",snum.top()); } return 0; }
标签:
原文地址:http://www.cnblogs.com/gpsx/p/5183214.html