Calculator(后缀表达式)

【题目要求】

Implement a calculator program based on a Reverse Polish notation.

【输入】

There could be several lines. Each line contains a Reverse Polish notation. The operators and operands are sepertated by spaces( there could be more than 1 spaces between input tokens).

1 2 3 - + *
1 -
2 0 /
-
 1  2  + 
2.2 3.25 1.1 2.5 / - 1.32 * +

【输出】

Output result of each test case and keep 4 numbers after the decimal point.
Output ‘ERROR’ if there are errors of the input test case.

ERROR
ERROR
ERROR
ERROR
3.0000
5.9092

【我的程序】

#include <stdio.h>
#include <iostream>
#include <iomanip>
#define maxSize 500
using namespace std;

class myCal
{
    private:
        int depth;
        double a[maxSize];

    public:
        myCal() { depth=-1; }

        void init() { depth=-1; }
        int getDepth() { return depth; }
        double getData() { return a[depth]; }

        void myPush(double t) { a[++depth]=t; }
        int myPlus() { if (depth<1) return 1; a[depth-1]+=a[depth]; depth--; return 0; }
        int myMinus() { if (depth<1) return 1; a[depth-1]-=a[depth]; depth--; return 0; }
        int myMultiple() { if (depth<1) return 1; a[depth-1]*=a[depth]; depth--; return 0; }
        int myDivide() { if (depth<1) return 1; if (a[depth]==0) return 1; a[depth-1]/=a[depth]; depth--; return 0; }
};

int main()
{
    char ch;
    double t;
    int flag=0;
    myCal my;

    cout << fixed << setprecision(4);
    do
    {
        ch=cin.get();

        if (ch=='\n')
        {
            if (flag==0 && my.getDepth()==0) cout<< my.getData() << endl;
            else cout<< "ERROR" << endl;
            flag=0;
            my.init();
        }

        if ((ch<='9' && ch>='0') || ch=='.') { cin.putback(ch); cin>>t; my.myPush(t); }

        if (ch=='+' && flag==0) flag=my.myPlus();
        if (ch=='-' && flag==0) flag=my.myMinus();
        if (ch=='*' && flag==0) flag=my.myMultiple();
        if (ch=='/' && flag==0) flag=my.myDivide();

    }while (ch!=EOF);
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
表达式后缀表达式是一种常见的算法,它可以将表达式转换为后缀表达式,使得计算更加方便。后缀表达式也叫做逆波兰表达式,它的计算方式是从左到右扫描表达式,遇到数字就压入,遇到运算符就从弹出两个数字进行计算,并将计算结果压入,最终只剩下一个数字,即为表达式的计算结果。 下面是一个C++的后缀表达式计算类的定义,其包括了后缀表达式的计算方法和一些辅助方法: ``` #include <stack> #include <string> #include <sstream> class PostfixCalculator { public: PostfixCalculator() {} double calculate(std::string postfix) { std::stack<double> s; std::stringstream ss(postfix); std::string token; while (std::getline(ss, token, ' ')) { if (isOperator(token)) { double operand2 = s.top(); s.pop(); double operand1 = s.top(); s.pop(); double result = applyOperator(token, operand1, operand2); s.push(result); } else { double operand = std::stod(token); s.push(operand); } } return s.top(); } private: bool isOperator(std::string token) { return token == "+" || token == "-" || token == "*" || token == "/"; } double applyOperator(std::string op, double operand1, double operand2) { if (op == "+") { return operand1 + operand2; } else if (op == "-") { return operand1 - operand2; } else if (op == "*") { return operand1 * operand2; } else if (op == "/") { return operand1 / operand2; } return 0; } }; ``` 使用该类可以方便地计算后缀表达式的值,例如: ``` PostfixCalculator calculator; double result = calculator.calculate("1 2 3 - 4 * + 10 5 / +"); ``` 其后缀表达式为"1 2 3 - 4 * + 10 5 / +",计算结果为11.0。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值