简单计算器

来源:互联网 发布:淘宝手机助手集分宝 编辑:程序博客网 时间:2024/05/06 20:27

http://acm.hdu.edu.cn/showproblem.php?pid=1237


#include <iostream>#include <cstdio>#include <cstring>#include <stack>using namespace std;int main(){//freopen("test.txt", "r", stdin);char str, c;double a, b;stack <double> num;while (scanf("%lf", &a) != EOF){while (!num.empty())num.pop();c = getchar();if (c == '\n' && a == 0)break;num.push(a);scanf("%c", &c);while (scanf("%lf", &b)){if (c == '*')                //优先级高,取数,计算{a = num.top();num.pop();num.push(a * b);}else if (c == '/')           //同上{a = num.top();num.pop();num.push(a / b);}else if (c == '+')           //优先级低,先存入栈num.push(b);else if (c == '-')          //减法变成加上那个数的负num.push(-b);c = getchar();if (c == '\n')               //结尾break;scanf("%c", &c);getchar();}double ans = 0.0;while (!num.empty()){ans += num.top();num.pop();}printf("%.2lf\n", ans);}return 0;}


0 0