2022-06-29 13:26:43 +00:00
|
|
|
from math import log
|
2022-06-28 21:55:53 +00:00
|
|
|
|
|
|
|
class Calculator():
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Calculator class for the Intermediate Python
|
|
|
|
class led by Ed Fine from Pluralsight. This has
|
|
|
|
basic calculator functions including keeping a running
|
|
|
|
total.
|
|
|
|
"""
|
2022-06-28 22:03:50 +00:00
|
|
|
|
2022-06-28 21:55:53 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.total = 0.0
|
|
|
|
self.actions = []
|
|
|
|
|
|
|
|
def add(self, val1, val2=None):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Add two numbers together and prints the result
|
|
|
|
then adds the sum to the running total. With only
|
|
|
|
one number provided, just add that to the running
|
|
|
|
total.
|
|
|
|
"""
|
2022-06-28 21:55:53 +00:00
|
|
|
running_total = self.total
|
|
|
|
|
|
|
|
if val2:
|
|
|
|
total = val1 + val2
|
|
|
|
print(total)
|
|
|
|
self.actions.append(f"{val1} + {val2}")
|
|
|
|
else:
|
|
|
|
total = val1
|
|
|
|
self.actions.append(f"{val1} + {running_total}")
|
|
|
|
|
|
|
|
self.total += total
|
|
|
|
self.actions.append(f" = {self.total}")
|
|
|
|
|
|
|
|
def sub(self, val1, val2=None):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Subtracts val2 from val1 and prints the result
|
|
|
|
then subtracts the difference from the running total.
|
|
|
|
With only one number provided, just subtract that to
|
|
|
|
the running total.
|
|
|
|
"""
|
2022-06-28 21:55:53 +00:00
|
|
|
running_total = self.total
|
|
|
|
|
|
|
|
if val2:
|
|
|
|
total = val1 - val2
|
|
|
|
print(total)
|
|
|
|
self.actions.append(f"{val1} - {val2}")
|
|
|
|
else:
|
|
|
|
total = val1
|
|
|
|
self.actions.append(f"{val1} - {running_total}")
|
|
|
|
|
|
|
|
self.total -= total
|
|
|
|
self.actions.append(f" = {self.total}")
|
|
|
|
|
|
|
|
def mult(self, val1, val2=None):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Multiplies the new values and prints the result
|
|
|
|
then multiplies the product to the running total.
|
|
|
|
With only one number provided, just mulitply the
|
|
|
|
running total with the lone value.
|
|
|
|
"""
|
|
|
|
running_total = self.total
|
2022-06-28 21:55:53 +00:00
|
|
|
|
|
|
|
if val2:
|
|
|
|
total = val1 * val2
|
|
|
|
print(total)
|
|
|
|
self.actions.append(f"{val1} * {val2}")
|
|
|
|
else:
|
|
|
|
total = val1
|
|
|
|
self.actions.append(f"{val1} * {running_total}")
|
|
|
|
|
|
|
|
self.total *= total
|
|
|
|
self.actions.append(f" = {self.total}")
|
|
|
|
|
|
|
|
def div(self, val1, val2=None):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Divides val1 by val2 and prints the result
|
|
|
|
then divides the running total by the quotient.
|
|
|
|
With only one number provided, the running total
|
|
|
|
will be divided by the value.
|
|
|
|
"""
|
2022-06-28 21:55:53 +00:00
|
|
|
running_total = self.total
|
|
|
|
|
|
|
|
if val2:
|
|
|
|
total = val1 / val2
|
|
|
|
print(total)
|
|
|
|
self.actions.append(f"{val1} / {val2}")
|
|
|
|
else:
|
|
|
|
total = val1
|
|
|
|
self.actions.append(f"{val1} / {running_total}")
|
|
|
|
|
|
|
|
self.total /= total
|
|
|
|
self.actions.append(f" = {self.total}")
|
|
|
|
|
|
|
|
def pow(self, val1, val2=None):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Raises val1 to the power of val2 and prints
|
|
|
|
the result then raises the running total by the
|
|
|
|
result. With only one number provided, just
|
|
|
|
raise the running total by the lone value.
|
|
|
|
"""
|
2022-06-28 21:55:53 +00:00
|
|
|
running_total = self.total
|
|
|
|
|
|
|
|
if val2:
|
|
|
|
total = val1 ** val2
|
|
|
|
print(total)
|
|
|
|
self.actions.append(f"{val1} ** {val2}")
|
|
|
|
else:
|
|
|
|
total = val1
|
|
|
|
self.actions.append(f"{val1} ** {running_total}")
|
|
|
|
|
|
|
|
self.total = total ** self.total
|
|
|
|
self.actions.append(f" = {self.total}")
|
|
|
|
|
|
|
|
def log(self, val):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Prints the logarithm of the value. This
|
|
|
|
does not affect the running total.
|
|
|
|
"""
|
2022-06-28 21:55:53 +00:00
|
|
|
print(log(val))
|
|
|
|
self.actions.append(f"log({val})")
|
|
|
|
|
|
|
|
# self.total = log(val) ** self.total
|
|
|
|
# self.actions.append(f" = {self.total}")
|
|
|
|
|
|
|
|
def showcalc(self):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Prints out the running history since the
|
|
|
|
last all clear command.
|
|
|
|
"""
|
2022-06-28 21:55:53 +00:00
|
|
|
for action in self.actions:
|
|
|
|
print(action)
|
|
|
|
|
|
|
|
def total(self):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Prints the running total."""
|
2022-06-28 21:55:53 +00:00
|
|
|
return(self.total)
|
|
|
|
|
|
|
|
def ac(self):
|
2022-06-29 13:26:43 +00:00
|
|
|
"""Resets the running total to 0.0 and
|
|
|
|
empties the history."""
|
2022-06-28 21:55:53 +00:00
|
|
|
self.total = 0.0
|
|
|
|
self.actions = []
|
|
|
|
|
|
|
|
|