• HDU Problem 简单计算器【栈】


    简单计算器

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 18056    Accepted Submission(s): 6353

    Problem Description
    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
     
    Input
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
     
    Output
    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
     
    Sample Input
    1 + 2 4 + 2 * 5 - 7 / 11 0
     
    Sample Output
    3.00 13.36
     
    Source
     
    Recommend
    JGShining   |   We have carefully selected several similar problems for you:  1230 1235 1231 1228 1248 
     
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define MAX_N 1005
    using namespace std;
    const int INF = 0xffff;
    
    int main() {
        double n, temp;
        char b, c[2];
        while (scanf("%lf%c", &n, &b)) {
            if (n == 0 && b == '
    ') break;
            double ans = 0;
            stack<double> stk;
            stk.push(n);
            while (scanf("%s %lf", c, &n) != EOF) {
                if (c[0] == '+')   stk.push(n);
                else if (c[0] == '-')  stk.push(-n);
                else if (c[0] == '*') {
                    temp = n*stk.top();
                    stk.pop();
                    stk.push(temp);
                }
                else {
                    temp = stk.top()/n;
                    stk.pop();
                    stk.push(temp);
                }
                if (b = getchar() , b == '
    ') break;
            }
            while (!stk.empty()) {
                ans += stk.top();
                stk.pop();
            }
            printf("%.2lf
    ", ans);
        }
        return 0;
    }

  • 相关阅读:
    c#使用SoundPlayer播放wav格式音频
    c#NAudio 录音功能实现
    c#异步方法调用
    c# 读取文件目录下的信息
    angular笔记_1
    js获取form元素,不使用id
    事物回滚机制
    ckplayer跨域调用
    帝国移动pc站文章
    页面切换导航样式也随之改变
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770897.html
Copyright © 2020-2023  润新知