ZBLOG

修改代码不输出#:#include #include #include using namespace std; // 判断字符是否为运算符 bool isOperator(char ch) { return (ch == '+' || ch == '-' || ch == '*' || ch == '/'); } // 获取运算符的优先级 int getPriority(char op) { if (op == ...

以下是修改后的代码,不会输出#:

#include <iostream>
#include <stack>
#include <string>
using namespace std;

// 判断字符是否为运算符
bool isOperator(char ch) {
    return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}

// 获取运算符的优先级
int getPriority(char op) {
    if (op == '+' || op == '-')
        return 1;
    else if (op == '*' || op == '/')
        return 2;
    return 0;
}

// 中缀表达式转后缀表达式
string infixToPostfix(string infix) {
    stack<char> operators; // 运算符栈
    string postfix = "";   // 后缀表达式

    for (char ch : infix) {
        if (ch != '#') { // 排除 #
            if (ch == '(') {
                operators.push(ch);
            }
            else if (ch == ')') {
                while (!operators.empty() && operators.top() != '(') {
                    postfix += operators.top();
                    operators.pop();
                }
                operators.pop(); // 弹出左括号
            }
            else if (isOperator(ch)) {
                while (!operators.empty() && getPriority(operators.top()) >= getPriority(ch)) {
                    postfix += operators.top();
                    operators.pop();
                }
                operators.push(ch);
            }
            else { // 操作数
                postfix += ch;
            }
        }       
    }

    while (!operators.empty()) {
        postfix += operators.top();
        operators.pop();
    }

    return postfix;
}

// 计算后缀表达式的结果
double evaluatePostfix(string postfix) {
    stack<double> operands; // 操作数栈

    for (char ch : postfix) {
        if (!isOperator(ch)) { // 操作数
            operands.push(ch - '0'); // 字符转数字
        }
        else { // 运算符
            double operand2 = operands.top();
            operands.pop();
            double operand1 = operands.top();
            operands.pop();

            switch (ch) {
            case '+':
                operands.push(operand1 + operand2);
                break;
            case '-':
                operands.push(operand1 - operand2);
                break;
            case '*':
                operands.push(operand1 * operand2);
                break;
            case '/':
                operands.push(operand1 / operand2);
                break;
            }
        }
    }

    return operands.top();
}

int main() {
    string infix;
    cin >> infix;

    string postfix = infixToPostfix(infix);
    cout << "转换的后缀表达式为:" << postfix << endl;

    double result = evaluatePostfix(postfix);
    cout << "计算结果为:" << result << endl;

    return 0;
}

修改后的代码会在读取输入时排除掉字符 ‘#‘,从而不会输出它。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=10372

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?