波兰式转换为逆波兰式

  • 波兰式是在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之前,所以这种表示法也称为前缀表达式。例如: (2 + 4) * 6的波兰式为 *+246
  • 逆波兰式(Reverse Polish notation,RPN,或逆波兰记法),将运算符写在操作数之后,也叫后缀表达式。例如:(2 + 4) * 6的逆波兰式为 24+6*
  • 算法实现(java):
    1. 构建波兰式二叉树
    2. 后续遍历二叉树生成逆波兰式
    3. 输入时每次接受一个运算符或操作数
public class Test {
    public static void main(String[] args){
        System.out.println("Build PN tree:");
        Node tree = buildTree();
        System.out.println("\npreOrderTraverse:");
        preOrderTraverse(tree);
        System.out.println("\ninOrderTraverse:");
        inOrderTraverse(tree);
        System.out.println("\npostOrderTraverse:");
        postOrderTraverse(tree);
    }   
    static class Node{
        String value;
        Node left;
        Node right;
    }

    static Node buildTree(){
        Scanner input = new Scanner(System.in);
        String exp = input.next();
        Node node = new Node();
        node.value = exp;
        if(exp.equals("+") || exp.equals("-") || exp.equals("*") || exp.equals("/")){
            node.left = buildTree();
            node.right = buildTree();
        }
        return node;
    }

    public static void preOrderTraverse(Node tree){
        if(tree == null){
            return;
        }
        System.out.print(tree.value);
        preOrderTraverse(tree.left);
        preOrderTraverse(tree.right);
    }

    public static void inOrderTraverse(Node tree){
        if(tree == null){
            return;
        }
        if(tree.left != null){
            System.out.print("(");
        }
        inOrderTraverse(tree.left);
        System.out.print(tree.value);
        inOrderTraverse(tree.right);
        if(tree.right != null){
            System.out.print(")");
        }
    }

    public static void postOrderTraverse(Node tree){
        if(tree == null){
            return;
        }
        postOrderTraverse(tree.left);
        postOrderTraverse(tree.right);
        System.out.print(tree.value);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将中缀算数表达式转换逆波兰式的C++代码: ```c++ #include <iostream> #include <stack> #include <string> #include <sstream> #include <vector> using namespace std; bool is_operator(const string& token) { return token == "+" || token == "-" || token == "*" || token == "/"; } int precedence(const string& token) { if (token == "+" || token == "-") { return 1; } else if (token == "*" || token == "/") { return 2; } else { return 0; } } vector<string> infix_to_postfix(const vector<string>& infix) { stack<string> s; vector<string> postfix; for (const auto& token : infix) { if (is_operator(token)) { while (!s.empty() && is_operator(s.top()) && precedence(token) <= precedence(s.top())) { postfix.push_back(s.top()); s.pop(); } s.push(token); } else if (token == "(") { s.push(token); } else if (token == ")") { while (!s.empty() && s.top() != "(") { postfix.push_back(s.top()); s.pop(); } s.pop(); } else { postfix.push_back(token); } } while (!s.empty()) { postfix.push_back(s.top()); s.pop(); } return postfix; } int evaluate_postfix(const vector<string>& postfix) { stack<int> s; for (const auto& token : postfix) { if (is_operator(token)) { int b = s.top(); s.pop(); int a = s.top(); s.pop(); if (token == "+") { s.push(a + b); } else if (token == "-") { s.push(a - b); } else if (token == "*") { s.push(a * b); } else { s.push(a / b); } } else { stringstream ss(token); int x; ss >> x; s.push(x); } } return s.top(); } int main() { // 中缀表达式: (3+4)*5-6/2 vector<string> infix = {"(", "3", "+", "4", ")", "*", "5", "-", "6", "/", "2"}; vector<string> postfix = infix_to_postfix(infix); for (const auto& token : postfix) { cout << token << " "; } cout << endl; int result = evaluate_postfix(postfix); cout << "Result: " << result << endl; return 0; } ``` 这个代码使用了 `stack` 来实现算法,其中 `infix_to_postfix` 函数将中缀表达式转换为后缀表达式, `evaluate_postfix` 函数计算后缀表达式的值。在 `main` 函数中,我们使用了 `(3+4)*5-6/2` 这个表达式来测试我们的代码,输出结果为: ``` 3 4 + 5 * 6 2 / - Result: 29 ``` 这表明我们的代码成功地将中缀表达式转换为了后缀表达式,并正确地计算了表达式的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值