后缀计算器Java
我必须从文件中读入后缀表达式。后缀表达式必须有空格分隔每个运算符或操作数。到目前为止,只有在输入文件中的运算符或操作数之间没有空格的情况下,我的方法才有效。(也就是说,如果文件有12+,我得到的结果是3。)为了做到这一点,我认为我需要标记输入,但我不确定如何标记。这就是我目前所拥有的。谢谢你的回复后缀计算器Java,java,calculator,postfix-notation,stack,Java,Calculator,Postfix Notation,Stack,我必须从文件中读入后缀表达式。后缀表达式必须有空格分隔每个运算符或操作数。到目前为止,只有在输入文件中的运算符或操作数之间没有空格的情况下,我的方法才有效。(也就是说,如果文件有12+,我得到的结果是3。)为了做到这一点,我认为我需要标记输入,但我不确定如何标记。这就是我目前所拥有的。谢谢你的回复 import java.util.*; import java.io.*; public class PostfixCalc{ public static void main (String [] a
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);
}
}
import java.util.*;
导入java.io.*;
公共类PostfixCalc{
公共静态void main(字符串[]args)引发异常{
File File=新文件(“in.txt”);
扫描仪sc=新扫描仪(文件);
字符串输入=sc.next();
堆栈计算=新堆栈();
while(sc.hasNext()){
对于(int i=0;i
有几件事你需要改变,你可以一步一步地做
堆栈
声明为包含整数
s,而不是字符
s字符串
s而不是字符
sInteger.parseInt()
解析操作数。这将把String
s转换为Integer
s。(实际上,它将它们转换为int
s,但在您的情况下,这种差异并不重要。)scanner.useDelimiter()
将扫描仪分隔符设置为\s+
,这将匹配任何空白字符的序列当然,还有无数其他方法可以处理您的输入,但我试图让您了解如何更改现有代码以执行它所需的操作。您不需要扫描仪 只需使用读取文件,然后使用其readLine方法获取行 然后使用
String tokens[] = line.split("\\s+?")
您将获得“令牌”数组,可以在您的代码中进行处理
要识别号码,可以使用以下正则表达式:
Pattern isNumber = Pattern.compile("^\\d+?$")
if (isNumber.matcher(token).matches()) {
push(Integer.parseInt(token));
}
为了标记化,可以使用
String.split()
和一个空格作为分隔符
String[] inputs = input.split(" ");
这是我刚刚编写的完整解决方案,它使用基于单链表的堆栈实现来制作后缀计算器
A-postfix计算器
D-示例输入文件:“postfix.txt”
电子演示输出
看一看。默认情况下,它在空白处(空格、制表符、换行符等)标记。在这里使用扫描仪实际上更容易<默认情况下,code>next()将检索下一个空格分隔的标记。当然,之后可以使用模式。
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