java - How to find value of prefix expression using a recursive method? -
i have following pseudocode , need write java method evaluate prefix expression:
algorithm valueofprefixexpression(prefixexpression)
input: valid positive-integer arithmetic expression in prefix form
return value: value of prefix expression
if next token integer
return integer
else
read operator, op
firstoperand gets valueofprefixexpression(remainingexpression)
secondoperand gets valueofprefixexpression(remainingexpression)
return firstoperand op secondoperand
endif
how can write method? tried , think right getting "missing return statement" error can't compile. assume method called if args has 1 or more elements. (no empty arrays)
public static int prefixalgorithm(string[] args) { (int = 0; < 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; } } } }
prefixevaluator input , scanner:
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 +, -, *, / , %"); system.out.print("expression? "); system.out.println("value = " + evaluate(console)); } // pre : input contains legal prefix expression // post: expression consumed , result 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 1 of +, -, *, / or % // post: returns result of applying given operator // 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); } } }
prefixevaluator no input , queue:
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>(); (string element : expression) { expressionqueue.add ( element ); } system.out.println("value = " + evaluate(expressionqueue)); } // pre : input contains legal prefix expression // post: expression consumed , result 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 1 of +, -, *, / or % // post: returns result of applying given operator // 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); } } }
i/o example:
this program evaluates prefix expressions
for operators +, -, *, / , %
expression? - * + 4 3 2 5
value = 9.0
documentation:
queues: queue (java platform se 7 )
patterns: pattern (java platform se 7 )
Comments
Post a Comment