zoukankan      html  css  js  c++  java
  • HDU1237 简单计算器【堆栈】

    简单计算器

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    Total Submission(s): 21518    Accepted Submission(s): 7722
    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


    问题链接HDU1237 简单计算器

    问题描述参见上文。

    问题分析这是一个表达式求值问题,可以用递归来处理,也可以用堆栈来处理。

    程序说明:程序中,使用堆栈来处理运算符的优先级,运算符和操作符分别放在两个堆栈中。

    参考链接:(略)



    AC的C++语言程序:

    /* HDU1237 简单计算器 */
    
    #include <iostream>
    #include <string>
    #include <stack>
    #include <cctype>
    #include <cstdio>
    
    using namespace std;
    
    int main()
    {
        string s;
        stack<char> op;
        stack<double> operand;
        double operand1, operand2;
    
        while(getline(cin, s) && s != "0") {
            for(int i=0; s[i]; i++) {
                if(isdigit(s[i])) {
                    operand1 = 0;
                    while(isdigit(s[i])) {
                        operand1 = operand1 * 10 + s[i] - '0';
                        i++;
                    }
                    i--;
                    operand.push(operand1);
                } else if(s[i] == '+' || s[i] == '-') {
                    if(op.empty())
                        op.push(s[i]);
                    else {
                        char sop = op.top();
                        op.pop();
                        operand2 = operand.top();
                        operand.pop();
                        operand1 = operand.top();
                        operand.pop();
                        if(sop == '+')
                            operand.push(operand1 + operand2);
                        else
                            operand.push(operand1 - operand2);
                        op.push(s[i]);
                    }
                } else if(s[i] == '*' || s[i] == '/') {
                    char cop = s[i];
                    i += 2;
                    operand2 = 0;
                    while(isdigit(s[i])) {
                        operand2 = operand2 * 10 + s[i] - '0';
                        i++;
                    }
                    i--;
                    operand1 = operand.top();
                    operand.pop();
                    if(cop == '*')
                        operand.push(operand1 * operand2);
                    else
                        operand.push(operand1 / operand2);
                }
            }
    
            while(!op.empty()) {
                char sop = op.top();
                op.pop();
                operand2 = operand.top();
                operand.pop();
                operand1 = operand.top();
                operand.pop();
                if(sop == '+')
                    operand.push(operand1 + operand2);
                else
                    operand.push(operand1 - operand2);
            }
    
            printf("%.2f
    ", operand.top());
        }
    
        return 0;
    }


  • 相关阅读:
    个人浏览器安装的插件
    angularjs 1 Failed to read the 'selectionStart' property from 'HTMLInputElement':
    git 配置用户名和邮箱
    修改Atom 隐藏.gitignore忽略的文件/文件夹的配置
    Ubuntu 安装php_intl 扩展
    yii2 ./yii command : No such file or directory
    vagrant yii2 Exception 'yiidbException' with message 'SQLSTATE[HY000] [2002]
    [转载]Ubuntu 14.04设置固定ip
    [转载]删除所有的.svn文件夹
    Centos7.2正常启动关闭CDH5.16.1
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563786.html
Copyright © 2011-2022 走看看