-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
229 lines (181 loc) · 5.96 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'use strict';
const colorPickerModel = {
waitingForPick: false,
setColor(key, color) {
this[key] = `<span style="color: ${color}">${key}</span>`;
},
prepColorString(expression) {
let colorString = '';
expression.split('').forEach((item) => {
if (this[item]) { colorString += this[item]; }
else { colorString += item; }
});
return colorString;
}
}
const inputOutputModel = {
expression: [],
integerReg: /^[+\-0-9]+$/,
operatorReg: /^[*/+-]+$/,
plusMinusReg: /[+-]/,
reset() { this.expression = []; },
expressionString() {
return this.expression.join('');
},
formatResult(n) {
//thanks to: https://stackoverflow.com/a/36028587/6848825
return Number(n).toPrecision(11).replace(/\.?0+$/,"");
},
expressionLengthOk(expressionString) {
return expressionString.length <= 13;
},
lastItem() {
return this.expression[this.expression.length - 1];
},
lastIsInteger() {
return isFinite(this.lastItem()) && this.integerReg.test(this.lastItem());
},
lastIsPosNegSign() {
let lastIsSecondOperatorInSequence = this.expression.length > 0 && this.isOperator(-2);
let lastIsNegPosSignAndIsFirstItem = this.expression.length == 1 && this.plusMinusReg.test(this.lastItem());
return lastIsSecondOperatorInSequence || lastIsNegPosSignAndIsFirstItem;
},
acceptNegPosSign(input) {
let inputIsFirstItem = this.expression.length === 0;
let inputIsSecondOperatorInSequence = this.expression.length > 1 && isFinite(this.expression.slice(-2,-1));
return inputIsFirstItem || inputIsSecondOperatorInSequence;
},
isOperator(from) {
return this.expression.slice(from).every( (item) => {
return this.operatorReg.test(item);
});
},
concatLastItem(input) {
this.expression[this.expression.length - 1] = this.lastItem() + input;
},
deleteCharacter() {
if (this.expression.length >= 1 && this.lastItem().length > 1) {
this.expression[this.expression.length - 1] = this.lastItem().slice(0,-1);
}
else { this.expression.pop(); }
},
run(key) {
if (octopus.isWaitingForPick()) { octopus.pickColor(key); }
else if (/[0-9]/.test(key)) { this.digit(key); }
else if (/[*/+-]/.test(key)) { this.operator(key); }
else if (/\./.test(key)) { this.period(key); }
else if (key === 'Backspace') { this.deleteCharacter(); }
else if (key === 'Enter' && this.expression.length > 0 && !this.operatorReg.test(this.expression.slice(-1))) { octopus.processInput(this.expression); }
else if (key === 'ClearAll') { octopus.clearAll(); }
},
digit(input) {
if (isFinite(this.lastItem())) {
this.concatLastItem(input);
}
else if (this.lastIsPosNegSign()) {
this.concatLastItem(input);
}
else { this.expression.push(input); }
},
operator(input) {
if (isFinite(this.lastItem())) {
this.expression.push(input);
}
else if (this.plusMinusReg.test(input) && this.acceptNegPosSign(input)) {
this.expression.push(input);
}
},
period(input) {
if (this.lastIsInteger()) {
this.concatLastItem(input);
}
if (this.isOperator(-1) || this.expression.length === 0) {
this.expression.push('0.');
}
}
}
const shuntModel = {
queue: [],
stack: [],
run(array) {
this.parseArray(array);
this.pushStackToQueue();
this.stack = [];
},
reset() {
this.queue = [];
this.stack = [];
},
parseArray(array) {
array.forEach(item => {
if (isFinite(item)) { this.queue.push(parseFloat(item)); } //where string becomes number
else { this.placeOperator(item); }
})
},
pushStackToQueue() {
while(this.stack.length > 0) {
this.queue.push(this.stack.pop());
}
},
placeOperator(op) {
while (this.topOfStackPrecedenceHigherOrEqual(op, this.topOfStack()) && this.stack.length > 0) {
this.queue.push(this.stack.pop());
}
this.stack.push(op);
},
topOfStackPrecedenceHigherOrEqual(currentOp, stackTopOp) {
let plusMinus = /[+-]/;
let multDiv = /[*\/]/;
return (multDiv.test(stackTopOp) || plusMinus.test(currentOp));
},
topOfStack() { return this.stack[this.stack.length - 1]; }
}
const postfixEvalModel = {
stack: [],
result: null,
reset() {
postfixEvalModel.stack = [];
postfixEvalModel.result = null;
},
error() {
if(isFinite(postfixEvalModel.result)) { return false; }
return true;
},
operators: {
'*': function([a, b]) { return a * b; },
'/': function([a, b]) { return a / b; },
'+': function([a, b]) { return a + b; },
'-': function([a, b]) { return a - b; }
},
run(postfix) {
for (let item of postfix) { //for... of syntax allows return to break the loop which forEach does not
if (isFinite(item)) { this.stack.push(item); }
else {
if (this.insufficientOperands()) { return; };
this.eval(item);
}
};
this.checkAndAssignResult();
},
eval(op) {
let operands = this.stack.splice(-2);
this.stack.push(this.operators[op](operands));
},
insufficientOperands() {
if (this.stack.length < 2) {
this.result = 'Error: insufficient operands';
this.stack = [];
return true;
}
return false;
},
checkAndAssignResult() {
if (this.stack.length > 1) {
this.result = 'Error: too many operands';
}
else {
this.result = this.stack[0];
}
this.stack = [];
}
}