RPN计算器 - C++实现

功能概述

基本运算

支持加减乘除(+、-、*、/)四则运算,自动处理运算优先级

高级功能

包含平方根(sqrt)、幂运算(^)、斐波那契数列(fib)、杨辉三角行和(pascal)

交互模式

提供交互式操作界面,支持命令行控制(clear、show、history等)

批量计算

支持批量处理多个RPN表达式,自动展示每个表达式的计算结果

历史记录

自动保存计算历史,支持查看和清空历史记录

调试模式

提供调试模式,显示计算过程中的栈操作和中间结果

完整C++代码

#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <sstream>
#include <cmath>
#include <stdexcept>
#include <map>
#include <iomanip>

class RPNCalculator {
private:
    std::stack<double> operands;
    std::vector<std::string> history;
    bool debugMode;

    // 检查是否为运算符
    bool isOperator(const std::string& token) {
        return token == "+" || token == "-" || token == "*" || token == "/" ||
            token == "sqrt" || token == "^" || token == "fib" || token == "pascal";
    }

    // 检查是否为数字
    bool isNumber(const std::string& token) {
        std::istringstream iss(token);
        double value;
        iss >> value;
        return !iss.fail() && iss.eof();
    }

    // 处理四则运算
    void handleBasicOperation(const std::string& op) {
        if (operands.size() < 2) {
            throw std::runtime_error("错误: 栈中操作数不足");
        }

        double right = operands.top(); operands.pop();
        double left = operands.top(); operands.pop();
        double result;

        if (op == "+") result = left + right;
        else if (op == "-") result = left - right;
        else if (op == "*") result = left * right;
        else if (op == "/") {
            if (right == 0) throw std::runtime_error("错误: 除零错误");
            result = left / right;
        }

        operands.push(result);
        if (debugMode) {
            std::cout << "计算: " << left << " " << op << " " << right << " = " << result << std::endl;
        }
    }

    // 计算斐波那契数列
    double fibonacci(int n) {
        if (n < 0) throw std::runtime_error("错误: 斐波那契数列参数必须为非负整数");
        if (n == 0) return 0;
        if (n == 1) return 1;

        double a = 0, b = 1;
        for (int i = 2; i <= n; i++) {
            double temp = b;
            b = a + b;
            a = temp;
        }
        return b;
    }

    // 计算杨辉三角指定行和
    double pascalTriangle(int n) {
        if (n < 0) throw std::runtime_error("错误: 杨辉三角行数必须为非负整数");

        double sum = 0;
        double value = 1;
        for (int k = 0; k <= n; k++) {
            sum += value;
            value = value * (n - k) / (k + 1);
        }
        return sum;
    }

public:
    RPNCalculator() : debugMode(false) {}

    // 设置调试模式
    void setDebugMode(bool mode) {
        debugMode = mode;
    }

    // 清空栈
    void clearStack() {
        while (!operands.empty()) {
            operands.pop();
        }
    }

    // 显示当前栈内容
    void displayStack() {
        if (operands.empty()) {
            std::cout << "栈为空" << std::endl;
            return;
        }

        std::stack<double> temp = operands;
        std::vector<double> elements;

        std::cout << "当前栈内容 (从顶到底): ";
        while (!temp.empty()) {
            elements.push_back(temp.top());
            temp.pop();
        }

        for (auto it = elements.rbegin(); it != elements.rend(); ++it) {
            std::cout << *it << " ";
        }
        std::cout << std::endl;
    }

    // 执行单个RPN表达式
    double evaluateExpression(const std::string& expression) {
        std::istringstream iss(expression);
        std::string token;
        std::vector<std::string> tokens;

        // 分割表达式为标记
        while (iss >> token) {
            tokens.push_back(token);
        }

        for (const auto& t : tokens) {
            if (isNumber(t)) {
                double value = std::stod(t);
                operands.push(value);
                if (debugMode) {
                    std::cout << "压入数字: " << value << std::endl;
                }
            }
            else if (isOperator(t)) {
                if (t == "sqrt") {
                    if (operands.empty()) throw std::runtime_error("错误: 栈为空");