好的,我必须从文件中读取后缀表达式。后缀表达式必须具有空格以分隔每个运算符或操作数。到目前为止,只有在输入文件中的运算符或操作数之间没有空格时,我才能使用它。 (即如果文件有12+,我得到的结果是3.)为了做到这一点,我认为我需要标记输入,但我不知道如何。这就是我到目前为止所拥有的。感谢您的回复。
import java.util.*;
import java.io.*;
public class PostfixCalc{
public static void main (String [] args) throws Exception {
File file = new File("in.txt");
Scanner sc = new Scanner(file);
String input = sc.next();
Stack<Integer> calc = new Stack<Integer>();
while(sc.hasNext()){
for(int i = 0; i < input.length(); i++){
char c = input.charAt(i);
int x = 0;
int y = 0;
int r = 0;
if(Character.isDigit(c)){
int t = Character.getNumericValue(c);
calc.push(t);
}
else if(c == '+'){
x = calc.pop();
y = calc.pop();
r = x+y;
calc.push(r);
}
else if(c == '-'){
x = calc.pop();
y = calc.pop();
r = x-y;
calc.push(r);
}
else if(c == '*'){
x = calc.pop();
y = calc.pop();
r = x*y;
calc.push(r);
}
else if(c == '/'){
x = calc.pop();
y = calc.pop();
r = x/y;
calc.push(r);
}
}
}
int a = calc.pop();
System.out.println(a);
}
}
答案 0 :(得分:2)
您需要更改几件事,您可以一步一步地完成。
Stack
包含Integer
而不是Character
s。String
而不是Character
s。Integer.parseInt()
解析操作数。这会将String
转换为Integer
s。 (实际上,它会将它们转换为int
s,但在您的情况下,这种差异并不重要。)Scanner.useDelimiter()
设置扫描仪分隔符为\s+
,这将匹配任何空格字符的序列。当然有无数其他方法可以处理您的输入,但我试图让您了解如何更改现有代码以执行其需要执行的操作。
答案 1 :(得分:2)
为了标记化,您可以将String.split()
与单个空格一起用作分隔符;
String[] inputs = input.split(" ");
以下是我刚刚撰写的完整解决方案,该解决方案使用基于单链表的堆栈实现来制作后缀计算器;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class PostFixCalculator {
private static final String ADD = "+";
private static final String SUB = "-";
private static final String MUL = "*";
private static final String DIV = "/";
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 int calculate(String input) {
SinglyLinkedListStack<Integer> stack = new SinglyLinkedListStack<>();
String[] inputs = input.split(" ");
return handleCalculation(stack, inputs);
}
private static int handleCalculation(SinglyLinkedListStack<Integer> stack, String[] el) {
int 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) ) {
operand2 = stack.pop();
operand1 = stack.pop();
switch(el[i]) {
case ADD: {
int local = operand1 + operand2;
stack.push(local);
break;
}
case SUB: {
int local = operand1 - operand2;
stack.push(local);
break;
}
case MUL: {
int local = operand1 * operand2;
stack.push(local);
break;
}
case DIV: {
int local = operand1 / operand2;
stack.push(local);
break;
}
}
} else {
stack.push(Integer.parseInt(el[i]));
}
}
return stack.pop();
}
}
public class SinglyLinkedListStack<T> {
private int size;
private Node<T> head;
public SinglyLinkedListStack() {
head = null;
size = 0;
}
public void push(T element) {
if(head == null) {
head = new Node(element);
} else {
Node<T> newNode = new Node(element);
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("postfix.txt");
}
}
6 5 2 3 + 8 * + 3 + *
6 5 2 3 + 8 * + 3 + * = 288
答案 2 :(得分:0)
您不需要扫描仪
只需使用BufferedReader来读取文件,然后使用它的方法readLine来获取行
然后使用
String tokens[] = line.split("\\s+?")
您将获得“令牌”数组,可以在您的代码中处理。
要识别号码,您可以使用以下正则表达式:
Pattern isNumber = Pattern.compile("^\\d+?$")
if (isNumber.matcher(token).matches()) {
push(Integer.parseInt(token));
}