質問編集履歴
1
コードの追記,やりたいことについて
test
CHANGED
File without changes
|
test
CHANGED
@@ -244,6 +244,288 @@
|
|
244
244
|
|
245
245
|
### やりたいこと
|
246
246
|
|
247
|
-
現状の設計では式が表示されない設計になっているのですがそれを式が表示できるようにしたい。
|
248
|
-
|
249
|
-
|
247
|
+
テキストフィールドで途中式を表示し、labelで合計を表示させたい。
|
248
|
+
|
249
|
+
=を使わずに合計を表示させたい。
|
250
|
+
|
251
|
+
(入力された数字を都度に計算しlabelに表示させる)
|
252
|
+
|
253
|
+
### //最新コード
|
254
|
+
|
255
|
+
```swift
|
256
|
+
|
257
|
+
//
|
258
|
+
|
259
|
+
// ViewController.swift
|
260
|
+
|
261
|
+
// calcuator
|
262
|
+
|
263
|
+
//
|
264
|
+
|
265
|
+
// Created by ... on 2018/09/10.
|
266
|
+
|
267
|
+
// Copyright © 2018年 ... All rights reserved.
|
268
|
+
|
269
|
+
//
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
import UIKit
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
class ViewController: UIViewController {
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
@IBOutlet weak var CalcProc: UITextField! //process
|
282
|
+
|
283
|
+
@IBOutlet weak var CalcRslt: UILabel! //result
|
284
|
+
|
285
|
+
var numberOnScrren:Double = 0;
|
286
|
+
|
287
|
+
var previousNumber:Double = 0;
|
288
|
+
|
289
|
+
var performingMath = false
|
290
|
+
|
291
|
+
var operation = 0;
|
292
|
+
|
293
|
+
var isFirst = true // 最初の処理かどうか
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
//tag 0~10 11は00入れたらあるが現状はない
|
298
|
+
|
299
|
+
@IBAction func tapNumber(_ sender: Any) {
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
if isFirst {
|
304
|
+
|
305
|
+
isFirst = false
|
306
|
+
|
307
|
+
CalcRslt.text! = ""
|
308
|
+
|
309
|
+
// 一度だけ実行したい処理
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
if performingMath == true
|
316
|
+
|
317
|
+
{
|
318
|
+
|
319
|
+
CalcRslt.text = String((sender as AnyObject).tag + 0)
|
320
|
+
|
321
|
+
numberOnScrren = Double(CalcRslt.text!)!
|
322
|
+
|
323
|
+
//
|
324
|
+
|
325
|
+
CalcProc.text = String((sender as AnyObject).tag + 0)
|
326
|
+
|
327
|
+
numberOnScrren = Double(CalcProc.text!)!
|
328
|
+
|
329
|
+
performingMath = false
|
330
|
+
|
331
|
+
}else
|
332
|
+
|
333
|
+
{
|
334
|
+
|
335
|
+
CalcRslt.text = CalcRslt.text! + String((sender as AnyObject).tag + 0)
|
336
|
+
|
337
|
+
numberOnScrren = Double(CalcRslt.text!)!
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
CalcProc.text = CalcRslt.text! + String((sender as AnyObject).tag + 0)
|
342
|
+
|
343
|
+
numberOnScrren = Double(CalcProc.text!)!
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
//tag 12~15
|
354
|
+
|
355
|
+
@IBAction func calcBtr(_ sender: Any) {
|
356
|
+
|
357
|
+
if CalcRslt.text != "" && (sender as AnyObject).tag != 11 && (sender as AnyObject).tag != 17{
|
358
|
+
|
359
|
+
previousNumber = Double(CalcRslt.text!)!
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
if (sender as AnyObject).tag == 12 //Plus
|
364
|
+
|
365
|
+
{
|
366
|
+
|
367
|
+
CalcProc.text = String(numberOnScrren) + "+";
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
if (sender as AnyObject).tag == 13 //Minus
|
372
|
+
|
373
|
+
{
|
374
|
+
|
375
|
+
CalcProc.text = String(numberOnScrren) + "-";
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
if (sender as AnyObject).tag == 14 // Multiply
|
380
|
+
|
381
|
+
{
|
382
|
+
|
383
|
+
CalcProc.text = String(numberOnScrren) + "×";
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
if (sender as AnyObject).tag == 15 //Divide
|
388
|
+
|
389
|
+
{
|
390
|
+
|
391
|
+
CalcProc.text = String(numberOnScrren) + "÷";
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
operation = (sender as AnyObject).tag
|
398
|
+
|
399
|
+
performingMath = true
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
else if (sender as AnyObject).tag != 0 || (sender as AnyObject).tag != 9 //ここで計算する処理
|
404
|
+
|
405
|
+
{
|
406
|
+
|
407
|
+
if operation == 12
|
408
|
+
|
409
|
+
{
|
410
|
+
|
411
|
+
CalcRslt.text = String(previousNumber + numberOnScrren)
|
412
|
+
|
413
|
+
CalcProc.text = String(previousNumber + numberOnScrren)
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
else if operation == 13
|
418
|
+
|
419
|
+
{
|
420
|
+
|
421
|
+
CalcRslt.text = String(previousNumber - numberOnScrren)
|
422
|
+
|
423
|
+
CalcProc.text = String(previousNumber - numberOnScrren)
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
else if operation == 14
|
428
|
+
|
429
|
+
{
|
430
|
+
|
431
|
+
CalcRslt.text = String(previousNumber * numberOnScrren)
|
432
|
+
|
433
|
+
CalcProc.text = String(previousNumber * numberOnScrren)
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
else if operation == 15
|
438
|
+
|
439
|
+
{
|
440
|
+
|
441
|
+
CalcRslt.text = String(previousNumber / numberOnScrren)
|
442
|
+
|
443
|
+
CalcProc.text = String(previousNumber / numberOnScrren)
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
}
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
//tag 16,17
|
454
|
+
|
455
|
+
@IBAction func deleteBtr(_ sender: Any) {
|
456
|
+
|
457
|
+
if (sender as AnyObject).tag == 17
|
458
|
+
|
459
|
+
{
|
460
|
+
|
461
|
+
CalcRslt.text = "0"
|
462
|
+
|
463
|
+
CalcProc.text = "0"
|
464
|
+
|
465
|
+
previousNumber = 0;
|
466
|
+
|
467
|
+
numberOnScrren = 0;
|
468
|
+
|
469
|
+
operation = 0;
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
if (sender as AnyObject).tag == 17
|
476
|
+
|
477
|
+
{
|
478
|
+
|
479
|
+
/*一文字ずつ消す
|
480
|
+
|
481
|
+
ex)1234の時123になる*/
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
//18~21
|
490
|
+
|
491
|
+
@IBAction func toolBtr(_ sender: Any) {
|
492
|
+
|
493
|
+
}
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
//22,23
|
498
|
+
|
499
|
+
@IBAction func otherBtr(_ sender: Any) {
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
override func viewDidLoad() {
|
508
|
+
|
509
|
+
super.viewDidLoad()
|
510
|
+
|
511
|
+
// Do any additional setup after loading the view, typically from a nib.
|
512
|
+
|
513
|
+
}
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
override func didReceiveMemoryWarning() {
|
518
|
+
|
519
|
+
super.didReceiveMemoryWarning()
|
520
|
+
|
521
|
+
// Dispose of any resources that can be recreated.
|
522
|
+
|
523
|
+
}
|
524
|
+
|
525
|
+
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
}
|
530
|
+
|
531
|
+
```
|