2011-04-03 45 views
0

好的。所以我試圖編寫一個應用程序,以正確的操作順序解決問題(即PEMDAS,但沒有PE大聲笑)。我可能做錯了,但無論如何。我需要幫助的是,作爲數組中每個元素的迭代,如果它是一個運算符,我刪除兩個元素(運算符周圍的數字,因爲我將運算符替換爲正確的計算,所以具有三個元素的數組5 +5,將成爲一個元素的數組,10)這將是很好,但for語句的條件不會更新,換句話說,當我更新數組時,array.length變短,但是for-statement無法識別並繼續傳遞數組的邊界,這會導致outofbounds錯誤。解釋最好的辦法是我的代碼,所以這裏是:幫助更新循環條件

for (i = 0; i < splitEquation.length; i++){ 
     if (splitEquation[i].equals("*")){ 
      splitEquation[i] = Double.toString(Double.parseDouble(splitEquation[i - 1]) * Double.parseDouble(splitEquation[i + 1])); 
      splitEquation = removeIndex(splitEquation, i - 1); 
      splitEquation = removeIndex(splitEquation, i + 1); 
      solution += Double.parseDouble(splitEquation[i - 1]); 

     } 
} 

而且removeIndex():

private String[] removeIndex(String [] s, int index){ 
    String [] n = new String[s.length - 1]; 

    System.arraycopy(s, 0, n, 0, index - 1); 
    System.arraycopy(s, index, n, index - 1, s.length - index); 
    return n; 
} 

由於提前, -Eric

附:讓我知道如果你需要我的代碼做什麼任何澄清:)

回答

0

使用一個ArrayList會使你的生活輕鬆許多:

ArrayList<String> splitEquation = 
    new ArrayList<String>(Arrays.toList("5","*","5")); 

for (i = 0; i < splitEquation.size(); i++){ 
    if (splitEquation.get(i).equals("*")) { 
     splitEquation.set(i, 
      (Double.toString(Double.parseDouble(splitEquation.get(i - 1)) * 
       Double.parseDouble(splitEquation.get(i + 1)))); 
     splitEquation.remove(i - 1); // shortens array by 1 
     splitEquation.remove(i); // this used to be (i + 1) 
     i--; // move back one (your computed value) 
     solution += Double.parseDouble(splitEquation.get(i)); 

    } 
} 

話雖這麼說......你真的需要修改你的陣列到位了嗎?

編輯:問題現在更加清楚,數組正在被修改,因爲它需要被評估爲一系列表達式。他還希望增加加減:

遞歸函數是你的朋友:)

public static double result(ArrayList<String> mySequence, double total, int index) 
{ 
    if (index == 0) 
     total = Double.parseDouble(mySequence.get(index)); 

    if (index == (mySequence.size() - 1)) 
     return total; 
    else if (mySequence.get(index).equals("*")) 
     total *= Double.parseDouble(mySequence.get(index + 1)); 
    else if (mySequence.get(index).equals("/")) 
     total /= Double.parseDouble(mySequence.get(index + 1)); 
    else if (mySequence.get(index).equals("+")) 
    { 
     index++; 
     double start = Double.parseDouble(mySequence.get(index)); 
     total += result(mySequence, start, index); 
     return total; 
    } 
    else if (mySequence.get(index).equals("-")) 
    { 
     index++; 
     double start = Double.parseDouble(mySequence.get(index)); 
     total -= result(mySequence, start, index); 
     return total; 
    } 

    index++; 
    return result(mySequence, total, index); 

} 

public static void main(String args[]) 
{ 
    ArrayList<String> splitEquation = 
     new ArrayList<String>(
      Arrays.asList("5","*","5","/","5","*","4","-","3","*","8")); 

    double myResult = result(splitEquation, 0, 0); 
    System.out.println("Answer is: " + myResult); 
} 

輸出:

答案是:-4.0

+0

但是這到底會不會越界?如果我刪除它,for語句將繼續。修改它是我能想到的唯一方法:/謝謝你! – Eric 2011-04-03 16:32:19

+0

因爲'i - ;'它不會超出範圍 - 它將迭代器設置爲有效元素。然後它被循環遞增,並與'i 2011-04-03 16:37:36

+0

修改數組的目的是什麼?你想結束一個計算值的數組?爲什麼不使用第二個數組作爲結果? – 2011-04-03 16:43:45

0

爲什麼修改輸入數組,當你可以使用輸入數組作爲只讀並創建輸出ArrayLists(或更好的堆棧)?

+0

大聲笑。不知道該怎麼做。因爲那樣我就必須迭代每個新數組。我還是新來的這個東西:)。 – Eric 2011-04-03 16:35:01

+0

您試圖從for循環中修改數組或集合,這是一個災難處理 - 除非您可以使用Iterator刪除項目。更好地簡單地循環收集並在循環中提取信息,將其放入一個堆棧中(或者對於操作員來說是兩個,對於一個數字來說是一個),這只是另一個集合。 – 2011-04-03 16:37:32

0

我認爲如果你使用動態結構,比如ArrayList,你應該可以解決這個問題。

根據該:

容量是用於存儲在 列表中的元素的陣列 的大小。它總是至少與列表大小一樣大 。當元素被添加到ArrayList 時,其容量 會自動增長。

this以前SO後,使用動態數據結構應該解決您的問題,我認爲,同樣的機制適用於當您刪除,以及在列表中添加的元素。但是,您可能需要實施一些機制來跟蹤每次更換產品後的產品位置。