我制作了一个后缀计算器,我很确定它有效,但它是一个读数错误。计算器用于从文件中的行读取后缀表示法中的数字并计算它们。但是它不识别行,例如,如果我有两行,它将组合两个表达式。我有文件阅读的麻烦所以任何帮助将不胜感激。对不起任何格式问题这是我的第一篇文章。所以这是我的代码所以我的问题更好理解。
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类错误但我不确定如何解决这个问题而不破坏我的程序。
答案 0 :(得分:0)
while (line != null) {
sb.append(line);
line = br.readLine();
}
基本上,您正在阅读每一行并将其附加到单个Stringbuffer
,这将导致文件中每一行的连接。此缓冲区不包含换行符或行结束的任何其他符号。
readline
:
读取一行文字。一条线被认为是由换行符('\ n'),回车符('\ r')或回车符后面的任何一个终止。
返回:包含行内容的String,不包括 任何行终止字符,如果流的末尾有,则返回null 已到达投掷:
如果您打算一次阅读所有内容,我建议将这些行添加到ArrayList中。