Qt 第9课、计算器中缀转后缀算法

计算器核心算法:

1、将中缀表达式进行数字运算符的分离
2、将中缀表达式转换成后缀表达式
3、通过后缀表达式计算最后的结果
在这里插入图片描述


三、计算器中缀转后缀算法

计算器中缀转后缀算法的意义在于把中缀表达式转换成后缀表达式,能够更好地计算

  • 算法的基本思路
    1、如果是isNumber,直接进输出队列
    2、如果是isOperator
    如果一个运算符优先级 <= 前一个运算符优先级,则这个前面那个运算符进输出队列
    其余的全部进栈
    3、如果是左括号,进栈
    4、如果是右括号,直到栈顶为左括号才不用把栈里的进输出队列,
    把栈顶元素pop()
    5、其余的报错
    如果栈里面不为空,直接全部进队列
bool QCalculatorDec::transform(QQueue<QString>& exp, QQueue<QString>& output)
{
    bool ret = match(exp);
    output.clear();
    QStack<QString> stack;

    while(ret && !exp.isEmpty())
    {
        QString str = exp.dequeue();

        if(isNumber(str))
        {
            output.enqueue(str);
        }
        else if(isOperator(str))
        {
            while(!stack.isEmpty() && priority(str) <= priority(stack.top()))
            {
                output.enqueue(stack.pop());
            }
            stack.push(str);
        }
        else if(isLeft(str))
        {
            stack.push(str);
        }
        else if(isRight(str))
        {
            while(!stack.isEmpty() && !isLeft(stack.top()))
            {
                output.enqueue(stack.pop());
            }
            stack.pop();
        }
        else
        {
            ret = false;
        }
    }
    while(!stack.isEmpty())
    {
        output.enqueue(stack.pop());
    }
    if(!ret)
    {
        output.clear();
    }
    return ret;
}
QCalculatorDec::QCalculatorDec()
{
    m_exp = "";
    m_result = "";

    QQueue<QString> str;

    str = split("9 + (-3 - 1) * 5 + 1");
    
    for(int i = 0; i < str.length(); i++)
    {
        qDebug() << str[i];
    }

    qDebug() << match(str) << endl;

    QQueue<QString> output;

    transform(str, output);
    for(int i = 0; i < output.length(); i++)
    {
        qDebug() << output[i];
    }
}

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++中实现中缀后缀求值的示例代码: ```cpp #include <iostream> #include <stack> #include <string> #include <sstream> using namespace std; int getPriority(char op) { if (op == '+' || op == '-') return 1; else if (op == '*' || op == '/') return 2; else return 0; } string infixToPostfix(string infix) { stack<char> opStack; stringstream postfix; for (char c : infix) { if (isdigit(c)) { postfix << c; } else if (c == '(') { opStack.push(c); } else if (c == ')') { while (!opStack.empty() && opStack.top() != '(') { postfix << opStack.top(); opStack.pop(); } opStack.pop(); // 弹出左括号 } else { while (!opStack.empty() && getPriority(opStack.top()) >= getPriority(c)) { postfix << opStack.top(); opStack.pop(); } opStack.push(c); } } while (!opStack.empty()) { postfix << opStack.top(); opStack.pop(); } return postfix.str(); } int evaluatePostfix(string postfix) { stack<int> numStack; for (char c : postfix) { if (isdigit(c)) { numStack.push(c - '0'); } else { int operand2 = numStack.top(); numStack.pop(); int operand1 = numStack.top(); numStack.pop(); switch (c) { case '+': numStack.push(operand1 + operand2); break; case '-': numStack.push(operand1 - operand2); break; case '*': numStack.push(operand1 * operand2); break; case '/': numStack.push(operand1 / operand2); break; } } } return numStack.top(); } int main() { string infixExpression; cout << "请输入中缀表达式:"; cin >> infixExpression; string postfixExpression = infixToPostfix(infixExpression); cout << "后缀表达式:" << postfixExpression << endl; int result = evaluatePostfix(postfixExpression); cout << "计算结果:" << result << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值