反向波兰表示法Java

Posted

技术标签:

【中文标题】反向波兰表示法Java【英文标题】:Reverse Polish Notation Java 【发布时间】:2012-12-31 10:42:45 【问题描述】:

我想制作反向波兰表示法算法,但我的代码不起作用。谁能解释我为什么? 在我的代码中,我想取字符,直到 "/+-*"tokens 。如果此标记是下一个,则进行操作,其中包含 in case 。

import java.util.Stack;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

public class ONP 

    public static void main(String[] args) 
        Stack<Double> stack = new Stack<Double>();
        double number1;
        double number2;

        String x = JOptionPane.showInputDialog("Tokens");
        StringTokenizer str = new StringTokenizer(x);

        while (str.hasMoreElements()) 
            str.nextElement();
            stack.push((Double) str.nextElement());

            for (int i = 0; i < x.length(); i++) 

                switch (x.charAt(i)) 
                case '+':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 + number2);
                    break;
                case '-':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 - number2);
                    break;
                case '/':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 / number2);
                    break;
                case '*':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 * number2);
                    break;
                

            
            System.out.println(stack.pop());
        
    


我想在没有 StringBuilder 的情况下编写它。

输入:

2 2 + 2 / 2 /

输出:

(2+2) / 2 / 2

【问题讨论】:

【参考方案1】:

试试这个

String s1;  



while (str.hasMoreElements()) 
 
     s1 = str.nextElement();

     if(s1.equals("+") || s1.equals("-") || s1.equals("*") || s1.equals("/"))
     
          switch (s1) 
          
                case '+':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.print(number1 + " + " + number2);
                    break;
                case '-':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.print(number1 +" - "+ number2);
                    break;
                case '/':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.print(number1 +" / "+ number2);
                    break;
                case '*':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.print(number1 +" * "+ number2);
                    break;
                
         
         else
         
                 stack.push(s1);
         

    

而不是

 while (str.hasMoreElements()) 
        str.nextElement();
        stack.push((Double) str.nextElement());

        for (int i = 0; i < x.length(); i++) 

            switch (x.charAt(i)) 
            case '+':
                number1 = stack.pop();
                number2 = stack.pop();
                System.out.println(number1 + number2);
                break;
            case '-':
                number1 = stack.pop();
                number2 = stack.pop();
                System.out.println(number1 - number2);
                break;
            case '/':
                number1 = stack.pop();
                number2 = stack.pop();
                System.out.println(number1 / number2);
                break;
            case '*':
                number1 = stack.pop();
                number2 = stack.pop();
                System.out.println(number1 * number2);
                break;
            

        
        System.out.println(stack.pop());
    

【讨论】:

您的代码存在编译问题。 str.nextElement() 返回一个对象而不是字符串。开关盒标签也需要用双引号而不是单引号,例如“+”而不是“+”。

以上是关于反向波兰表示法Java的主要内容,如果未能解决你的问题,请参考以下文章

java 150.评估反向波兰表示法(#)

java 150.评估反向波兰表示法(#)

java 150.评估反向波兰表示法(#)

java 150.评估反向波兰表示法(#)

c_cpp 评估反向波兰表示法

反向波兰符号的中缀