W8 APP8
Please see attachment(s) for Assignment.
Mylib.py
#class for doing simple arithmetic,
#and calculating expressions.
class Calculator():
#constructor
#input are the numbers to be operated on
def __init__(self, n1, n2):
self.n1 = n1
self.n2 = n2
#return a dictionary of all arithmetic operations
def allInOne(self):
return {“add”:self.plus(),
“sub”:self.subtract(),
“mult”:self.multiply(),
“div”: self.divide()}
#check a value is within the given range lr..hr
#helper procedure
@staticmethod
def IsInRange(lr, hr, n):
return lr < n and n < hr
#add 2 numbers
def plus(self):
add = self.n1 + self.n2
return add
#subtract 2 numbers
def subtract(self):
sub = self.n1 - self.n2
return sub
#multiply 2 numbers
def multiply(self):
mult = self.n1 * self.n2
return mult
#division, n2 should not be zero
def divide(self):
try:
div = self.n1/(self.n2 + 0.0)
return div
except ZeroDivisionError as e:
print("The result of %s/%s = %s" % \
(self.n1, self.n2,"You cannot divide by Zero"))
except Exception :
print("Invalid input" , self.n1, self.n2)
#expression calculator
#will create the calculator object
@staticmethod
def scalc(val):
try:
#the epression is assumed to be comma separated
ns = val.split(",")
#split it into 3 parts
#remove whitespaces in the components
n1 = int(ns[0].strip())
n2 = int(ns[1].strip())
op = ns[2].strip()
calc = Calculator(n1, n2)
#create a calculator for doing arithmetic
#better option than mutating self.
#check if operator is one of the supported
if op == "*":
return calc.multiply()
elif op == "/":
return calc.divide()
elif op == "+":
return calc.plus()
elif op == "-":
return calc.subtract()
else:
print("Unknown operator", op)
#in case of invalid input catch the error
except Exception :
print("Invalid input:" , val)
W7_first_last.py
# Program name :
# Student Name :
# Course : ENTD220
# Instructor :
# Date : 02/20/2020
# Description : Expression & Arithmetic Calculator using classes
# Copy Wrong : This is my work
from Mylib import Calculator
def doScalc():
print("---------Expression calculator------------------")
print("Enter the expression with format N1,N2,operator:")
print("Supported operator * , /, -, +")
print("E.g 5, 4, *)")
val = input()
res = Calculator.scalc(val)
print('The result of "' + val + '" is', res)
def showList():
print("1) Add two numbers")
print("2) Multiply two numbers")
print("3) Subtract two numbers")
print("4) Divide two numbers")
print("5) Scalc")
print("6) All in one")
print("7) Exit")
def getUserInput():
low = int(input("Enter Lower range:"))
high = int(input(“Enter Higher range:”))
n1 = int(input(“Enter first number:”))
n2 = int(input(“Enter second number:”))
return [low, high, n1 , n2]
def doCalculation(op):
user = getUserInput()
low = user[0]
high = user[1]
n1 = user[2]
n2 = user[3]
if Calculator.IsInRange(low , high, n1) and \
Calculator.IsInRange(low , high, n2):
calc = Calculator(n1, n2)
if op == “+”:
res = calc.plus()
elif op == “*”:
res = calc.multiply()
elif op == “-“:
res = calc.subtract()
elif op == “/”:
res = calc.divide()
print(“%d %s %d = %d” % (n1, op, n2, res))
else:
print(“Error: input out of range”)
def doAllInOne():
user = getUserInput()
low = user[0]
high = user[1]
n1 = user[2]
n2 = user[3]
if Calculator.IsInRange(low , high, n1) and \
Calculator.IsInRange(low , high, n2):
calc = Calculator(n1, n2)
mydict = calc.allInOne()
print(“%d + %d = %d” % (n1, n2, mydict[“add”]))
print(“%d – %d = %d” % (n1, n2, mydict[“sub”]))
print(“%d * %d = %d” % (n1, n2, mydict[“mult”]))
print(“%d / %d = %d” % (n1, n2, mydict[“div”]))
def main():
while True:
showList()
action = int(input(“What do you want to do?”))
if action == 7:
break;
if action == 1:
user = getUserInput
doCalculation(“+”)
elif action == 2:
doCalculation(“*”)
elif action == 3:
doCalculation(“-“)
elif action == 4:
doCalculation(“/”)
elif action == 5:
doScalc()
elif action == 6:
doAllInOne()
print()
main()
print(“Thanks for using our calculator!”)