【问题标题】:Java Reading Lines and Doing Math EquationsJava 阅读线和做数学方程式
【发布时间】:2013-07-19 17:49:30
【问题描述】:

所以我有这个项目要做,我需要读取一个名为 Input 的文本文件,我正在这样做:

    public static void textParser() {
    File inputFile = new File("Input.txt");
    try {
        BufferedReader br = new BufferedReader(new FileReader(inputFile));
        String inputsText;
        while ((inputsText = br.readLine()) != null) {
            System.out.println(inputsText);
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

它有效。 Input.txt里面显示:

6
10 + 4
12 - 3
1000 / 50
9 * 64
2^5
90 % 8
1 + 1
6 * 4

第一行 (6) 将始终是待处理方程式的数量,可以不同于 6。 然后我必须做第一行所说的多少个方程,我将如何继续这样做?谢谢!

【问题讨论】:

  • 您应该更改的第一件事是将文件读入 ArrayList 以便您可以将字符串值存储在比标准输出更临时的位置。
  • @Kon 没有提供使用 ArrayList 的理由,您为什么建议这样做?
  • 他打算对文件里面的数据进行操作。他正在阅读文件,但直接将其打印到 sdout。
  • @Kon 当然,但我不认为 arraylist 是正确的结构。
  • 对于计算,我会使用ScriptEngine, e.g.

标签: java math equation


【解决方案1】:

您需要编写一个解析器。无需为您做功课,这是足够的伪代码:

for line in ReadFile()  
{  
  for token in split(line,expression)  
  {  
      if token is digit  
         digits.enqueue(token) 
      if token is symbol  
         symbols.enqueue(token)    
  }  
     for element in digits,symbols:   
         applySymbol(firstDigit,secondDigit,symbol)
}  

【讨论】:

  • 很好的答案,但是您不考虑优先顺序。由于没有明确要求...+1!
  • 嗯,这很令人困惑..但感谢伪,我会试着弄清楚
  • @user2230236 有什么令人困惑的地方?
  • 我只是不知道该怎么做。我还是个初学者,但我想这样做,但不知道怎么做。我可以阅读文本文件,但后来我被难住了。
【解决方案2】:

我已经用不同的语言解决了这个问题几次。查看Shunting-yard algorithm

基本上,您将运算符和操作数推送和弹出到优先级队列中。您基本上是将中缀转换为后缀。一旦您的方程采用后置表示法,它就更容易求解。

如果您没有优先顺序,则担心问题会简单得多,但仍然可以通过相同的方法解决。

编辑:

我们人类在修复符号中使用: 3 + 5 - 1 运算符在操作数之间。

在 Post fix 表示法中如下所示: 3 5 + 1 -

运算符出现在操作数之后。以这种方式编写的方程很容易评估。您只需将操作数压入堆栈,然后使用 next 运算符评估最后 2 个。所以在这里,你将 3 和 5 压入堆栈。然后遇到 + 运算符,因此将 3 和 5 相加,得到 8。将 8 压入堆栈。现在你读到了 1。将 1 压入堆栈。现在你读到-。从 1 中减去 8。你会得到 7 的答案。

调车场算法告诉你如何在中缀和后缀之间转换。

祝你好运!

【讨论】:

  • 感谢您的链接先生,非常有帮助。但是你能提供一个Java的例子吗?谢谢!
  • 是的,我可以在家里的电脑上使用。但我宁愿不为你解决这个任务。它会教你很多东西!
  • 嗯,我还是很困惑,不知道从哪里开始
  • 您到底对什么感到困惑?你知道post-fix和in-fix的区别吗?你读过调车场算法吗? Woot4Moo 发布了一些很好的伪代码作为指南。您了解数据结构,并且需要在其中存储运算符和操作数吗?
  • 看了Shunting算法,还是不知道post-fix和in-fix的区别。 :\
【解决方案3】:

一个选项是使用 ANTLR 生成解析器,这个 tutorial 几乎涵盖了你想要做的事情

【讨论】:

    【解决方案4】:

    首先你需要将它们存储在一个字符串数组中

    然后获取数组中的第一个元素,并将其转换为整数。

    基于整数值循环必须被迭代。所以形成了循环。现在你需要从下一个索引开始读取字符串数组。

    首先要进行算术运算,您需要有一个由 4 个字符组成的数组 '+','-','*','%'

    根据 char 数组拆分字符串。这你可以把它作为一个单独的功能来做。因为每次都需要调用它。我说的是性能。

    然后你会得到这两个值的解析和它们的拆分操作符。

    现在您可以执行算术运算了。

    这就是你所需要的。

    【讨论】:

      【解决方案5】:

      我终于找到了另一种可行的方法,这就是我的做法:

          public static void textParser() {
          File inputFile = new File("Input.txt");
          try {
              Scanner scanner = new Scanner(inputFile);
              int numberOfQuestions = Integer.parseInt(scanner.next());
              for (int i = 1; i <= numberOfQuestions; i++) {
                  int firstInt = Integer.parseInt(scanner.next());
                  String operationSign = scanner.next();
                  int secondInt = Integer.parseInt(scanner.next());
                  if (operationSign.contains("+")) {
                      int answer = firstInt + secondInt;
                      System.out.println("Equation " + i + " : " + firstInt
                              + " + " + secondInt + " = " + answer);
                  } else if (operationSign.contains("-")) {
                      int answer = firstInt - secondInt;
                      System.out.println("Equation " + i + " : " + firstInt
                              + " - " + secondInt + " = " + answer);
                  } else if (operationSign.contains("/")) {
                      int answer = firstInt / secondInt;
                      System.out.println("Equation " + i + " : " + firstInt
                              + " / " + secondInt + " = " + answer);
                  } else if (operationSign.contains("*")) {
                      int answer = firstInt * secondInt;
                      System.out.println("Equation " + i + " : " + firstInt
                              + " * " + secondInt + " = " + answer);
                  } else if (operationSign.contains("%")) {
                      int answer = firstInt % secondInt;
                      System.out.println("Equation " + i + " : " + firstInt
                              + " % " + secondInt + " = " + answer);
                  }
              }
      
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      

      感谢大家的帮助!

      【讨论】:

        猜你喜欢
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2017-01-16
        • 2019-10-29
        • 2016-05-27
        • 2019-11-05
        • 1970-01-01
        相关资源
        最近更新 更多