Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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:

image text in transcribed

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

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

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

Students also viewed these Databases questions

Question

What is an interface? What keyword is used to define one?

Answered: 1 week ago