2015-02-24 64 views
1

我有一個方法getRPNString(),它返回Reverse Polish Notation字符串。我想通過空格鍵分割這個字符串來計算它。現在我無法理解如何在我的RNP字符串中添加空格鍵,因爲它不適用於兩位數字。RPN表達式中元素之間的空格java

public class Calc1 { 

public static void main(String[] args) { 

    String in = "((5+3*(4+2)*12)+3)/(1+3)+5"; 
    String out = getRPNString(in); 
    System.out.println(out); 

} 

private static String getRPNString(String in) { 
    LinkedList<Character> oplist = new LinkedList<>(); 
    StringBuilder out = new StringBuilder(); 

    for (int i = 0; i < in.length(); i++) { 
     char op = in.charAt(i); 
     if (op == ')') { 
      while (oplist.getLast() != '(') { 
       out.append(oplist.removeLast()); 
      } 
      oplist.removeLast(); 
     } 

     if (Character.isDigit(op)) { 

      out.append(op); 

      /*int j = i + 1; 
      for (; j < in.length(); j++) { 
       if (!Character.isDigit(j)) { 
        break; 
       } 
       i++; 
      } 
      out.append(in.substring(i, j));*/ 

     } 

     if (op == '(') { 
      oplist.add(op); 
     } 

     if (isOperator(op)) { 
      if (oplist.isEmpty()) { 
       oplist.add(op); 
      } else { 
       int priority = getPriority(op); 
       if (priority > getPriority(oplist.getLast())) { 
        oplist.add(op); 
       } else { 
        while (!oplist.isEmpty() 
          && priority <= getPriority(oplist.getLast())) { 
         out.append(oplist.removeLast()); 
        } 
        oplist.add(op); 
       } 
      } 
     } 

    } 

    while (!oplist.isEmpty()) { 
     out.append(oplist.removeLast()); 
    } 

    return out.toString(); 
} 

private static boolean isOperator(char c) { 
    return c == '+' || c == '-' || c == '*' || c == '/' || c == '%'; 
} 

private static int getPriority(char op) { 
    switch (op) { 

    case '*': 
    case '/': 
     return 3; 

    case '+': 
    case '-': 
     return 2; 

    case '(': 
     return 1; 

    default: 
     return -1; 
    } 
} 

}

我試圖通過追加(」「)在我的StringBuilder變量進行添加spacebars。但是兩位數字不對。我想我完全不明白如何做到這一點。例如,如果輸入是String in =「((5 + 3 *(4 + 2)* 12)+3)/(1 + 3)+5」;出局將是5342+ + 3 + 13 +/5 +,當我向所有調用添加空格鍵out.append('')** out是** 5 3 4 2 + * 1 2 * + 3 + 1 3 +/5 +,所以像「12」這樣的數字變成了「1 2」。 你能幫忙嗎?

回答

0

只要改變你已經註釋掉,之後Character.isDigit(op)的代碼:我改變了我的方法

int j = i + 1; 
int oldI = i;//this is so you save the old value 
for (; j < in.length(); j++) { 
    if (!Character.isDigit(in.charAt(j))) { 
     break; 
    } 
    i++; 
} 
out.append(in.substring(oldI, j)); 
out.append(' '); 
+1

嗨,它的工作原理,謝謝。 – dimads 2015-02-25 12:13:26

0

,現在它工作正常。當我將 !Character.isDigit(j)改寫但是需要!Character.isDigit(in.charAt(j))時,我記錄了我的錯誤。

private static String getRPNString(String in) { 
    LinkedList<Character> oplist = new LinkedList<>(); 
    StringBuilder out = new StringBuilder(); 

    for (int i = 0; i < in.length(); i++) { 
     char op = in.charAt(i); 
     if (op == ')') { 
      while (oplist.getLast() != '(') { 
       out.append(oplist.removeLast()).append(' '); 
      } 
      oplist.removeLast(); 
     } 

     if (Character.isDigit(op)) { 

      int j = i + 1; 
      int oldI = i;//this is so you save the old value 
      for (; j < in.length(); j++) { 
       if (!Character.isDigit(in.charAt(j))) { 
        break; 
       } 

       i++; 
      } 

      out.append(in.substring(oldI, j)); 
      out.append(' '); 

     } 

     if (op == '(') { 
      oplist.add(op); 
     } 

     if (isOperator(op)) { 
      if (oplist.isEmpty()) { 
       oplist.add(op); 
      } else { 
       int priority = getPriority(op); 
       if (priority > getPriority(oplist.getLast())) { 
        oplist.add(op); 
       } else { 
        while (!oplist.isEmpty() 
          && priority <= getPriority(oplist.getLast())) { 
         out.append(oplist.removeLast()).append(' '); 
        } 
        oplist.add(op); 
       } 
      } 
     } 

    } 

    while (!oplist.isEmpty()) { 
     out.append(oplist.removeLast()).append(' '); 
    } 

    return out.toString(); 
} 

現在它產生正確的表達。 ((5 + 3 *(4 + 2)* 12)+3)/(1 + 3)+5 輸出:5 3 4 2 + * 12 * + 3 + 1 3 +/5 +