Newer
Older
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>RPN calculator</title>
<style type="text/css">
#container {
display: flex;
flex-flow: row;
width: 700px;
margin: 10px auto;
border: 1px grey dotted;
}
#container > div {
border: 1px grey dotted;
}
#container > div > p {
margin-left: 10px;
}
#stack {
width: 200px;
height: 500px
}
#keyboard {
width: 300px;
height: 500px
}
#trail {
width: 200px;
height: 500px
}
#stack-content {
margin-left: 10px;
}
#trail-content {
margin-left: 10px;
}
.keyboard {
font-size: 1.2em;
text-align: center;
}
.keyboard table {
margin: 2px auto;
}
.keyboard td {
width: 60px;
height: 60px;
}
#number-display {
border: 1px grey solid;
width: 100%;
text-align: right;
font-size: 1.5em;
}
#error-msg {
height: 30px;
}
</style>
</head>
<body>
<div id="container">
<div id="stack">
<p>Stack</p>
<div id="stack-content"></div>
</div>
<div id="keyboard">
<p>Keyboard</p>
<div class="keyboard">
<table>
<tr>
<td colspan="4" id="error-msg"></td>
</tr>
<tr>
<td colspan="4" id="number-display"></td>
<td data-bt-name="num7">7</td>
<td data-bt-name="num8">8</td>
<td data-bt-name="num9">9</td>
<td title="backspace" data-bt-name="backspace"><</td>
<td data-bt-name="num4">4</td>
<td data-bt-name="num5">5</td>
<td data-bt-name="num6">6</td>
<td title="times" data-bt-name="times">*</td>
<td data-bt-name="num1">1</td>
<td data-bt-name="num2">2</td>
<td data-bt-name="num3">3</td>
<td title="minus" data-bt-name="minus">-</td>
<td title="change sign" data-bt-name="change-sign">±</td>
<td title="dot" data-bt-name="dot">.</td>
<td title="plus" data-bt-name="plus">+</td>
<td title="swap" data-bt-name="swap">SWAP</td>
<td title="undo" data-bt-name="undo">UNDO</td>
<td title="divide" data-bt-name="divide">÷</td>
<td title="confirm or duplicate number" data-bt-name="return">RETURN</td>
</tr>
</table>
</div>
</div>
<div id="trail">
<p>Trail</p>
<div id="trail-content"></div>
</div>
</div>
<script type="text/javascript" src="vendor/jquery.min.js"></script>
<script type="text/javascript" src="fsm.js"></script>
(function () {
/**
* show dumb data for UI testing.
*/
const showDumbData = function () {
$("#number-display").text('789');
$("#stack-content").append($('<pre>').text('3: 1'));
$("#stack-content").append($('<pre>').text('2: 2'));
$("#stack-content").append($('<pre>').text('1: 3'));
$("#stack-content").append($('<pre>').text(' .'));
$("#trail-content").append($('<pre>').text(' 1'));
$("#trail-content").append($('<pre>').text(' 2'));
$("#trail-content").append($('<pre>').text('> 3'));
};
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
const rpnTestBasic = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
m.sendKey("num2");
m.sendKey("return");
console.assert(m.numberStack.length === 2);
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 3);
};
const rpnTestNumberHandling = function () {
var m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("num2");
m.sendKey("return");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 12);
console.assert(m.currentNumber === "");
m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num0");
m.sendKey("dot");
m.sendKey("num1");
m.sendKey("num2");
m.sendKey("return");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 0.12);
console.assert(m.currentNumber === "");
m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num0");
m.sendKey("dot");
m.sendKey("num1");
m.sendKey("num2");
m.sendKey("backspace");
console.assert(m.currentNumber === "0.1");
m.sendKey("return");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 0.1);
console.assert(m.currentNumber === "");
};
const rpnTestAutoCommitNumber = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
m.sendKey("num2");
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 3);
console.assert(m.currentNumber === "");
console.assert(m.currentState === "idle");
};
const rpnTestOperator = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
m.sendKey("num2");
m.sendKey("return");
console.assert(m.numberStack.length === 2);
m.sendKey("minus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === -1);
m.sendKey("num3");
m.sendKey("num0");
m.sendKey("return");
console.assert(m.numberStack.length === 2);
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 29);
m.sendKey("return"); // duplicate
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 29 * 2);
m.sendKey("num9");
m.sendKey("change-sign");
m.sendKey("swap");
console.assert(m.numberStack.length === 2);
console.assert(m.numberStack[0] === -9);
console.assert(m.numberStack[1] === 29 * 2);
};
const rpnBackspaceOperator = function () {
const m = new fsm.RPNCalculator();
m.sendKey("num1");
m.sendKey("num2");
console.assert(m.currentNumber === "12");
m.sendKey("backspace"); // remove last digit in current number.
console.assert(m.currentNumber === "1");
m.sendKey("num3");
m.sendKey("return");
console.assert(m.currentNumber === "");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 13);
m.sendKey("backspace"); // delete last number in stack.
console.assert(m.numberStack.length === 0);
};
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const rpnTestNumberErrorHandling = function () {
var m = new fsm.RPNCalculator();
m.sendKey("num0");
m.sendKey("num0");
console.assert(m.currentNumber === "0");
m = new fsm.RPNCalculator();
m.sendKey("dot");
console.assert(m.currentNumber === "0.");
m.sendKey("backspace");
console.assert(m.currentNumber === "0");
m.sendKey("backspace");
console.assert(m.currentNumber === "");
m = new fsm.RPNCalculator();
m.sendKey("num1");
m.sendKey("dot");
m.sendKey("num2");
console.assert(m.currentNumber === "1.2");
m.sendKey("dot"); // future dot is ignored and show error.
console.assert(m.currentNumber === "1.2");
m.sendKey("num3");
console.assert(m.currentNumber === "1.23");
m.sendKey("dot");
console.assert(m.currentNumber === "1.23");
m.sendKey("return");
console.assert(m.numberStack[0] === 1.23);
};
const rpnTestNotEnoughElementOnStack = function () {
const m = new fsm.RPNCalculator();
console.assert(m.numberStack.length === 0);
m.sendKey("num1");
m.sendKey("return");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 1);
m.sendKey("plus");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === 1);
console.assert(m.lastError !== null);
m.sendKey("backspace");
console.assert(m.numberStack.length === 0);
console.assert(m.lastError === null);
m.sendKey("change-sign");
console.assert(m.lastError !== null);
m.sendKey("num1");
console.assert(m.lastError === null);
m.sendKey("change-sign");
console.assert(m.numberStack.length === 1);
console.assert(m.numberStack[0] === -1);
console.assert(m.lastError === null);
};
/**
* run all tests.
*/
const runTests = function () {
console.assert(fsm.matchesSubstringAbb("123abb123") === true);
console.assert(fsm.matchesSubstringAbb("123ab123") === false);
console.assert(fsm.matchesSubstringAbb("123abaabb123") === true);
console.assert(fsm.matchesSubstringAbb("123abbb123") === true);
console.assert(fsm.matchesSubstringAbb("123ababab123") === false);
console.assert(fsm.matchesSubstringAbb("") === false);
console.assert(fsm.matchesSubstringAbb("123") === false);
console.assert(fsm.matchesSubstringAbb("123ab") === false);
console.assert(fsm.matchesSubstringAbb("123abb") === true);
console.assert(fsm.matchesSubstringAbb("abb") === true);
rpnTestBasic();
rpnTestNumberHandling();
rpnTestAutoCommitNumber();
rpnTestOperator();
rpnTestNotEnoughElementOnStack();
rpnBackspaceOperator();
/**
* update web UI to match the state of th rpnCalculator.
*
* currently it syncs rpnCalculator.currentNumber and rpnCalculator.numberStack.
*/
const updateUI = function (rpnCalculator) {
$('#number-display').text(rpnCalculator.currentNumber);
$('#error-msg').text(rpnCalculator.lastError);
const stack = rpnCalculator.numberStack;
const stackSize = stack.length;
$("#stack-content").children().remove();
for (i = 0; i < stackSize; ++i) {
$("#stack-content").append($('<pre>').text((stackSize - i) + ': ' + stack[i]));
}
$("#stack-content").append($('<pre>').text(' .'));
};
const rpnCalculator = new fsm.RPNCalculator();
// init web UI
updateUI(rpnCalculator);
// setup event handler
$('.keyboard td').click(function (evt) {
const keyName = $(target).attr('data-bt-name');
if (keyName !== undefined) {
console.log("user clicked on button: " + keyName);
rpnCalculator.sendKey(keyName);
// update UI
updateUI(rpnCalculator);
}
</script>
</body>
</html>