java 3 5 + 1 - Please remember, there are space(s) between operands/operators in the ex[1]pression. So your solution needs to think of this aspect. You will solve the problem as stated below:- (1)...


java


3 5 + 1 -


Please remember, there are space(s) between operands/operators in the ex[1]pression. So your solution needs to think of this aspect.


You will solve the problem as stated below:-


(1) [Design a simple calculator that helps you solve the expression


given.


Please be reminded that you need to design the calculator and not use


in-built math methods from the programming language library to solve


the expression. Also at the end of the program as a comment mention the time and space complexity of your solution. Time and space complexity



Post-fix String Expression<br>Create a stack to store operands (or values)<br>Scan the given expression and do the following for every scanned element<br>If the element is a number, push it into the stack<br>If the element is an operator, pop operands for the operator from the stack. Evaluate the operator<br>and push the result back to the stack<br>When the expression is ended, the number in the stack is the final answer<br>Post-fix String Expression (Example)<br>Expression => “2 3 1 * + 9 -“<br>Scan '2', it's a number, so push it to stack. Stack contains '2'<br>Scan 3', again a number, push it to stack, stack now contains '2 3’ (from bottom to<br>top)<br>Scan 1', again a number, push it to stack, stack now contains '2 3 1'<br>, it's an operator, pop two operands from stack, apply the * operator on<br>Scan<br>operands, we get 3*1 which results in 3. We push the result '3' to stack. The stack<br>now becomes '2 3'<br>

Extracted text: Post-fix String Expression Create a stack to store operands (or values) Scan the given expression and do the following for every scanned element If the element is a number, push it into the stack If the element is an operator, pop operands for the operator from the stack. Evaluate the operator and push the result back to the stack When the expression is ended, the number in the stack is the final answer Post-fix String Expression (Example) Expression => “2 3 1 * + 9 -“ Scan '2', it's a number, so push it to stack. Stack contains '2' Scan 3', again a number, push it to stack, stack now contains '2 3’ (from bottom to top) Scan 1', again a number, push it to stack, stack now contains '2 3 1' , it's an operator, pop two operands from stack, apply the * operator on Scan operands, we get 3*1 which results in 3. We push the result '3' to stack. The stack now becomes '2 3'
Post-fix String Expression (Example)<br>Scan +', it's an operator, pop two operands from stack, apply the + operator on<br>operands, we get 3 + 2 which results in 5. We push the result '5' to stack. The stack<br>now becomes '5'.<br>Scan '9', it's a number, we push it to the stack. The stack now becomes '5 9'.<br>Scan -', it's an operator, pop two operands from stack, apply the – operator on<br>operands, we get 5 – 9 which results in -4. We push the result -4' to the stack. The<br>stack now becomes '-4'<br>There are no more elements to scan, we return the top element from the stack<br>(which is the only element left in a stack)<br>Test Cases<br>Check for the following:<br>o Valid Input (numbers)<br>Valid Operators<br>• Empty Input<br>o No operator in input<br>Difference between 1, 2, and/or 3 digit number (think of how to use space character)<br>

Extracted text: Post-fix String Expression (Example) Scan +', it's an operator, pop two operands from stack, apply the + operator on operands, we get 3 + 2 which results in 5. We push the result '5' to stack. The stack now becomes '5'. Scan '9', it's a number, we push it to the stack. The stack now becomes '5 9'. Scan -', it's an operator, pop two operands from stack, apply the – operator on operands, we get 5 – 9 which results in -4. We push the result -4' to the stack. The stack now becomes '-4' There are no more elements to scan, we return the top element from the stack (which is the only element left in a stack) Test Cases Check for the following: o Valid Input (numbers) Valid Operators • Empty Input o No operator in input Difference between 1, 2, and/or 3 digit number (think of how to use space character)

Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here