Java 我能';我的后缀计算器不能正常工作
我不知道我应该在这里做什么,但我认为我的大部分代码都很好。我只能编辑Evaluate()方法内部的代码。请帮忙 这是我的课堂和我的主要方法Java 我能';我的后缀计算器不能正常工作,java,post,postfix-notation,evaluator,Java,Post,Postfix Notation,Evaluator,我不知道我应该在这里做什么,但我认为我的大部分代码都很好。我只能编辑Evaluate()方法内部的代码。请帮忙 这是我的课堂和我的主要方法 package labs.lab3; import java.util.Scanner; // Needed for the Scanner import java.io.*; // Needed for the File and IOException public class TestDriver { /** *
package labs.lab3;
import java.util.Scanner; // Needed for the Scanner
import java.io.*; // Needed for the File and IOException
public class TestDriver {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
System.out.printf("%-30s", "Postfix Expression");
System.out.printf("%-30s", "Evaluation Result");
System.out.println();
String filename = "./src/labs/lab3/PostfixExpressions.txt";
File file = new File(filename);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
String expression = inputFile.nextLine();
System.out.printf("%-30s", expression);
PostfixEvaluator evaluator = new PostfixEvaluator(expression);
System.out.printf("%-30s" , evaluator.Evaluate());
System.out.println();
}
inputFile.close();
}
}
这是我的修复后评估课程:
package labs.lab3;
import java.util.Stack;
import java.util.EmptyStackException;
import java.util.StringTokenizer;
public class PostfixEvaluator
{
private Stack<Integer> stack;
private String expression;
private String token;
public PostfixEvaluator(String e)
{
stack = new Stack<Integer>();
expression = e;
}
// Evaluate the postfix expression and return the evaluation result
public int Evaluate()
{
int op1,op2;
int result;
StringTokenizer st = new StringTokenizer(expression);//split the expression into tokens
String token=st.nextToken();
while (st.hasMoreTokens()){
if (Character.isDigit(token.charAt(0))) {
int value = Integer.parseInt(token);
stack.push(value);
}
else if (!Character.isDigit(token.charAt(0))) {
op1=stack.pop();
op2=stack.pop();
result = Calculate(op1,op2,token.charAt(0));
stack.push(result);
}
}
int answer = stack.pop();
return answer;
}
// Perform an operation on the two operands
public int Calculate(int operand1, int operand2, char operation)
{
int result = 0;
switch (operation)
{
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '/':
result = operand1 / operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '%':
result = operand1 % operand2;
break;
}
return result;
}
}
packagelabs.lab3;
导入java.util.Stack;
导入java.util.EmptyStackException;
导入java.util.StringTokenizer;
公共类后定影评估器
{
私有堆栈;
私有字符串表达式;
私有字符串令牌;
公共后固定计算器(字符串e)
{
堆栈=新堆栈();
表达式=e;
}
//计算后缀表达式并返回计算结果
公共int Evaluate()
{
int op1,op2;
int结果;
StringTokenizer st=new StringTokenizer(表达式);//将表达式拆分为标记
字符串标记=st.nextToken();
而(st.hasMoreTokens()){
if(Character.isDigit(token.charAt(0))){
int value=Integer.parseInt(令牌);
堆栈推送(值);
}
如果(!Character.isDigit(token.charAt(0)),则为else{
op1=stack.pop();
op2=stack.pop();
结果=计算(op1,op2,token.charAt(0));
stack.push(结果);
}
}
int answer=stack.pop();
返回答案;
}
//对两个操作数执行操作
公共整数计算(整数操作数1、整数操作数2、字符运算)
{
int结果=0;
开关(操作)
{
格“+”:
结果=操作数1+操作数2;
打破
案例'-':
结果=操作数1-操作数2;
打破
案例“/”:
结果=操作数1/操作数2;
打破
案例“*”:
结果=操作数1*操作数2;
打破
案例“%”:
结果=操作数1%操作数2;
打破
}
返回结果;
}
}
谢谢我看不到您在标记器中前进。您只需调用
nextToken
一次,在循环之外。代码的其余部分似乎表明
应该使用整个表达式,因此需要在内部调用nextToken
循环