-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
148 lines (123 loc) · 4.29 KB
/
ViewController.swift
File metadata and controls
148 lines (123 loc) · 4.29 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
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
//
// ViewController.swift
// RetroCalc
// Copyright © 2017 Mikko Rouru. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController {
//Setup
@IBOutlet weak var outputLabel: UILabel!
var btnSound: AVAudioPlayer!
enum Operation: String {
case Divide = "/"
case Multiply = "*"
case Subtract = "-"
case Add = "+"
case Empty = "Empty"
}
var currentOperation = Operation.Empty
var runningNumber = ""
var leftValueString = ""
var rightValueString = ""
var result = ""
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "buttonsound", ofType: "wav")
let soundURL = URL(fileURLWithPath: path!)
do {
try btnSound = AVAudioPlayer(contentsOf: soundURL)
btnSound.prepareToPlay()
} catch let err as NSError {
print(err.debugDescription)
}
outputLabel.text = "0"
}
//Actions
@IBAction func numberPressed(sender: UIButton) {
playSound()
runningNumber += "\(sender.tag)"
outputLabel.text = runningNumber
}
@IBAction func onDividePressed(sender: AnyObject) {
processOperation(operation: .Divide)
}
@IBAction func onMultiplyPressed(sender: AnyObject) {
processOperation(operation: .Multiply)
}
@IBAction func onSubtractPressed(sender: AnyObject) {
processOperation(operation: .Subtract)
}
@IBAction func onAddPressed(sender: AnyObject) {
processOperation(operation: .Add)
}
@IBAction func onEqualPressed(sender: AnyObject) {
processOperation(operation: currentOperation)
}
@IBAction func onClearPressed(sender: AnyObject) {
runningNumber.removeAll()
leftValueString.removeAll()
rightValueString.removeAll()
result = ""
currentOperation = Operation.Empty
outputLabel.text = "0"
playSound()
}
func playSound() {
if btnSound.isPlaying {
btnSound.stop()
}
btnSound.play()
}
//Logic
func processOperation(operation: Operation) {
playSound()
if currentOperation != Operation.Empty {
// One round of calculations, second one
if runningNumber != "" {
rightValueString = runningNumber
runningNumber = ""
if currentOperation == Operation.Multiply {
result = "\(Double(leftValueString)! * Double(rightValueString)!)"
} else if currentOperation == Operation.Divide {
if (rightValueString == "0") {
dividedByZero()
} else {
result = "\(Double(leftValueString)! / Double(rightValueString)!)"
}
} else if currentOperation == Operation.Subtract {
result = "\(Double(leftValueString)! - Double(rightValueString)!)"
} else if currentOperation == Operation.Add {
result = "\(Double(leftValueString)! + Double(rightValueString)!)"
}
if (outputLabel.text != "Syntax error") {
leftValueString = result
if (result.hasSuffix(".0")) {
let range = result.index(result.endIndex, offsetBy: -2)..<result.endIndex
result.removeSubrange(range)
}
outputLabel.text = result
}
}
currentOperation = operation
} else {
// This is the first time when operator has been pressed
if (runningNumber != "") {
leftValueString = runningNumber
runningNumber = ""
currentOperation = operation
} else {
leftValueString = "0"
currentOperation = operation
}
}
}
//Error check if divided by zero
func dividedByZero() {
runningNumber.removeAll()
leftValueString = "0"
rightValueString.removeAll()
result = ""
outputLabel.text = "Syntax error"
}
}