diff --git a/calculator.py b/calculator.py new file mode 100644 index 0000000..54ef697 --- /dev/null +++ b/calculator.py @@ -0,0 +1,97 @@ + +class Calculator(): + 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): + from math import log + + 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 = [] + +