Question
DO NOT CHANGE FUNCTION NAMES, ARGUMENTS and the things given in the starter code. Calculator Class (Python): Implement a class that calculates mathematic expressions. The
DO NOT CHANGE FUNCTION NAMES, ARGUMENTS and the things given in the starter code.
Calculator Class (Python):
Implement a class that calculates mathematic expressions. The input passed to this Calculator is in infix notation, which gets converted to postfix internally, then the postfix expression is evaluated.
setExpr(self, new_expr) - Sets the expression for the calculator to evaluate. This method is already implemented for you.
getExpr(self) - Property method for getting the expression in the calculator. This method is already implemented for you.
isNumber(self, txt) - Returns True if txt is a string that can be converted to a float, False otherwise.
_getPostfix(self, expr) - Converts an expression from infix to postfix. All numbers in the output must be represented as a float.
calculate(self) - A property method that evaluates the infix expression saved in self.__expr. First convert the expression to postfix, then use a stack to evaluate the output postfix expression.
STARTER CODE:
Attributes Type Name str expr Description The expression this calculator will evaluate Methods Type Name Description None setExpr(self, new_expr) Sets the expression for the calculator to evaluate getExpr(self) Getter method for the private expression attribute bool isNumber(self, txt) Returns True if txt can be converted to a float _getPostfix(self, expr) Converts an expression from infix to postfix float calculate(self) Calculates the expression stored in this calculator str str class Calculator: def __init_(self): self._expr None @property def getExpr(self): return self._expr def setExpr(self, new_expr): if isinstance(new_expr, str): self._expr=new_expr else: print('setExpr error: Invalid expression') return None def isNumber(self, txt): pass def _getPostfix(self, txt): postOp = Stack() pass @property def calculate(self): if not isinstance(self._expr,str) or len(self._expr)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started