98 lines
2.5 KiB
Python
98 lines
2.5 KiB
Python
|
|
class Calculator():
|
|
from math import log
|
|
|
|
def __init__(self):
|
|
self.total = 0.0
|
|
self.actions = []
|
|
|
|
def add(self, val1, val2=None):
|
|
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):
|
|
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):
|
|
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 div(self, val1, val2=None):
|
|
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):
|
|
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):
|
|
print(log(val))
|
|
self.actions.append(f"log({val})")
|
|
|
|
# self.total = log(val) ** self.total
|
|
# self.actions.append(f" = {self.total}")
|
|
|
|
def showcalc(self):
|
|
for action in self.actions:
|
|
print(action)
|
|
|
|
def total(self):
|
|
return(self.total)
|
|
|
|
def ac(self):
|
|
self.total = 0.0
|
|
self.actions = []
|
|
|
|
|