后缀计算器的文件读取错误

File reading error with postfix calculator

我制作了一个 post 修正计算器,我很确定它可以工作,但它是一个读取错误。该计算器旨在从文件中的一行读取 postfix 表示法中的数字并计算它们。但是它不识别行,例如,如果我有两行,它将组合两个表达式。我在读取文件时遇到问题,因此将不胜感激。抱歉,对于任何格式问题,这是我的第一个 post。所以这是我的代码,以便更好地理解我的问题。

import java.io.*;
import java.util.*;
import java.nio.charset.Charset;


public class PostfixCalculator {

private static final String ADD = "+"; 
private static final String SUB = "-";
private static final String MUL = "*";
private static final String DIV = "/";
private static final String EXP = "^"; 
private static final String NEG = "_";  //unary negation
private static final String SQRT = "#";

public void calculateFile(String fileName) throws IOException {
    BufferedReader br = null;
    StringBuilder sb = null;
    try {
        FileReader fileReader = new FileReader(fileName);
        br = new BufferedReader(fileReader);

        sb = new StringBuilder();
        String line = br.readLine();

        while (line  != null) {
            sb.append(line);
            line = br.readLine();
        }

        String input = sb.toString();
        System.out.println(input + " = " + calculate(input));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        br.close();
    }


   } 

private double calculate(String input) {
    SinglyLinkedListStack<Double> stack = new SinglyLinkedListStack<Double>();

    String[] inputs = input.split(" ");

    return handleCalculation(stack, inputs);
}

private static double handleCalculation(SinglyLinkedListStack<Double> stack, String[] el) {
    double operand1, operand2;

    for(int i = 0; i < el.length; i++) {
        if( el[i].equals(ADD) || el[i].equals(SUB) || el[i].equals(MUL) || el[i].equals(DIV)||el[i].equals(EXP)||el[i].equals(NEG)||el[i].equals(SQRT)) {
            operand2 = stack.pop();
            operand1 = stack.pop();
            switch(el[i]) {
                case ADD: {
                    double local = operand1 + operand2;
                    stack.push(local);
                    break;
                }

                case SUB: {
                    double local = operand1 - operand2;
                    stack.push(local);
                    break;
                }

                case MUL: {
                    double local = operand1 * operand2;
                    stack.push(local);
                    break;
                }

                case DIV: {
                    double local = operand1 / operand2;
                    stack.push(local);
                    break;
                }
                case EXP: {
                    double local = Math.pow(operand1, operand2);
                    stack.push(local);
                    break; 

                }
                case NEG: {
                    double local = -(operand1);
                    stack.push(local);
                    break;
                }
                case SQRT:{ 
                    double local = Math.pow(operand1, .5);
                    stack.push(local);
                    break; 
                }

            }
        } else {
            stack.push(Double.parseDouble(el[i]));
        }
    }

    return (double)stack.pop();
}

}

这是我的链表

public class SinglyLinkedListStack<T> {

private int size;
private Node<T> head;

public SinglyLinkedListStack() {
    head = null;
    size = 0;
}

public void push(double local) {
    if(head == null) {
        head = new Node(local);
    } else {
        Node<T> newNode = new Node(local);
        newNode.next = head;
        head = newNode;
    }

    size++;
}

public T pop() {
    if(head == null)
        return null;
    else {
        T topData = head.data;

        head = head.next;
        size--;

        return topData;
    }
}

public T top() {
    if(head != null)
        return head.data;
    else
        return null;
}

public int size() {
    return size;
}

public boolean isEmpty() {
    return size == 0;
}

private class Node<T> {
    private T data;
    private Node<T> next;

    public Node(T data) {
        this.data = data;
    }

}


}

这是我的演示

import java.io.IOException;

public class PostFixCalculatorDemo {
 public static void main(String[] args) throws IOException {
    PostfixCalculator calc = new PostfixCalculator();
    calc.calculateFile("in.dat");
}
}

文本文件包含

2 3 ^ 35 5 / -

1 2 + 3 * # 4 - 5 6 - + _

我的输出是 2 3 ^ 35 5 / -1 2 + 3 * # 4 - 5 6 - + _ = -8.0 正如所见,它正在结合两条线。我希望它分别读取这两行并计算 2 3 ^ 35 5 / - 和 1 2 + 3 * # 4 - 5 6 - + _ 而不是将它们放在一起。我知道它这样做是因为我将 calculateFile class 编程错了,但我不确定如何在不破坏我的程序的情况下解决这个问题。

 while (line  != null) {
        sb.append(line);
        line = br.readLine();
    }

基本上,您正在读取每一行并将其附加到单个 Stringbuffer,这将导致文件中每一行的串联。此缓冲区不会包含换行符或行结束处的任何其他符号。

来自documentation

readline:

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached Throws:

如果您打算一次读取所有内容,我建议将这些行添加到 ArrayList。