-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
69 lines (55 loc) · 1.93 KB
/
ViewController.swift
File metadata and controls
69 lines (55 loc) · 1.93 KB
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
//
// ViewController.swift
// Calculator
//
// Created by Jacob Todd on 3/2/17.
// Copyright © 2017 Jacob Todd. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
private var userIsInTheMiddleOfTyping: Bool = false
private var brain = CalculatorBrain()
// create a computed property
var displayValue: Double {
get {
return Double(digitDisplay.text!)!
}
set {
digitDisplay.text = String(newValue)
}
}
//instance variable = property
@IBOutlet private weak var digitDisplay: UILabel! //The exclamation point automatically unwraps the variable (implicitly unwrapped option)
@IBAction func touchDigit(_ sender: UIButton) {
let digit = sender.currentTitle!
let dupDecimal = decimalDuplicateCheck(input: digit, stringToCheck: digitDisplay.text)
if userIsInTheMiddleOfTyping {
let textCurrentlyInDisplay = digitDisplay.text!
if !dupDecimal{
digitDisplay.text = textCurrentlyInDisplay + digit
}
} else {
digitDisplay.text = digit == "." ? "0" + digit: digit
userIsInTheMiddleOfTyping = true
}
}
func decimalDuplicateCheck(input digit: String, stringToCheck text: String?) -> Bool {
if userIsInTheMiddleOfTyping {
return digit == "." && text?.contains(".") == true
} else {
return false
}
}
@IBAction func performOperation(_ sender: UIButton) {
if userIsInTheMiddleOfTyping {
brain.setOperand(displayValue)
userIsInTheMiddleOfTyping = false
}
if let mathematicalSymbol = sender.currentTitle {
brain.performOperation((mathematicalSymbol))
}
if let result = brain.result {
displayValue = result
}
}
}