标签:stack
1 + 2 4 + 2 * 5 - 7 / 11 0
3.00 13.36
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define eps 1e-8 using namespace std; #define N 10005 int main() { int i; char c,cc; double a,b; while(~scanf("%lf%c",&a,&c)) { if(fabs(a-0)<eps&&c=='\n') break; if(c=='\n') { printf("%.2lf\n",a); continue; } stack<double>q; q.push(a); scanf("%c%lf",&c,&a); if(c=='+') q.push(a); else if(c=='-') q.push(a*(-1)); else { double cur=q.top(); q.pop(); if(c=='*') q.push(cur*a); else q.push(cur/a); } while(scanf("%c",&c)) { if(c=='\n') break; scanf("%c%lf",&c,&a) ; if(c=='+') q.push(a); else if(c=='-') q.push(a*(-1)); else { double cur=q.top(); q.pop(); if(c=='*') q.push(cur*a); else q.push(cur/a); } } double ans=0; while(!q.empty()) { ans+=q.top(); q.pop(); } printf("%.2lf\n",ans); } return 0; }
标签:stack
原文地址:http://blog.csdn.net/u014737310/article/details/40661439