Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

!!! Don't not add or change code except within the //commented instructions. Program to understands operator precedence: a. Implement: int precedence (char op) // Return

!!! Don't not add or change code except within the //commented instructions.
Program to understands operator precedence: a. Implement: int precedence (char op) // Return the precedence value of op. For example, // precedence('/') returns 2. b. Modify processOperator to use the following logic: While the top element (if there is one) of the operator stack has precedence >= than the precedence of op, evaluate it (call evaluateTopOperator()), Program should works with parentheses. You should only have to modify processOperator
Program must understands unary minus: -1 - -3 = 2
add a boolean variable previousWasNumberOrClose to Calculator that is true if the previous token was a number or a close parenthesis ')'. Set it to the appropriate value whenever doNumber or doOperator is called. If doOperator is called with '-', use this variable to decide between calling processOperator with 'u' instead of op. b. Processing unary '-': to process 'u' in processOperator, just push it on the operator stack. That's it! c. Evaluating unary 'u': in evaluateTopOperator, if the top is 'u', pop the number stack JUST ONCE, negate the number (-a), and push it again.
 package prog05; import java.util.Stack; import java.util.Scanner; import javax.swing.SwingUtilities; import prog02.UserInterface; import prog02.GUI; import prog02.ConsoleUI; public class Calculator { static final String OPERATORS = "()+-*/u^"; static final int[] PRECEDENCE = { -1, -1, 1, 1, 2, 2, 3, 4 }; Stack operatorStack = new Stack(); Stack numberStack = new Stack(); UserInterface ui = new GUI("Calculator"); Calculator (UserInterface ui) { this.ui = ui; } void emptyStacks () { while (!numberStack.empty()) numberStack.pop(); while (!operatorStack.empty()) operatorStack.pop(); } String numberStackToString () { String s = "numberStack: "; Stack helperStack = new Stack(); // EXERCISE // Put every element of numberStack into helperStack // You will need to use a loop. What kind? // What condition? When can you stop moving elements out of numberStack? // What method do you use to take an element out of numberStack? // What method do you use to put that element into helperStack. // Now put the back, but also add each one to s: // s = s + " " + number; return s; } String operatorStackToString () { String s = "operatorStack: "; // EXERCISE return s; } void displayStacks () { ui.sendMessage(numberStackToString() + " " + operatorStackToString()); } void doNumber (double x) { numberStack.push(x); displayStacks(); } void doOperator (char op) { processOperator(op); displayStacks(); } double doEquals () { while (!operatorStack.empty()) evaluateTopOperator(); return numberStack.pop(); } double evaluateOperator (double a, char op, double b) { switch (op) { case '+': return a + b; // EXERCISE } System.out.println("Unknown operator " + op); return 0; } void evaluateTopOperator () { char op = operatorStack.pop(); // EXERCISE displayStacks(); } void processOperator (char op) { operatorStack.push(op); } static boolean checkTokens (UserInterface ui, Object[] tokens) { for (Object token : tokens) if (token instanceof Character && OPERATORS.indexOf((Character) token) == -1) { ui.sendMessage(token + " is not a valid operator."); return false; } return true; } static void processExpressions (UserInterface ui, Calculator calculator) { while (true) { String line = ui.getInfo("Enter arithmetic expression or cancel."); if (line == null) return; Object[] tokens = Tokenizer.tokenize(line); if (!checkTokens(ui, tokens)) continue; try { for (Object token : tokens) if (token instanceof Double) calculator.doNumber((Double) token); else calculator.doOperator((Character) token); double result = calculator.doEquals(); ui.sendMessage(line + " = " + result); } catch (Exception e) { ui.sendMessage("Bad expression."); calculator.emptyStacks(); } } } public static void main (String[] args) { UserInterface ui = new ConsoleUI(); Calculator calculator = new Calculator(ui); processExpressions(ui, calculator); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions