簡體   English   中英

Java閱讀線和做數學方程式

[英]Java Reading Lines and Doing Math Equations

因此,我有一個項目要做,我需要讀取一個名為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。然后我必須執行第一行所說的方程式的數量,我將如何繼續這樣做? 謝謝!

您需要編寫一個解析器。 如果不為您做家庭作業,這就是應該足夠的偽代碼:

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)
}  

我已經用不同的語言解決了幾次這個問題。 查看調車場算法

基本上,您將運算符和操作數推入並彈出到優先級隊列中。 您基本上是將infix轉換為post-fix。 一旦您的方程式采用后綴表示法,則更容易解決。

如果您沒有優先順序,則擔心該問題要簡單得多,但仍可以使用相同的方法解決。

編輯:

人類使用固定符號表示:3 + 5-1運算符位於操作數之間。

在后修復符號中看起來像這樣:3 5 +1-

運算符出現在操作數之后。 以這種方式編寫的方程式易於評估。 您只需將操作數壓入堆棧,然后使用next運算符求出最后2個。 所以在這里,您將3和5壓入堆棧。 然后遇到+運算符,因此將3和5相加,得到8。將8壓入堆棧。 現在您讀到1。將1壓入堆棧。 現在,您閱讀-。 從1減去8。得到的答案是7。

調車場算法告訴您如何在后綴和后綴之間進行轉換。

祝好運!

一種選擇是使用ANTLR生成解析器,本教程幾乎涵蓋了您嘗試做的事情

首先,您需要將它們存儲在字符串數組中

然后獲取數組中的第一個元素,並將其轉換為整數。

基於整數值,必須迭代循環。 這樣就形成了循環。 現在您需要開始從下一個索引讀取字符串數組。

首先進行算術運算,您需要具有4個字符的數組'+','-','*','%'

根據char數組分割字符串。 您可以將其作為單獨的功能來完成。 因為每次都需要調用它。 為了表現,我是說。

然后,您將解析兩個值,並拆分它們的運算符。

現在您可以執行算術運算了。

多數民眾贊成在它你所需要的。

我終於想出了一種不同的工作方式,這就是我的工作方式:

    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();
    }
}

謝謝大家的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM