-2

当我做不同的组合(如 d-c+a+b)时,它会给我一个错误的数字,如 118.0。有人可以告诉我我的代码在哪里我的计算是错误的..谢谢

ValVarPairs.txt 包含这些数字-> 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);  
 }
}
4

2 回答 2

2

现在,如果您的输入是-++

您永远不会解析订单或 a、b、c 和 d。你总是假设顺序 a->b->c->d。

所以 d-c+a+b 将是: a-b+c+d 与您提供的输出一致(100-5+10+13 = 118)

OP的代码

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

/操作代码

在这个循环中,当 k 不是运算符时,您应该读取它是哪个字母,并存储字母出现的顺序。或者构建其他类型的映射。在任何情况下,您都不能只忽略非操作符字符,因为它们是输入的一部分。

于 2013-10-24T17:16:19.183 回答
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);
    }
}
于 2013-10-24T17:26:37.943 回答