【栈】简单计算器

https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/K

【Accepted】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<stack>
 9 #include<map>
10 using namespace std;
11 const int maxn=2e2+3;
12 const double eps=1e-6;
13 char str[maxn];
14 
15 int main()
16 {
17     double n;
18     char ch;
19     char op[3];
20     while(~scanf("%lf%c",&n,&ch))
21     {
22         if(n==0.0&&ch=='
')
23         {
24             break;
25         }
26         stack<double> num;
27         num.push(n);
28         while(scanf("%s%lf",op,&n))
29         {
30             if(op[0]=='+')
31             {
32                 num.push(n);
33             }
34             else if(op[0]=='-')
35             {
36                 num.push(-n);
37             }
38             else if(op[0]=='*')
39             {
40                 double tmp=num.top()*n;
41                 num.pop();
42                 num.push(tmp);
43             }
44             else if(op[0]=='/')
45             {
46                 double tmp=num.top()/n;
47                 num.pop();
48                 num.push(tmp);
49             }
50             ch=getchar();
51             if(ch=='
')
52                 break;
53         }
54         double ans=0.0;
55         while(!num.empty())
56         {
57             ans+=num.top();
58             num.pop();
59         }
60         printf("%.2f
",ans);
61     }
62     return 0;
63 }
View Code
原文地址:https://www.cnblogs.com/itcsl/p/7260161.html