好的,我必须从文件中读取后缀表达式.后缀表达式必须具有空格以分隔每个运算符或操作数.到目前为止,只有在输入文件中的运算符或操作数之间没有空格时,我才能使用它.(即如果文件有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 …Run Code Online (Sandbox Code Playgroud)