Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Section 2 : The Calculator class ( 5 0 points ) Implement a class that calculates arithmetic expressions. The input passed to this Calculator is

Section 2: The Calculator class (50 points)
Implement a class that calculates arithmetic expressions. The input passed to this Calculator is in
infix notation, which gets converted to postfix internally, then the postfix expression is evaluated
(we evaluate in postfix instead of infix because of how much simpler it is). More details about
infix to postfix conversion can be found in the video lectures.
This calculator should support numeric values, five arithmetic operators (+,,*,/,^), and the
enclosing marks (),[] and {}. Follow the PEMDAS order of operations (you can define
precedence of operators with a dictionary or a helper method). Note that exponentiation is ** in
Python.
You can assume that expressions will have tokens (operators, operands) separated by a
single space. For example, '4+5.65'
For the case of negative numbers, you can assume the negative sign will be prefixed to the
number. For example, '4+-5.65','4--5.65'
You Calculator must support implied multiplication. For example, '4(5)' returns 20.0
The str.split() method can be helpful to isolate tokens. Expressions are considered invalid if they
meet any of the following criteria:
Contains unsupported operators 4 $ 5
Contains consecutive operators 4*+5
Has missing operands 4+
Has missing operators 45
Has unbalanced parenthesis )4+5( or (4+5)) or (4+5]
Make sure to have proper encapsulation of your code by using proper variable scopes and writing
other helper methods to generalize your code for processes such as string processing and input
validation. Do not forget to document your code.
As a suggestion, start by implementing your methods assuming the expressions are always valid,
that way you have the logic implemented and you only need to work on validating the expressions.
Attributes
Type Name Description
str __expr The expression this calculator will evaluate
Methods
Type Name Description
None setExpr(self, new_expr) Sets the expression for the calculator to evaluate
str getExpr(self) Getter method for the private expression attribute
bool _isNumber(self, aSring) Returns True if aSring can be converted to a float
str _getPostfix(self, expr) Converts an expression from infix to postfix
float calculate(self) Calculates the expression stored in this calculator
Section 2: The Calculator class
setExpr(self, new_expr)
Sets the expression for the calculator to evaluate. This method is already implemented for you.
Input (excluding self)
str new_expr The new expression (in infix) for the calculator to evaluate
getExpr(self)
Property method for getting the expression in the calculator. This method is already implemented
for you.
Output
str The value stored in __expr
_isNumber(self, txt)(5 points)
Returns True if txt is a string that can be converted to a float, False otherwise. Note that the type
conversion float('4.56') returns 4.56 but float('456') raises an exception. A try/except
block could be useful here.
Input (excluding self)
str txt The string to check if it represents a number
Output
bool True if txt can be successfully casted to a float, False otherwise
_getPostfix(self, expr)(35 points)
Converts an expression from infix to postfix. All numbers in the output must be represented as a
float. You must use the Stack defined in section 1 in this method, otherwise your code will not
receive credit. (Stack applications video lecture can be helpful here).
Input (excluding self)
str expr The expression in infix form
Output
str The expression in postfix form
None None is returned if the input is an invalid expression
Section 2: The Calculator class
calculate(self)(10 points)
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. You must use the
Stack defined in section 1 and _getPostfix in this method, otherwise your code will not receive
credit.
Input (excluding self)
str txt The string to check if it represents a number
Output
float The result of the expression
None None is returned if the input is an invalid expression or if expression cant be
computed
Section 3: The AdvancedCalculator class (30 points)
The AdvancedCalculator class represents a calculator that supports multiple expressions over
many lines, as well as the use of variables. Lines will be split by semicolons (;), and every token
will be separated by a single space. Each line will start with a variable name, then an = character,
then a mathematical expression. The last line will ask to return a mathematical expression too.
You can assume that:
An expression will not reference variables that have not been defined yet.
Variable names will be consistent and case sensitive. A valid variable name is a non-empty
string of alphanumeric characters, the first of which must be a letter.
You must use a Calculator to evaluate each expression in this class, otherwise, no credit will be
given.
Attributes
Type Name

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

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago