Answered step by step
Verified Expert Solution
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 : The Calculator class 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,
For the case of negative numbers, you can assume the negative sign will be prefixed to the
number. For example,
You Calculator must support implied multiplication. For example, returns
The strsplit method can be helpful to isolate tokens. Expressions are considered invalid if they
meet any of the following criteria:
Contains unsupported operators $
Contains consecutive operators
Has missing operands
Has missing operators
Has unbalanced parenthesis or or
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 setExprself newexpr Sets the expression for the calculator to evaluate
str getExprself Getter method for the private expression attribute
bool isNumberself aSring Returns True if aSring can be converted to a float
str getPostfixself expr Converts an expression from infix to postfix
float calculateself Calculates the expression stored in this calculator
Section : The Calculator class
setExprself newexpr
Sets the expression for the calculator to evaluate. This method is already implemented for you.
Input excluding self
str newexpr The new expression in infix for the calculator to evaluate
getExprself
Property method for getting the expression in the calculator. This method is already implemented
for you.
Output
str The value stored in expr
isNumberself txt points
Returns True if txt is a string that can be converted to a float, False otherwise. Note that the type
conversion float returns but float raises an exception. A tryexcept
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
getPostfixself expr 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 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 : The Calculator class
calculateself 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 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 : The AdvancedCalculator class 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 nonempty
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
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