-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
model.js
129 lines (122 loc) · 3.14 KB
/
model.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
function Calculator() {
this.operatorNeedsReset = true;
this.operandNeedsReset = true;
this.accumulatorNeedsReset = true;
this.decimal = -1;
this.ResetRegisters();
}
Calculator.prototype.DoOperation = function() {
switch (this.operator) {
case '+':
this.accumulator += this.operand;
break;
case '-':
this.accumulator -= this.operand;
break;
case '/':
this.accumulator /= this.operand;
break;
case '*':
this.accumulator *= this.operand;
break;
default:
this.accumulator = this.operand;
break;
}
return this.accumulator;
}
Calculator.prototype.SendAccumulatorByUDP = function() {
var calcResult = this.accumulator;
}
Calculator.prototype.ResetRegisters = function() {
if (this.operatorNeedsReset) {
this.operatorNeedsReset = false;
this.operator = null;
}
if (this.operandNeedsReset) {
this.operandNeedsReset = false;
this.operand = 0;
this.decimal = -1;
}
if (this.accumulatorNeedsReset) {
this.accumulatorNeedsReset = false;
this.accumulator = 0;
}
}
Calculator.prototype.HandleButtonClick = function(buttonValue) {
var result;
switch (buttonValue) {
case '+':
case '-':
case '/':
case '*':
this.decimal = -1;
if (this.operatorNeedsReset) {
this.operatorNeedsReset = false;
this.operator = null;
this.operand = this.accumulator;
}
this.operandNeedsReset = true;
result = this.DoOperation();
this.operator = buttonValue;
break;
case '=':
this.decimal = -1;
this.operandNeedsReset = true;
this.operatorNeedsReset = true;
this.DoOperation();
this.SendAccumulatorByUDP();
break;
case 'AC':
this.decimal = -1;
this.accumulatorNeedsReset = true;
this.operandNeedsReset = true;
this.operatorNeedsReset = true;
this.ResetRegisters();
break;
case '.':
if (this.decimal < 0)
this.decimal = 0;
this.operand = parseFloat(this.operand);
result = this.operand;
break;
case '+ / -':
this.operand *= -1;
break;
case 'back':
this.accumulatorNeedsReset = false;
this.ResetRegisters();
if (this.operand == 0) {
this.operator = 'back';
this.operatorNeedsReset = true;
}
else {
var operandStr = this.operand + '';
operandStr = operandStr.slice(0, operandStr.length - 1);
if (operandStr == '') this.operand = 0;
else this.operand = parseFloat(operandStr);
}
break;
default:
this.ResetRegisters();
if (this.decimal >= 0) {
this.decimal += 1;
this.operand += ( Math.pow(10, -1 * this.decimal)
* parseInt(buttonValue));
} else {
this.operand *= 10;
this.operand += parseInt(buttonValue);
}
result = this.operand;
break;
}
if (result == null) {
result = this.accumulator;
}
var rstr_len = (result + '').length;
if ((result >= 0 && rstr_len > 8) ||
(result < 0 && rstr_len > 9)) {
result = 'Overflow';
}
return [this.operator, this.operand, this.accumulator];
}