2013-10-24 272 views
-2

當我做了像d-c + a + b這樣的不同組合時,它給了我一個像118.0這樣的inccorect數字。誰能告訴我,在我的代碼我的計算是錯誤的.. 謝謝輸出不正確

的ValVarPairs.txt包含這些numbers-> A = 100,B = 5,c = 10,d = 13 這是我編碼。

package com.ecsgrid; 

import java.io.*; 

public class testC { 

public static void main(String[] args) { 
    int i = 0,j = 0; 
    double result, values[] = new double[4]; 
    char k, operators[] = new char[3]; 
    for (i = 0; i <= 2; i++) 
    operators[i] = '+';  // default is to add the values 

    File myfile; 
    StreamTokenizer tok; 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    String InputText; 

    i = 0; 
    try { 
    myfile = new File("C:\\VarValPairs.txt"); 
    tok = new StreamTokenizer(new FileReader(myfile)); 
    tok.eolIsSignificant(false); 

    while ((tok.nextToken() != StreamTokenizer.TT_EOF) && (i <= 3)){ 
     if ((tok.ttype == StreamTokenizer.TT_NUMBER)) 
     values[i++] = tok.nval; 
     } 
    } 
    catch(FileNotFoundException e) { System.err.println(e); return; } 
    catch(IOException f) { System.out.println(f); return; } 

    System.out.println("Enter letters and operators:"); 

    try { 
    InputText = in.readLine(); 
    } 
    catch(IOException f) { System.out.println(f); return; } 

    for (i = 0; i < InputText.length(); i++) 
    { 
    k = InputText.charAt(i); 
    if ((k == '+') || (k == '-')) 
    { 
     if (j <= 2) operators[j++] = k; 
    } 
    } 

    result = values[0]; 
    for (i = 0; i <= 2; i++){ 
    if (operators[i] == '+') 
    result = result + values[i+1]; 
    else 
    result = result - values[i+1]; 
    } 
    System.out.println(result); 
} 
} 
+0

做一些調試 – Nishant

+0

學會使用調試器 – segfault

+0

不要再發表類似這樣的重複。 –

回答

1

讓我們調試這一點,增加一些系統出局......

這是你會看到每一步 100.0 - 5.0 95.0 + 10.0 105.0 + 13.0 118.0

您值數組是{100,5,10,13},而你的算子數組是{ - ,+,+}

你沒有映射a = 100,b = 5,c = 10,d = 13,除非你然後映射那些使用基於非操作數輸入鍵的映射來解析操作數的操作,它不會起作用。所以,如果我要使用字符的int值,我可以用這種方式翻譯它。

import java.io.*; 

public class TestC { 

    public static void main(String[] args) { 
     int i = 0, j = 0; 
     double result, values[] = new double[4]; 
     char k, operatorsAndOperands[] = new char[3]; 
     for (i = 0; i <= 2; i++) 
      operatorsAndOperands[i] = '+'; // default is to add the values 

     File myfile; 
     StreamTokenizer tok; 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     String InputText; 

     i = 0; 
     try { 
      myfile = new File("C:\\VarValPairs.txt"); 
      tok = new StreamTokenizer(new FileReader(myfile)); 
      tok.eolIsSignificant(false); 

      while ((tok.nextToken() != StreamTokenizer.TT_EOF) && (i <= 3)) { 
       if ((tok.ttype == StreamTokenizer.TT_NUMBER)) 
        values[i++] = tok.nval; 
      } 
      for (int l = 0; l < values.length; l++) { 
       System.out.println(values[l]); 
      } 
     } catch (FileNotFoundException e) { 
      System.err.println(e); 
      return; 
     } catch (IOException f) { 
      System.out.println(f); 
      return; 
     } 

     System.out.println("Enter letters and operators:"); 

     try { 
      InputText = in.readLine().toUpperCase(); 
     } catch (IOException f) { 
      System.out.println(f); 
      return; 
     } 

     if(InputText.length() > 0){ 
      operatorsAndOperands = new char[InputText.length()]; 
     } else { 
      System.out.println("No Operations specified"); 
      return; 
     } 
     for (i = 0; i < InputText.length(); i++) { 
      k = InputText.charAt(i); 
      operatorsAndOperands[j++] = k; 
     } 

     result = 0; 
     for (i = 0; i < operatorsAndOperands.length; i++) { 
      System.out.println(operatorsAndOperands[i] + " " + (int)operatorsAndOperands[i]); 
      if(i+1<operatorsAndOperands.length) 
       System.out.println(operatorsAndOperands[i+1]); 
      switch(operatorsAndOperands[i]){ 
      case '+': 
       if(operatorsAndOperands[i+1] != '+' && operatorsAndOperands[i+1] != '-'){ 
        result+=values[(int)operatorsAndOperands[i+1] - (int)'A']; 
        i++; 
       } 
       break; 
      case '-': 
       if(operatorsAndOperands[i+1] != '+' && operatorsAndOperands[i+1] != '-'){ 
        result-=values[(int)operatorsAndOperands[i+1] - (int)'A']; 
        i++; 
       } 
       break; 
      default: 
       result = values[(int)operatorsAndOperands[i] - (int)'A']; 
       break; 
      }; 
      System.out.println(result); 
     } 
     System.out.println(result); 
    } 
} 
+0

我應該如何映射它? – soupi7

+0

一種方法是使用Hashmap將a映射爲100 b至5等,或者使用字符的int值來獲取運算符值,請參閱我的編輯。 –

+0

非常感謝你的神話,我已經在你的幫助下解決了它。 – soupi7

2

現在你會得到相同的輸出,如果你的投入是-++

你永遠不解析訂單或A,B,c和d。你總是假定順序a-> b-> c-> d。

所以d-C + A + B將是:A-B + C + d這與你提供的輸出(100-5 + 10 + 13 = 118)

OP的CODE

一致
for (i = 0; i < InputText.length(); i++) 
    { 
    k = InputText.charAt(i); 
    if ((k == '+') || (k == '-')) 
    { 
     if (j <= 2) operators[j++] = k; 
    } 
    } 

/OP的行爲準則

在這個循環中,當k不是一個運營商,你應該讀w ^這是它的信,並存儲字母出現的順序。或者建立一些其他類型的映射。在任何情況下,您都不能忽略非運算符字符,因爲它們是輸入的一部分。

+0

如果K不是 – soupi7

+0

@ soupi7 k,我該如何選擇哪一個是運營商?在某些情況下,每個角色都是如此。你現在正在遍歷整個字符串,找到運算符,並將它們放入一個數組中。當你這樣做。如果k不是運算符(在if語句中是else),那麼您需要實現某種邏輯來保存它是哪個字符。在第一次迭代中,k將是d。把'd'放到一個數組中,你知道它是第一個。 – Cruncher

+0

我把d放在arrray中,同樣的問題仍然存在 – soupi7