下面是一个使用栈实现的C++代码示例,将中缀表达式转换为后缀表达式并计算结果:
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;
// 判断字符是否为运算符
bool isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/');
}
// 获取运算符的优先级
int getPriority(char op) {
    if (op == '+' || op == '-')
        return 1;
    else if (op == '*' || op == '/')
        return 2;
    return 0; // 其他情况返回0
}
// 中缀表达式转后缀表达式
string infixToPostfix(string infix) {
    string postfix = ""; // 存放后缀表达式
    stack<char> operatorStack; // 运算符栈
    for (char& ch : infix) {
        if (ch == ' ') {
            continue; // 忽略空格字符
        } else if (isdigit(ch)) {
            postfix += ch; // 操作数直接加入后缀表达式中
        } else if (isOperator(ch)) {
            while (!operatorStack.empty() && operatorStack.top() != '(' &&
                   getPriority(operatorStack.top()) >= getPriority(ch)) {
                postfix += operatorStack.top(); // 将栈顶的高优先级运算符弹出并加入后缀表达式中
                operatorStack.pop();
            }
            operatorStack.push(ch); // 当前运算符入栈
        } else if (ch == '(') {
            operatorStack.push(ch); // 左括号入栈
        } else if (ch == ')') {
            while (!operatorStack.empty() && operatorStack.top() != '(') {
                postfix += operatorStack.top(); // 将栈顶运算符弹出并加入后缀表达式中
                operatorStack.pop();
            }
            if (!operatorStack.empty())
                operatorStack.pop(); // 弹出左括号
        }
    }
    while (!operatorStack.empty()) {
        postfix += operatorStack.top(); // 将栈中剩余的运算符加入后缀表达式中
        operatorStack.pop();
    }
    return postfix;
}
// 计算后缀表达式的结果
double evaluatePostfix(string postfix) {
    stack<double> operandStack; // 操作数栈
    stringstream ss(postfix);
    string token;
    while (getline(ss, token, ' ')) {
        if (isdigit(token[0])) {
            double operand = stod(token); // 将操作数从字符串转换为浮点数
            operandStack.push(operand);
        } else if (isOperator(token[0])) {
            double operand2 = operandStack.top();
            operandStack.pop();
            double operand1 = operandStack.top();
            operandStack.pop();
            switch (token[0]) { // 根据不同运算符进行计算并将结果压入操作数栈
                case '+':
                    operand1 += operand2;
                    break;
                case '-':
                    operand1 -= operand2;
                    break;
                case '*':
                    operand1 *= operand2;
                    break;
                case '/':
                    if (operand2 != 0)
                        operand1 /= operand2;
                    else {
                        cerr << "Error: Division by zero." << endl;
                        exit(1);
                    }
                    break;
                default:
                    cerr << "Error: Invalid operator." << endl;
                    exit(1);
            }
            operandStack.push(operand1);
        }
    }
    if (!operandStack.empty())
        return operandStack.top();
    
    cerr << "Error: Invalid expression." << endl;
    exit(1);
}
int main() {
    string infixExpression;
    getline(cin, infixExpression);
    // 去除首尾的'#'字符
    infixExpression = infixExpression.substr(1, infixExpression.length() - 2);
    string postfixExpression = infixToPostfix(infixExpression);
    cout << "转换的后缀表达式为:" << postfixExpression << endl;
    double result = evaluatePostfix(postfixExpression);
    cout << "计算结果为:" << result << endl;
    return 0;
}
你可以在输入时通过标准输入读取中缀表达式,然后调用infixToPostfix函数将其转换为后缀表达式,并调用evaluatePostfix函数计算结果。最后输出后缀表达式和计算结果。
注意:该代码只适用于符合题目要求的简单中缀表达式,对于复杂的运算符优先级、括号等情况可能需要进行修改。
内容由零声教学AI助手提供,问题来源于学员提问


        
        
        
        
        
        
        
        
        
                
                
                
                
                
                
                