Question #3392

2) Write a calculator history functionality in continuation of the above example and perform the multiplication, subtraction, division and square of the given numbers and store the result in calculator_history.txt file

Expert's answer

fout = open ("calculator_history.txt", "w")

def calc_mul (a, b):

fout.write(str(a) + "*" + str(b) + "=" + str(a*b) + "\n")

return a * b

def calc_add (a, b):

fout.write(str(a) + "+" + str(b) + "=" + str(a+b) + "\n")

return a + b

def calc_sub (a, b):

fout.write(str(a) + "-" + str(b) + "=" + str(a-b) + "\n")

return a - b

def calc_div (a, b):

fout.write(str(a) + "/" + str(b) + "=" + str(a/b) + "\n")

return a / b

def calc_square (a):

fout.write(str(a) + "^2" + "=" + str(a*a) + "\n")

return a * a

a, b, c, d = 10, 20, 30, 5

print( calc_square( calc_div( calc_mul( calc_sub(a,b), c ), d)))

def calc_mul (a, b):

fout.write(str(a) + "*" + str(b) + "=" + str(a*b) + "\n")

return a * b

def calc_add (a, b):

fout.write(str(a) + "+" + str(b) + "=" + str(a+b) + "\n")

return a + b

def calc_sub (a, b):

fout.write(str(a) + "-" + str(b) + "=" + str(a-b) + "\n")

return a - b

def calc_div (a, b):

fout.write(str(a) + "/" + str(b) + "=" + str(a/b) + "\n")

return a / b

def calc_square (a):

fout.write(str(a) + "^2" + "=" + str(a*a) + "\n")

return a * a

a, b, c, d = 10, 20, 30, 5

print( calc_square( calc_div( calc_mul( calc_sub(a,b), c ), d)))

## Comments

## Leave a comment