2013-05-06 25 views
0

首先,我會坦率地指出這是一個作業問題。具有堆棧和符號表的表達式解析器

我使用下面的代碼作爲我的基礎上構建一個表達式解析器: 編輯:

public class Interpreter { 

public static Double parser(ST<String, Double> variables, String[] inputArray) 
{  
    Double valueHolder; 

    Stack<String> ops = new Stack<String>(); 
    Stack<Double> vals = new Stack<Double>(); 

    for(int i = 0; i < inputArray.length; i++) 
    { 
     String input = inputArray[i]; 

     if (input.equals("("))      ; 
     else if (input.equals("+"))  ops.push(input); 
     else if (input.equals("-"))  ops.push(input); 
     else if (input.equals("*"))  ops.push(input); 
     else if (input.equals("/"))  ops.push(input); 
     else if (input.equals("sqrt")) ops.push(input); 

     else if (input.equals(")")) 
     { 
      String op = ops.pop(); 
      double v = vals.pop(); 
      if  (op.equals("+")) v = vals.pop() + v;  
      else if (op.equals("-")) v = vals.pop() - v; 
      else if (op.equals("*")) v = vals.pop() * v; 
      else if (op.equals("/")) v = vals.pop()/v; 
      else if (op.equals("sqrt")) v = Math.sqrt(v); 
      vals.push(v); 
     } 
     else if (input.matches("\\D")) 
      { 
       valueHolder = variables.get(inputArray[i]); 
       vals.push(valueHolder); 
      } 
     else vals.push(Double.parseDouble(input)); 
    } 

    return vals.pop(); 
} 

public static String[] getValue(ST<String, Double> variables, String[] inputArray) 
{ 
    Double keyHolder; 
    Double valueHolder; 

    for(int i = 0; i < inputArray.length; i++) 
    { 
     if(variables.contains(inputArray[i])) 
      { 
       keyHolder = variables.get(inputArray[i]); 
       inputArray[i] = keyHolder.toString(); 
      } 
     else if (!variables.contains(inputArray[i])) 
     { if (inputArray[i].matches("\\D")) //if letter 
      { if (!inputArray[i].equals("=")) //if not "=" 
       {for (int j = i + 1; j < inputArray.length; j++) //next element 
        { if (inputArray[j].matches("\\D")) //if letter 
         {if (!inputArray[j].matches("=")) //if not "=" 
          { 

           //get values and do math 
          } 
         } 
        else if (inputArray[j].matches("\\d")) // if digit 
        { if (j + 1 >= inputArray.length) 
         { 
          valueHolder = Double.parseDouble(inputArray[j]); 
          variables.put(inputArray[i], valueHolder); 
         } 
         else parser(variables, inputArray); //if 
        } 
        } 
       } 
      } 

     } 
    } 

    return inputArray; 
} 

public static void main(String[] args) 
{ 
    ST<String, Double> variables = new ST<String, Double>(); 

    while(!StdIn.isEmpty()) 
    { 
     String input = StdIn.readLine(); 
     String[] inputArray = input.split("\\s+");   // read a line and split it by whitespace 
     inputArray = getValue(variables, inputArray);  // remove any variables and replace with their associated values 
     double y = parser(inputArray); 
     System.out.println(y); 
    } 

} 

}

控制檯輸入會是這樣的:

A = 5 
B = 10 
C = A + B 
D = C * C 
print(D) 

在這種情況下,控制檯的輸出是225.我的問題是我無法弄清楚如何拆分inpu t在符號表的鍵和值之間。用於輸入密鑰和值的API是void put(Key key, Value v),如果密鑰設置爲null,則密鑰將被刪除。先謝謝了。

回答

0

它不只是字符串分割,需要其他檢查過,如:

1)分割字符串基於=模式

if(stringValue.matches("=")) 
{ 
String[] valArray = stringValue.split("="); 
} 
else 
{ 
// do something else 
} 

這會給你的字符串數組。現在循環訪問字符串數組並檢查以下條件。

2)檢查是否存在本numeric

即:valArray[].matches("\d");

3)如果numeric值存在,檢查是否存在多於1次出現的alphabet numeric值本(以查看是否存在多於一個變量)

這是爲了檢查是否存在任何字母分割字符串>>valArray[].matches("\D");

4)最後,如果僅存在1數值和字母數字1值目前,存儲鍵和值。

5)如果有超過1個空變量出現,那麼您將需要跳過該操作(加號,減號......),直到在鍵值數組中存在變量值。

You can check this by checking your key-value pair array. You don't store key-value if the value if empty. 

6)如果=不存在,則在字符串中檢查print並執行打印操作。

即:

if(stringValue.matches("print")); 
{ 
//Do something 
} 

注:stringValue是你的控制檯輸入線。

+0

我試過了,但我無法讓拆分工作。我編輯了我的問題以反映我創建的代碼的合併。當我調試代碼時,我發現無論我做什麼,第一個參數都被識別爲'Stack',第二個參數被分解爲一個數組作爲'S tack',每個字母都是s的一個元素.Array []'。無論你是否及時看到這一點,謝謝你的幫助。 – rice2007 2013-05-07 01:56:25

+0

作爲Stack的第一個參數和第二個S T a c k?那是在你輸入控制檯之後嗎?你的控制檯輸入是什麼? – 2013-05-07 03:15:23

+0

我的控制檯輸入第一次是'A = 5'' B = 10'' C = A + B'' D = C * C'' print(D)',每個塊在一個單獨的輸入線上。然後我嘗試了'A = 5'。每次都有同樣的結果。 – rice2007 2013-05-07 03:26:20