回答編集履歴
2
コード修正2
test
CHANGED
@@ -111,3 +111,199 @@
|
|
111
111
|
}
|
112
112
|
|
113
113
|
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
私の答えはこうです。よくある状態遷移です。
|
118
|
+
|
119
|
+
[AC]ボタンはご自身で実装してみてください。
|
120
|
+
|
121
|
+
```JavaScript
|
122
|
+
|
123
|
+
var kotae = document.getElementById('ans');
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
var zeroToNine = ["zero", "one", "two", "three", "four",
|
128
|
+
|
129
|
+
"five", "six", "seven", "eight", "nine"];
|
130
|
+
|
131
|
+
var digitButtons = new Array(10);
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
// 演算子ボタン
|
136
|
+
|
137
|
+
var operators = ["plus", "minus", "mul", "div"];
|
138
|
+
|
139
|
+
var operatorButtons = new Array(operators.length);
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
var calculatingButton = document.getElementById('equ');
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
// 状態定数
|
148
|
+
|
149
|
+
var STATE = {
|
150
|
+
|
151
|
+
INITIAL : 0,
|
152
|
+
|
153
|
+
DIGIT_BUTTON_PRESSED_AT_FIRST : 1,
|
154
|
+
|
155
|
+
OPERATOR_BUTTON_PRESSED : 2,
|
156
|
+
|
157
|
+
DIGIT_BUTTON_PRESSED_AT_SECOND : 3,
|
158
|
+
|
159
|
+
CALCULATING_BUTTON_PRESSED : 4
|
160
|
+
|
161
|
+
};
|
162
|
+
|
163
|
+
// 状態変数
|
164
|
+
|
165
|
+
var state = STATE.INITIAL;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
// 数値記憶用
|
170
|
+
|
171
|
+
var memory = 0;
|
172
|
+
|
173
|
+
// 押された演算子
|
174
|
+
|
175
|
+
var pressedOperator = '';
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
for (var i = 0; i <= 9; i++) {
|
180
|
+
|
181
|
+
digitButtons[i] = document.getElementById(zeroToNine[i]);
|
182
|
+
|
183
|
+
digitButtons[i].onclick = function() {
|
184
|
+
|
185
|
+
// アラビア数字に変換
|
186
|
+
|
187
|
+
var digit = zeroToNine.indexOf(this.id) + '';
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
switch (state) {
|
192
|
+
|
193
|
+
case STATE.INITIAL:
|
194
|
+
|
195
|
+
case STATE.DIGIT_BUTTON_PRESSED_AT_FIRST:
|
196
|
+
|
197
|
+
kotae.value += digit;
|
198
|
+
|
199
|
+
state = STATE.DIGIT_BUTTON_PRESSED_AT_FIRST;
|
200
|
+
|
201
|
+
break;
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
case STATE.OPERATOR_BUTTON_PRESSED:
|
206
|
+
|
207
|
+
kotae.value = '';
|
208
|
+
|
209
|
+
// noBreak;
|
210
|
+
|
211
|
+
case STATE.DIGIT_BUTTON_PRESSED_AT_SECOND:
|
212
|
+
|
213
|
+
kotae.value += digit;
|
214
|
+
|
215
|
+
state = STATE.DIGIT_BUTTON_PRESSED_AT_SECOND;
|
216
|
+
|
217
|
+
break;
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
};
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
for (i = 0; i < operatorButtons.length; i++) {
|
230
|
+
|
231
|
+
operatorButtons[i] = document.getElementById(operators[i]);
|
232
|
+
|
233
|
+
operatorButtons[i].onclick = function() {
|
234
|
+
|
235
|
+
switch (state) {
|
236
|
+
|
237
|
+
case STATE.DIGIT_BUTTON_PRESSED_AT_FIRST:
|
238
|
+
|
239
|
+
case STATE.CALCULATING_BUTTON_PRESSED:
|
240
|
+
|
241
|
+
pressedOperator = this.id;
|
242
|
+
|
243
|
+
memory = parseInt(kotae.value, 10);
|
244
|
+
|
245
|
+
state = STATE.OPERATOR_BUTTON_PRESSED;
|
246
|
+
|
247
|
+
break;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
};
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
calculatingButton.onclick = function() {
|
258
|
+
|
259
|
+
if (state === STATE.DIGIT_BUTTON_PRESSED_AT_SECOND) {
|
260
|
+
|
261
|
+
var operand = parseInt(kotae.value, 10);
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
switch (pressedOperator) {
|
266
|
+
|
267
|
+
case "plus":
|
268
|
+
|
269
|
+
kotae.value = memory + operand;
|
270
|
+
|
271
|
+
break;
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
case "minus":
|
276
|
+
|
277
|
+
kotae.value = memory - operand;
|
278
|
+
|
279
|
+
break;
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
case "mul":
|
284
|
+
|
285
|
+
kotae.value = memory * operand;
|
286
|
+
|
287
|
+
break;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
case "div":
|
292
|
+
|
293
|
+
kotae.value = memory / operand;
|
294
|
+
|
295
|
+
break;
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
state = STATE.CALCULATING_BUTTON_PRESSED;
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
};
|
308
|
+
|
309
|
+
```
|
1
コード修正
test
CHANGED
@@ -67,3 +67,47 @@
|
|
67
67
|
数字のキーはこのようにすると、コードを短縮でき、間違えにくくなります。
|
68
68
|
|
69
69
|
2桁以上にも対応しています。
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
これでどうですか。
|
74
|
+
|
75
|
+
```JavaScript
|
76
|
+
|
77
|
+
var kotae = document.getElementById('ans');
|
78
|
+
|
79
|
+
var zeroToNine = ["zero", "one", "two", "three", "four",
|
80
|
+
|
81
|
+
"five", "six", "seven", "eight", "nine"];
|
82
|
+
|
83
|
+
var digitElements = new Array(10);
|
84
|
+
|
85
|
+
var tasu = document.getElementById('plus');
|
86
|
+
|
87
|
+
var hiku = document.getElementById('minus');
|
88
|
+
|
89
|
+
var kakeru = document.getElementById('mul');
|
90
|
+
|
91
|
+
var waru = document.getElementById('div');
|
92
|
+
|
93
|
+
var ikoru = document.getElementById('equ');
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
for (var i = 0; i <= 9; i++) {
|
98
|
+
|
99
|
+
digitElements[i] = document.getElementById(zeroToNine[i]);
|
100
|
+
|
101
|
+
digitElements[i].onclick = function() {
|
102
|
+
|
103
|
+
// アラビア数字に変換
|
104
|
+
|
105
|
+
var digit = zeroToNine.indexOf(this.id) + '';
|
106
|
+
|
107
|
+
kotae.value += digit;
|
108
|
+
|
109
|
+
};
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
```
|