编译原理-逆波兰表达式JAVA算法

来源:互联网 发布:linux 查看主机arp 编辑:程序博客网 时间:2024/04/26 19:31

Demo

基于Java语言

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">public class InversePoland {  
  2.     // 9+(3-1)*3+10/2 = 20  
  3.     //private static String[] ss = new String[]{"9","+","(","3","-","1",")","*","3","+","10","/","2"};  
  4.        
  5.     //24+3-8*(6-3)*2+10-3  
  6.     private static String[] ss = new String[]{"24","+","3","-","8","*","(","6","-","3",")","*","2","+","10","-","3"};  
  7.     private static Stack<String> stack = new Stack<String>();  
  8.        
  9.     //判断是否为数字  
  10.     private boolean isNum(String s) {  
  11.         if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("(") || s.equals(")")) {  
  12.             return false;  
  13.         }  
  14.         return true;  
  15.     }  
  16.        
  17.     //获取符号优先级  
  18.     private int getPriority(String s) {  
  19.         if (s.equals("+") || s.equals("-")) {  
  20.             return 1;  
  21.         } else if (s.equals("*") || s.equals("/")) {  
  22.             return 2;  
  23.         }  
  24.         return -1;  
  25.     }  
  26.        
  27.     //输出字符串  
  28.     private void print(String s) {  
  29.         System.out.print(s + " ");  
  30.     }  
  31.        
  32.     //符号操作  
  33.     private void symbolOperation(String s) {  
  34.         if (stack.size() == 0 || s.equals("(")) { //栈为空,直接进栈  
  35.             stack.push(s);  
  36.         } else if (s.equals(")")) {  //当前字符为“)”,则将栈中(以上的全部出栈输出  
  37.             while (stack.size() > 0) {  
  38.                 String pop_s = stack.pop();  
  39.                 if(pop_s.equals("(")) {  
  40.                     break;  
  41.                 } else {  
  42.                     print(pop_s);  
  43.                 }  
  44.             }   
  45.         } else {  
  46.             int pri_s = getPriority(s);  
  47.             while (stack.size() > 0 ) {  
  48.                 String top_s = stack.lastElement();  
  49.                 if (top_s.equals("(")) {  
  50.                     stack.push(s);  
  51.                     return;  
  52.                 } else {  
  53.                     int top_pri = getPriority(top_s);  
  54.                     if (pri_s <= top_pri) {  
  55.                         String pop_s = stack.pop();  
  56.                         print(pop_s);  
  57.                     } else {  
  58.                         break;  
  59.                     }  
  60.                 }  
  61.             }  
  62.             stack.push(s);  
  63.         }  
  64.     }  
  65.        
  66.     public void test() {  
  67.         for (String s : ss) {  
  68.             if (isNum(s)) {  
  69.                 print(s);  
  70.             } else {  
  71.                 symbolOperation(s);  
  72.             }  
  73.         }  
  74.         while (stack.size() > 0) {  
  75.             String pop_s = stack.pop();  
  76.             print(pop_s);  
  77.         }  
  78.     }  
  79. }  
  80. </span>  

输出结果

24 3 + 86 3 - * 2 * - 10 + 3 – 

0 0
原创粉丝点击