2016-01-27 39 views
1

我正在研究一個迷你科學計算器,它適用於infixpostfix算法。我的輸入是一箇中綴字符串..而我的infixpostfix轉換邏輯需要arraystring。那麼,如何可以拆分中綴串是這樣的: 將中綴字符串拆分爲java中的字符串數組

100+(0.03*55)/45-(25+55) 

至字符串的陣列,其中每個操作數和運算符是一個數組元素。這樣

"100" , "+" , "(" , "0.03" , "*" , "55" , ")" , "/" , "45" , "-" 

等等...

注意這裏是字符串中沒有空間,所以它不能正則表達式" "的基礎上進行拆分。

回答

0

請看this other question的答案。

這應該做的伎倆:

Pattern p = Pattern.compile("(?:(\\d+)|([+-*/\\(\\)]))"); 
Matcher m = p.matcher("100+(0.03*55)/45-(25+55)"); 
List<String> tokens = new LinkedList<String>(); 
while(m.find()) 
{ 
    String token = m.group(0); //group 0 is always the entire match 
    tokens.add(token); 
} 
+1

隨着正則表達式'(正則表達式:(\\ d +)|([+ - */\\(\\)])'最外組是不?所以它從組'0'開始,可以提高性能 – Clashsoft

1

您可以使用正則表達式解析存儲在一個字符串表達式mathamatical。

expString.split("(?<=[-+*/\\(\\)])|(?=[-+*/\\(\\)])"); 

會爲你做詭計。

說,

String str = "100+(0.03*55)/45-(25+55)"; 
String[] outs = str.split("(?<=[-+*/\\(\\)])|(?=[-+*/\\(\\)])"); 
for (String element : outs) 
{ 
    System.out.println(element); 
} 

會給你的輸出,

100 
+ 
(
0.03 
* 
55 
) 
/
45 
- 
(
25 
+ 
55 
) 

請檢查我的實驗@http://rextester.com/QEMOYL38160

+0

輸出不正確,[100,+,(0.03,*,55),/,45, - ,(25,+,55)] – Richard

+1

代碼和輸出已更新@理查德 –

2

顯然,每一個字符是分開的道理,除了連續數字可能有點。因此,一個簡單的解決方案將迭代字符串,然後當您看到一個數字前面有另一個數字(或小數點分隔符,點)時,您將該字符添加到前一個標記,否則將其添加到新標記。

下面的代碼:

public static List<String> getTokens(String inputString) { 
    List<String> tokens = new ArrayList<String>(); 
    // Add the first character to a new token. We make the assumption 
    // that the string is not empty. 
    tokens.add(Character.toString(inputString.charAt(0))); 

    // Now iterate over the rest of the characters from the input string. 
    for (int i = 1; i < inputString.length(); i++) { 
     char ch = inputString.charAt(i); // Store the current character. 
     char lch = inputString.charAt(i - 1); // Store the last character. 

     // We're checking if the last character is either a digit or the 
     // dot, AND if the current character is either a digit or a dot. 
     if ((Character.isDigit(ch) || ch == '.') && (Character.isDigit(lch) || lch == '.')) { 
      // If so, add the current character to the last token. 
      int lastIndex = (tokens.size() - 1); 
      tokens.set(lastIndex, tokens.get(lastIndex) + ch); 
     } 
     else { 
      // Otherwise, add the current character to a new token. 
      tokens.add(Character.toString(ch)); 
     } 
    } 
    return tokens; 
} 

注意這種方法比大多數的正則表達式的方法更快。

1

下面是一個算法我會使用:對於當前的字符確定類型(位

開始與空字符串數組,和一個空字符串緩衝區

  • 步行從字符0至字符n
  • /句點,開放paren,關閉paren,數學運算符)
  • 如果當前字符類型與最後一個字符類型相同
  • 將當前字符添加到緩衝區
  • 如果不一樣,然後把緩衝區中的字符串數組,並開始一個新的緩衝區
0

你需要用前瞻與分裂向後看。

This Works。當然,如果你想包括更多的元素,提高正則表達式。

public static void main(String[] args) { 
    String input = "100+(0.03*55)/45-(25+55)"; 
    String test[] = input.split("((?<=[\\+\\-\\*\\/\\(\\)\\{\\}\\[\\]])|(?=[\\+\\-\\*\\/\\(\\)\\{\\}\\[\\]]))"); 
    System.out.println(Arrays.toString(test)); 
} 

更新:

((?<=[a-z]]),意味着它會分裂基於任何字符幷包括splited陣列中的字符,以及之後的元素。

(?=[a-z]),表示它將基於任何字符進行拆分,並在每個元素之前將字符包含在拆分數組中。

|,是兩個正則表達式之間的或運算符。

[\\+\\-\\*\\/\\(\\)\\{\\}\\[\\]]),是匹配到可能的組合

+0

可以請你解釋它是如何工作的,或者你可以給我的鏈接,我可以研究這個分割函數如何工作???我用簡單的正則表達式使用分割函數,但不是這樣, –

+0

@ AfzalAshraf更新 –