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.

DO NOT CHANGE FUNCTION NAMES, ARGUMENTS and the things given in the

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:

starter code. Calculator Class (Python): Implement a class that calculates mathematic expressions.

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!