2013-10-24 273 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