私はポストフィックスの計算機を作ったが、読んだときのエラーとしてはうまくいくとは思う。計算機は、ファイル内の行からポストフィックス表記の数値を読み込んで計算するように設計されています。しかし、それは行を認識しません。たとえば、2つの行がある場合、2つの式が結合されます。私はファイルの読み込みに問題があるので、どんな助けも高く評価されます。すべてのフォーマットの問題で申し訳ありませんが、これは私の最初の投稿です。だからここに私の問題が分かりやすいように私のコードです。ポストフィックス計算でのファイルの読み込みエラー
-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();
}
}
これは、テキストファイルが
2 3^35 5 /が含まれてい
import java.io.IOException;
public class PostFixCalculatorDemo {
public static void main(String[] args) throws IOException {
PostfixCalculator calc = new PostfixCalculator();
calc.calculateFile("in.dat");
}
}
私のデモ
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;
}
}
}
である。これは、私のリンクリストされます
1 2 + 3 *#4 - 5 - 6 5 - 6 - + _ =そのまま-8.0 + _
私の出力は、#4 * 2^35 5/-1 2 + 3それは2つの線を結合しているのを見た。私は2本の線を別々に読んで、2 3^35 5/- と1 2 + 3 *#4 - 5 6 - + _をまとめずに計算してほしい。私はcalculateFileクラスを間違ってプログラムしたのでこれを行うことは分かっていますが、プログラムを壊さずにこれを修正する方法がわかりません。
エラーはどこですか? – shmosel
エラーは、ファイルを行単位で読み取ることができないということです。 – masalaman
これは単なるあいまいな問題の説明です。エラーがある場合は、エラーメッセージとスタックトレースが必要です。ここに投稿してください。 – shmosel