-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindowViewModel.py
98 lines (86 loc) · 3.89 KB
/
MainWindowViewModel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class MainWindowViewModel:
def __init__(self) -> None:
self.leftSideValue = ""
self.rightSideValue = ""
self.waitingOperator = ""
self.displayValues = ["", ""]
def AddNumberValue(self, num):
if "=" in self.displayValues[0]:
self.ClearData()
if not self.waitingOperator:
self.leftSideValue += num
self.displayValues = ["", self.leftSideValue]
else:
self.rightSideValue += num
self.displayValues = [self.leftSideValue +
self.waitingOperator, self.rightSideValue]
def RemoveTypingLastCharactor(self):
if not self.waitingOperator:
self.leftSideValue = self.leftSideValue[:-1]
self.displayValues = ["", self.leftSideValue]
else:
self.rightSideValue = self.rightSideValue[:-1]
self.displayValues = [self.leftSideValue +
self.waitingOperator, self.rightSideValue]
def NegativeAndPositiveSwitch(self):
if not self.waitingOperator:
# Neu gia tri dang nhap khac empty hoac 0 thi tien hanh them bot dau tru
if self.leftSideValue and self.leftSideValue != "0":
self.leftSideValue = "-" + \
self.leftSideValue if not self.leftSideValue.startswith(
"-") else self.leftSideValue[1:]
self.displayValues = ["", self.leftSideValue]
else:
if self.rightSideValue and self.rightSideValue != "0":
self.rightSideValue = "-" + \
self.rightSideValue if not self.rightSideValue.startswith(
"-") else self.rightSideValue[1:]
self.displayValues = [self.leftSideValue +
self.waitingOperator, self.rightSideValue]
def AddOperator(self, operator):
if self.waitingOperator:
result = eval(self.leftSideValue +
self.waitingOperator + self.rightSideValue)
self.leftSideValue = str(result)
self.rightSideValue = ""
self.waitingOperator = operator
self.displayValues = [self.leftSideValue + self.waitingOperator, ""]
def TypePercentage(self):
if not self.waitingOperator:
result = eval(self.leftSideValue + "/100")
self.leftSideValue = str(result)
self.displayValues = ["", self.leftSideValue]
else:
result = eval(self.rightSideValue + "/100")
self.rightSideValue = str(result)
self.displayValues = [self.leftSideValue +
self.waitingOperator, self.rightSideValue]
def MakeFloat(self):
if "=" in self.displayValues[0]:
self.ClearData()
if not self.waitingOperator:
if self.leftSideValue:
self.leftSideValue += "." if "." not in self.leftSideValue else ""
else:
self.leftSideValue = "0."
self.displayValues = ["", self.leftSideValue]
else:
if self.rightSideValue:
self.rightSideValue += "." if "." not in self.rightSideValue else ""
else:
self.rightSideValue = "0."
self.displayValues = [self.leftSideValue +
self.waitingOperator, self.rightSideValue]
def Calculate(self):
result = eval(self.leftSideValue +
self.waitingOperator + self.rightSideValue)
self.displayValues = [
self.leftSideValue + self.waitingOperator + self.rightSideValue + "=", str(result)]
self.leftSideValue = str(result)
self.rightSideValue = ""
self.waitingOperator = ""
def ClearData(self):
self.leftSideValue = ""
self.rightSideValue = ""
self.waitingOperator = ""
self.displayValues = ["", ""]