Question: Handheld calculators allow users to perform multi-step calculations one step at a time, displaying intermediate results. For example, if you type 2+3+4+5= into this calculator,
Handheld calculators allow users to perform multi-step calculations one step at a time, displaying intermediate results. For example, if you type 2+3+4+5= into this calculator, you see 5 displayed as soon as you type 2+3+, and you see 9 displayed as soon as you type 2+3+4+ (and you see 14 displayed once you finish typing 2+3+4+5=).
Furthermore, many calculators will allow you to repeat the most recent operation by pressing = more than once. For example, if, after the 14 is displayed in this example, you press = once more, an additional +5 operation will be enacted, and the resulting display will read 19.
Implement a calculator that performs calculations one step at a time on a stored value as described above. Additionally, the calculator saves a log of recent operations performed. The calculator will work only oninttypes. Note: for this assignment, you may assume that we will not cause zero division errors by trying to divide anything by 0. You may also assume that the operands passed into yourstep()function will be nonnegative, although that should not make a difference in your implementation. Write a file that has:
Global variables representing information about the current calculation. You will need to store:
oanintrepresenting the current value stored in the calculator. The value of this variable should be initialized to0.
oastrrepresenting the most recent operation performed ("+","-","*", or"//").Initialize this variable to the empty string, since no operation has yet been performed.
oanintrepresenting the most recent argument (addend, subtrahend, factor, or divisor) used with the last operation.
oastrexpression representing the series of operations performed in the current calculation. See the instructions forget_expr()and example invocations for details on formatting. The variable should be initialized to"0", the string representation of the currentintvalue.
The following five functions:
oa function calledget_value()that simply returns theintrepresenting the current value stored in the calculator. This function should not modify any global variables.
oa function calledclear()that takes in an optionalintargument which defaults to0. The behavior ofclear()should be to reset the second and third globals mentioned above to their initial values, set the first and fourth globals mentioned above to the argument'sintvalue and itsstrrepresentation, respectively, and returns the currentintvalue.
oa function calledstep()that takes in astrarithmetic operator ("+","-","*", or"//") and anintargument (addend, subtrahend, factor, or divisor) to that operator. The behavior ofstep()should be to enact a single calculation step by updating the calculator's global variables to reflect the operation involved, and then return the calculator's currentintvalue.
oa function calledrepeat()that causes the calculator to repeat its last calculation step and returns the calculator's currentintvalue. If no previous operation has been recorded, the function should simply return the currentintvalue. Your implementation ofrepeat()should use your implementation ofstep().
oa function calledget_expr()that returns astrrepresenting the current expression. Note that, in this calculator, all operators areleft-associative. For clarity, your string should include parentheses around each left side of an operator. See theExample Invocationsbelow for more specific formatting guidelines.
If your program stores and regularly updates the current expression in the desired format as your fourth global variable from above, you may only have to return this stored variable in your implementatino ofget_expr().
Tip:you may find it helpful to write a helper function that updates the globalstrexpression wheneverstep()is called (so you would likely call this helper function from inside ofstep().
NOTE:You areexplicitly prohibitedfrom using the python built-ineval()orexec()functions in your code. You must calculate the results in your code.
In addition to functional correctness, having good variable names and having meaningful docstrings for all functions written are required.
Example Invocations:
When you run the file, nothing should happen. It defines a function, it does not run it. If in another file you write the following:
import pocket_calculator as calc
print("Example 1:") print(calc.get_expr())# Note that the initial expression will always simply be "0", and no parentheses are required until an operation step() is performedprint(calc.get_value())
print(" Example 2:")
step_1=calc.step("+",3)
print(calc.get_expr(), "=", step_2 = calc.step("-", 1) print(calc.get_expr(), "=", step_3 = calc.step("//", 2)
step_1)
step_2=calc.step("-",1) print(calc.get_expr(),"=", step_3=calc.step("//",2)
step_1) step_2) step_3) step_4)
print(calc.get_expr(), step_4=calc.repeat() print(calc.get_expr(),
print(calc.clear()) print(calc.get_expr())
print(" Example 3:")
"=","=",
print(calc.clear(10)) print(calc.step("*",2)) print(calc.step("+",1)) print(calc.repeat()) print(calc.repeat()) print(calc.get_expr())
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
