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
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'
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)