Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a class that can calculate mathematical expressions. The input passed to this calculator is Infix notation, internally convert it to a suffix, and then

Implement a class that can calculate mathematical expressions. The input passed to this calculator is Infix notation, internally convert it to a suffix, and then calculate the suffix expression (Because it is simpler, we use suffix instead of infix for evaluation). More details about The suffix can be found in the video lecture. The calculator should support numeric values, five arithmetic operators (+, , *, /, ^) and parenthesis.

Follow the PEMDAS order of operations (you can define the precedence of operators And dictionary). Please note that exponentiation is ** in Python. You can assume that the expression will have tokens (operators, operands) separated by a single space. For negative numbers, you can assume that the negative sign will be the prefix of the number. The str.split() method helps to separate tags. If the expression is invalid

Meet any of the following conditions: Contains unsupported operator 4 $ 5 Contains consecutive operators 4 * + 5 Missing operand 4+ Missing operator 4 5 Unbalanced brackets) 4 + 5 (or (4 + 5)) Try to do implicit multiplication 3 (5) instead of 3 * 5 Include a space before the negative number 4 * -5 instead of 4 * -5 Ensure that the code is properly encapsulated by using the appropriate variable scope and writing code Other helper methods that can generalize your code for processing such as string processing and input verification. Don't forget to record your code. It is recommended that you start by implementing your method, assuming these expressions are always valid, In this way, you can implement logic and only need to validate the expression.

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): ''' >>> x=Calculator() >>> x._isNumber(' 2.560 ') True >>> x._isNumber('7 56') False >>> x._isNumber('2.56p') False ''' # YOUR CODE STARTS HERE pass

def _getPostfix(self, txt): ''' Required: _getPostfix must create and use a Stack for expression processing >>> x=Calculator() >>> x._getPostfix('2 ^ 4') '2.0 4.0 ^' >>> x._getPostfix('2') '2.0' >>> x._getPostfix('2.1 * 5 + 3 ^ 2 + 1 + 4.45') '2.1 5.0 * 3.0 2.0 ^ + 1.0 + 4.45 +' >>> x._getPostfix('2 * 5.34 + 3 ^ 2 + 1 + 4') '2.0 5.34 * 3.0 2.0 ^ + 1.0 + 4.0 +' >>> x._getPostfix('2.1 * 5 + 3 ^ 2 + 1 + 4') '2.1 5.0 * 3.0 2.0 ^ + 1.0 + 4.0 +' >>> x._getPostfix('( 2.5 )') '2.5' >>> x._getPostfix ('( ( 2 ) )') '2.0' >>> x._getPostfix ('2 * ( ( 5 + -3 ) ^ 2 + ( 1 + 4 ) )') '2.0 5.0 -3.0 + 2.0 ^ 1.0 4.0 + + *' >>> x._getPostfix ('( 2 * ( ( 5 + 3 ) ^ 2 + ( 1 + 4 ) ) )') '2.0 5.0 3.0 + 2.0 ^ 1.0 4.0 + + *' >>> x._getPostfix ('( ( 2 * ( ( 5 + 3 ) ^ 2 + ( 1 + 4 ) ) ) )') '2.0 5.0 3.0 + 2.0 ^ 1.0 4.0 + + *' >>> x._getPostfix('2 * ( -5 + 3 ) ^ 2 + ( 1 + 4 )') '2.0 -5.0 3.0 + 2.0 ^ * 1.0 4.0 + +'

# In invalid expressions, you might print an error message, but code must return None, adjust doctest accordingly # If you are veryfing the expression in calculate before passing to postfix, this cases are not necessary

>>> x._getPostfix('2 * 5 + 3 ^ + -2 + 1 + 4') >>> x._getPostfix('2 * 5 + 3 ^ - 2 + 1 + 4') >>> x._getPostfix('2 5') >>> x._getPostfix('25 +') >>> x._getPostfix(' 2 * ( 5 + 3 ) ^ 2 + ( 1 + 4 ') >>> x._getPostfix(' 2 * ( 5 + 3 ) ^ 2 + ) 1 + 4 (') >>> x._getPostfix('2 * 5% + 3 ^ + -2 + 1 + 4') '''

# YOUR CODE STARTS HERE postfixStack = Stack() # method must use postfixStack to compute the postfix expression pass

@property def calculate(self): ''' Required: calculate must call postfix calculate must create and use a Stack to compute the final result as shown in the video lecture >>> x=Calculator() >>> x.setExpr('4 + 3 - 2') >>> x.calculate 5.0 >>> x.setExpr('-2 + 3.5') >>> x.calculate 1.5 >>> x.setExpr('4 + 3.65 - 2 / 2') >>> x.calculate 6.65 >>> x.setExpr('23 / 12 - 223 + 5.25 * 4 * 3423') >>> x.calculate 71661.91666666667 >>> x.setExpr(' 2 - 3 * 4') >>> x.calculate -10.0 >>> x.setExpr('7 ^ 2 ^ 3') >>> x.calculate 5764801.0 >>> x.setExpr(' 3 * ( ( ( 10 - 2 * 3 ) ) )') >>> x.calculate 12.0 >>> x.setExpr('8 / 4 * ( 3 - 2.45 * ( 4 - 2 ^ 3 ) ) + 3') >>> x.calculate 28.6 >>> x.setExpr('2 * ( 4 + 2 * ( 5 - 3 ^ 2 ) + 1 ) + 4') >>> x.calculate -2.0 >>> x.setExpr(' 2.5 + 3 * ( 2 + ( 3.0 ) * ( 5 ^ 2 - 2 * 3 ^ ( 2 ) ) * ( 4 ) ) * ( 2 / 8 + 2 * ( 3 - 1 / 3 ) ) - 2 / 3 ^ 2') >>> x.calculate 1442.7777777777778

# In invalid expressions, you might print an error message, but code must return None, adjust doctest accordingly >>> x.setExpr(" 4 + + 3 + 2") >>> x.calculate >>> x.setExpr("4 3 + 2") >>> x.calculate >>> x.setExpr('( 2 ) * 10 - 3 * ( 2 - 3 * 2 ) )') >>> x.calculate >>> x.setExpr('( 2 ) * 10 - 3 * / ( 2 - 3 * 2 )') >>> x.calculate >>> x.setExpr(' ) 2 ( * 10 - 3 * ( 2 - 3 * 2 ) ') >>> x.calculate

# For extra credit only. If not attemped, these cases must return None >>> x.setExpr('( 3.5 ) ( 15 )') >>> x.calculate 52.5 >>> x.setExpr('3 ( 5 ) - 15 + 85 ( 12 )') >>> x.calculate 1020.0 >>> x.setExpr("( -2 / 6 ) + ( 5 ( ( 9.4 ) ) )") >>> x.calculate 46.666666666666664 '''

if not isinstance(self.__expr,str) or len(self.__expr)<=0: print("Argument error in calculate") return None

calcStack = Stack() # method must use calcStack to compute the expression

# YOUR CODE STARTS HERE pass

pls pls pls do it in your own way

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

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions