C/C++ 实现计算给定的算术表达式字符串

废话不多说,直接上代码:

#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <cctype>
#include <stdexcept>

using namespace std;
bool is_operator(char c) {
    return c == '+' || c == '-' || c == '*' || c == '/';
}
int precedence(char op) {
    if (op == '+' || op == '-') {
        return 1;
    }
    else if (op == '*' || op == '/') {
        return 2;
    }
    else {
        return 0;
    }
}
double apply_operator(double a, double b, char op) {
    switch (op) {
    case '+': return a + b;
    case '-': return a - b;
    case '*': return a * b;
    case '/': return a / b;
    default: throw runtime_error("Invalid operator");
    }
}
double evaluate_expression(const string& expression) {
    stack<double> num_stack;
    stack<char> op_stack;
    for (size_t i = 0; i < expression.length(); ++i) {
        if (expression[i] == ' ') {continue; }
		else if (isdigit(expression[i]) || expression[i] == '.') {
            size_t j = i;
            while (j < expression.length() && (isdigit(expression[j]) || expression[j] == '.')) {
                ++j;
            }
            double number;
            stringstream ss(expression.substr(i, j - i));
            ss >> number;
            num_stack.push(number);
            i = j - 1;
        }
        else if (is_operator(expression[i])) {
            while (!op_stack.empty() && precedence(op_stack.top()) >= precedence(expression[i])) {
                double b = num_stack.top();
                num_stack.pop();
                double a = num_stack.top();
                num_stack.pop();
                char op = op_stack.top();
                op_stack.pop();
                num_stack.push(apply_operator(a, b, op));
            }
            op_stack.push(expression[i]);
        }
		else if (expression[i] == '(') {
            op_stack.push(expression[i]);
        }
        else if (expression[i] == ')') {
            while (!op_stack.empty() && op_stack.top() != '(') {
                double b = num_stack.top();
                num_stack.pop();
                double a = num_stack.top();
                num_stack.pop();
                char op = op_stack.top();
                op_stack.pop();
                num_stack.push(apply_operator(a, b, op));
            }
            if (op_stack.empty() || op_stack.top() != '(') {
                throw runtime_error("Mismatched parentheses");
            }
            op_stack.pop();
        }
        else {
            throw runtime_error("Invalid character");
        }
    }
    while (!op_stack.empty()) {
        double b = num_stack.top();
        num_stack.pop();
        double a = num_stack.top();
        num_stack.pop();
        char op = op_stack.top();
        op_stack.pop();
        num_stack.push(apply_operator(a, b, op));
    }
    if (num_stack.size() != 1) {
        throw runtime_error("Invalid expression");
    }

    return num_stack.top();
}
int main() {
    string expression;
    getline(cin, expression);
    try {
        double result = evaluate_expression(expression);
        cout << result << endl;
    }
    catch (const runtime_error& e) {
        // // 处理异常的代码块
        cerr << "Error: " << e.what() << endl;
    }

    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值