把简单的程序放大

Even226 逗比 2023-09-24 9:04:00 4
#include <iostream>
#include <string>
#include <stack>
#include <sstream>

int getPriority(char op) {
    if (op == '+' || op == '-') return 1;
    if (op == '*' || op == '/') return 2;
    return 0;
}
double executeOperation(double operand1, double operand2, char op) {
    switch (op) {
        case '+': return operand1 + operand2;
        case '-': return operand1 - operand2;
        case '*': return operand1 * operand2;
        case '/': return operand1 / operand2;
        default: return 0.0;
    }
}

double calculateExpression(const std::string& expression) {
    std::stack<double> operands;
    std::stack<char> operators;
    std::istringstream iss(expression);
    char token;

    while (iss >> token) {
        if (isdigit(token) || (token == '.')) {
            double number;
            iss.putback(token);
            iss >> number;
            operands.push(number);
        } else if (token == '(') {
            operators.push(token);
        } else if (token == ')') {
            while (!operators.empty() && operators.top() != '(') {
                char op = operators.top();
                operators.pop();
                double operand2 = operands.top();
                operands.pop();
                double operand1 = operands.top();
                operands.pop();
                operands.push(executeOperation(operand1, operand2, op));
            }
            if (!operators.empty() && operators.top() == '(') {
                operators.pop(); 
            }
        } else if (token == '+' || token == '-' || token == '*' || token == '/') {
            while (!operators.empty() && getPriority(operators.top()) >= getPriority(token)) {
                char op = operators.top();
                operators.pop();
                double operand2 = operands.top();
                operands.pop();
                double operand1 = operands.top();
                operands.pop();
                operands.push(executeOperation(operand1, operand2, op));
            }
            operators.push(token);
        }
    }

    while (!operators.empty()) {
        char op = operators.top();
        operators.pop();
        double operand2 = operands.top();
        operands.pop();
        double operand1 = operands.top();
        operands.pop();
        operands.push(executeOperation(operand1, operand2, op));
    }

    return operands.top();
}

int main() {
    std::string expression;
    std::cout << "请输入一个表达式(支持四则运算和括号): ";
    std::getline(std::cin, expression);

    try {
        double result = calculateExpression(expression);
        std::cout << "结果为: " << result << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
    }

    return 0;
}
{{ vote && vote.total.up }}

共 7 条回复

bai123 暗区传说

............

Even226 逗比

奈斯

Even226 逗比

真好多了10排

Even226 逗比
#include <iostream>
#include <string>
#include <stack>
#include <sstream>
#include <stdexcept>

int getPriority(char op) {
    if (op == '+' || op == '-') return 1;
    if (op == '*' || op == '/') return 2;
    return 0;
}

double executeOperation(double operand1, double operand2, char op) {
    switch (op) {
        case '+': return operand1 + operand2;
        case '-': return operand1 - operand2;
        case '*': return operand1 * operand2;
        case '/':
            if (operand2 == 0) {
                throw std::runtime_error("除零错误");
            }
            return operand1 / operand2;
        default: return 0.0;
    }
}
double calculateExpression(const std::string& expression) {
    std::stack<double> operands;
    std::stack<char> operators;
    std::istringstream iss(expression);
    char token;

    while (iss >> token) {
        if (isdigit(token) || (token == '.')) {
            double number;
            iss.putback(token);
            iss >> number;
            operands.push(number);
        } else if (token == '(') {
            operators.push(token);
        } else if (token == ')') {
            while (!operators.empty() && operators.top() != '(') {
                char op = operators.top();
                operators.pop();
                double operand2 = operands.top();
                operands.pop();
                double operand1 = operands.top();
                operands.pop();
                operands.push(executeOperation(operand1, operand2, op));
            }
            if (!operators.empty() && operators.top() == '(') {
                operators.pop();
            } else {
                throw std::runtime_error("括号不匹配");
            }
        } else if (token == '+' || token == '-' || token == '*' || token == '/') {
            while (!operators.empty() && getPriority(operators.top()) >= getPriority(token)) {
                char op = operators.top();
                operators.pop();
                double operand2 = operands.top();
                operands.pop();
                double operand1 = operands.top();
                operands.pop();
                operands.push(executeOperation(operand1, operand2, op));
            }
            operators.push(token);
        }
    }

    while (!operators.empty()) {
        char op = operators.top();
        operators.pop();
        double operand2 = operands.top();
        operands.pop();
        double operand1 = operands.top();
        operands.pop();
        operands.push(executeOperation(operand1, operand2, op));
    }

    if (operands.size() == 1) {
        return operands.top();
    } else {
        throw std::runtime_error("表达式无效");
    }
}

int main() {
    std::string expression;
    std::cout << "请输入一个表达式(支持四则运算和括号): ";
    std::getline(std::cin, expression);

    try {
        double result = calculateExpression(expression);
        std::cout << "结果为: " << result << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
    }

    return 0;
}
CPP 刷题王

?

Even226 逗比

感觉还不够

CPP 刷题王

666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666