Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Calculator-VI by tokiojapan55
class Calculator:
def input(self, key):
if key == "C":
self.clear()
elif key == "B":
if self.__state == "ope":
if self.__operator == "":
self.__display = "0" if self.len_display == 1 else self.__display[:-1]
self.__state = "num"
elif len(self.__operator) >= 2:
self.__operator = self.__operator[:-1]
else:
self.__operator = ""
self.__state = "num"
elif not self.is_error:
self.__display = "0" if self.len_display == 1 else self.__display[:-1]
self.__state = "num"
else:
self.__fn[self.__state][Calculator.get_type(key)](self, key)
self.__state = Calculator.get_type(key)
self.__prekey = key
@property
def display(self):
return self.__display
@property
def is_error(self):
return self.__display == "error"
@property
def len_display(self):
if self.is_error:
return 0
return len(self.__display) - (1 if self.__display[0] == "-" else 0)
def clear(self):
self.__value = None
self.__operator = ""
self.__display = "0"
self.__prekey = ""
self.__lastope = ""
self.__state = "num"
def __init__(self):
self.clear()
def __fn_num_num(self, key):
if self.is_error or self.len_display >= 5:
return
if key == "." and self.__display.find(".") >= 0:
return
if self.__display == "0":
self.__display = key
else:
self.__display += key
def __fn_num_ope(self, key):
self.__calc(key)
self.__lastope = ""
def __fn_num_equ(self, key):
self.__lastope = "" if self.__operator == "" or self.is_error else (self.__operator + self.__display)
self.__calc(key)
def __fn_ope_num(self, key):
self.__display = key
def __fn_ope_ope(self, key):
if self.__operator == "/" and key == "/":
self.__operator = "//"
elif self.__operator == "*" and key == "*":
self.__operator = "**"
else:
self.__operator = key
def __fn_ope_equ(self, key):
self.__value = "error" if self.is_error else eval(self.__display)
self.__lastope = "" if self.__operator == "" or self.is_error else (self.__operator + self.__display)
self.__calc(key)
def __fn_equ_num(self, key):
self.clear()
self.__display = key
def __fn_equ_ope(self, key):
self.__value = "error" if self.is_error else eval(self.__display)
self.__operator = key
def __fn_equ_equ(self, key):
self.__calc(key, True)
def __calc(self, key, recalc=False):
if self.is_error:
self.__value = "error"
if key != "=":
self.__operator = key
return
if self.__display.find(".") >= 0:
while self.__display[-1] == "0":
self.__display = self.__display[:-1]
if recalc:
if self.__lastope == "":
return
expr = self.__display + self.__lastope
else:
if self.__value == None or self.__operator == "":
self.__value = "error" if self.is_error else eval(self.__display)
if key != "=":
self.__operator = key
return
if self.__value == "error":
self.__display = "error"
if key != "=":
self.__operator = key
return
expr = str(self.__value) + self.__operator + self.__display
try:
self.__display = str(eval(expr))
if not recalc and self.__operator == "/" and self.__display.find(".") < 0:
self.__display += ".0"
except:
self.__display = "error"
self.__value = "error"
if key != "=":
self.__operator = key
return
point = self.__display.find(".")
if point < 0:
if self.len_display > 5:
self.__display = "error"
self.__value = "error"
if key != "=":
self.__operator = key
return
else:
sign = "-" if self.__display[0] == "-" else ""
major = self.__display[len(sign):point]
minor = self.__display[point + 1:]
while minor and len(major) + 1 + len(minor) > 5:
if int(minor[-1]) < 5:
minor = minor[:-1]
elif len(minor) == 1:
minor = ""
major = str(int(major) + 1)
else:
n = len(minor)
minor = str(int(minor[:-1]) + 1)
if len(minor) >= n:
minor = minor[1:]
major = str(int(major) + 1)
if len(major) + 1 > 5:
self.__display = "error"
self.__value = "error"
if key != "=":
self.__operator = key
return
while minor and minor[-1] == "0":
minor = minor[:-1]
self.__display = sign + major + "." + minor
self.__value = eval(self.__display)
if key != "=":
self.__operator = key
__fn = {
"num": {"num": __fn_num_num, "ope": __fn_num_ope, "equ": __fn_num_equ},
"ope": {"num": __fn_ope_num, "ope": __fn_ope_ope, "equ": __fn_ope_equ},
"equ": {"num": __fn_equ_num, "ope": __fn_equ_ope, "equ": __fn_equ_equ},
}
@staticmethod
def is_num(key):
return key.isdigit() or key == "."
@staticmethod
def is_ope(key):
return not (Calculator.is_num(key) or Calculator.is_equ(key))
@staticmethod
def is_equ(key):
return key == "="
@staticmethod
def get_type(key):
return "num" if Calculator.is_num(key) else "ope" if Calculator.is_ope(key) else "equ"
def calculator(log: str) -> str:
cal = Calculator()
for key in list(log):
cal.input(key)
return cal.display
Aug. 18, 2023
Comments: