我有以下伪代码,需要编写一个java方法来评估前缀表达式:
算法valueOfPrefixExpression(prefixExpression)
输入:前缀形式为有效的正整数算术表达式
返回值:前缀表达式的值
如果下一个标记是整数
返回整数
否则
阅读运营商,说op
firstOperand获取valueOfPrefixExpression(remainingExpression)
secondOperand获取valueOfPrefixExpression(remainingExpression)
return firstOperand op secondOperand
endif
我该怎么写这个方法?我尝试了这个,我认为它可能是正确的,但我得到一个“缺少返回语句”错误,所以我无法编译。假设args具有1个或多个元素,则仅调用假设方法。 (没有空数组)
public static int prefixAlgorithm(String[] args) {
for (int i = 0; i < args.length; i++) {
if (!args[i].equals("+") && !args[i].equals("-")
&& !args[i].equals("*") && !args[i].equals("/")) {
int operand = parseInt(args[i]);
return operand;
} else {
int firstOperand = prefixAlgorithm(Arrays.copyOfRange(args, i, (args.length - 1)));
int secondOperand = prefixAlgorithm(Arrays.copyOfRange(args, i, (args.length - 1)));
if (args[i].equals("+")) {
return firstOperand + secondOperand;
} else if (args[i].equals("-")) {
return firstOperand - secondOperand;
} else if (args[i].equals("*")) {
return firstOperand * secondOperand;
} else if (args[i].equals("/")) {
return firstOperand / secondOperand;
}
}
}
}
答案 0 :(得分:0)
import java.util.*;
public class PrefixEvaluator {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("This program evaluates prefix expressions");
System.out.println("for operators +, -, *, / and %");
System.out.print("expression? ");
System.out.println("value = " + evaluate(console));
}
// pre : input contains a legal prefix expression
// post: expression is consumed and the result is returned
public static double evaluate(Scanner input) {
if (input.hasNextDouble()) {
return input.nextDouble();
} else {
String operator = input.next();
double operand1 = evaluate(input);
double operand2 = evaluate(input);
return evaluate(operator, operand1, operand2);
}
}
// pre : operator is one of +, -, *, / or %
// post: returns the result of applying the given operator to
// the given operands
public static double evaluate(String operator, double operand1,
double operand2) {
if (operator.equals("+")) {
return operand1 + operand2;
} else if (operator.equals("-")) {
return operand1 - operand2;
} else if (operator.equals("*")) {
return operand1 * operand2;
} else if (operator.equals("/")) {
return operand1 / operand2;
} else if (operator.equals("%")) {
return operand1 % operand2;
} else {
throw new RuntimeException("illegal operator " + operator);
}
}
}
import java.util.*;
public class PrefixEvaluator {
public static void main(String[] args) {
String input = "- * + 4 3 2 5";
String[] expression = input.split ( " " );
Queue<String> expressionQueue = new LinkedList<String>();
for (String element : expression)
{
expressionQueue.add ( element );
}
System.out.println("value = " + evaluate(expressionQueue));
}
// pre : input contains a legal prefix expression
// post: expression is consumed and the result is returned
public static double evaluate(Queue <String> input) {
if(input.peek ( ) != null && input.peek ( ).matches ( "^(-?)\\d+$" ))
{
return Long.parseLong ( input.poll ( ) );
}
else
{
String operator = input.poll();
double operand1 = evaluate(input);
double operand2 = evaluate(input);
return evaluate(operator, operand1, operand2);
}
}
// pre : operator is one of +, -, *, / or %
// post: returns the result of applying the given operator to
// the given operands
public static double evaluate(String operator, double operand1,
double operand2) {
if (operator.equals("+")) {
return operand1 + operand2;
} else if (operator.equals("-")) {
return operand1 - operand2;
} else if (operator.equals("*")) {
return operand1 * operand2;
} else if (operator.equals("/")) {
return operand1 / operand2;
} else if (operator.equals("%")) {
return operand1 % operand2;
} else {
throw new RuntimeException("illegal operator " + operator);
}
}
}
该程序评估前缀表达式
表示运算符+, - ,*,/和%
表达? - * + 4 3 2 5
value = 9.0
答案 1 :(得分:0)
您的编译问题是因为您没有从该方法获得保证返回。如果arg [i]不是整个数组的四个预期字符之一,那么你只需要运行函数的底部。
你可能认为输入总是符合期望,但是知识比知道人类要好。 : - )