From 10c73653cf1cc27a517277bf0fba91bc8230d910 Mon Sep 17 00:00:00 2001 From: Mark McIntyre Date: Tue, 28 Jun 2022 17:55:53 -0400 Subject: [PATCH] adding calculator class --- calculator.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 calculator.py 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 = [] + +