In this question, you will write a function that calculates the result of an ari
ID: 3792580 • Letter: I
Question
In this question, you will write a function that calculates the result of an arithmetic expression involving scalars, where the arithmetic expression is given as a character string (e.g. 2 30+5 4'). The operators allowed in the arithmetic expression are and which represent addition, subtraction, multiplication, division, and exponentiation, respectively. However, the order of operations has been changed! Addition and subtraction now have precedence over multiplication and division, which now have precedence over exponentiation. Addition and subtraction still have equal precedence. Similarly, multiplication and division still have equal precedence. Operators of equal precedence should be evaluated from right. Write a function with the following header: function [result] my calculator inverse precedence (expression) where: expression is a row vector of class char that represents an arithmetic expression as described above. result is a scalar of class double that represents the value of the arithmetic expression described by expression. You can assume that: expression is not empty. expression is a valid arithmetic expression. expression contains only characters among: Theta 123456789. + */^. In particular, expression does not contain spaces nor parentheses. The first character in expression is one of the 10 digits. There are no two operators in a row (e.g., in expression You may not use Matlab's built-in functions split, in this question.Explanation / Answer
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); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.