-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
156 lines (110 loc) · 4.02 KB
/
ViewController.swift
File metadata and controls
156 lines (110 loc) · 4.02 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
148
149
150
151
152
153
154
//
// ViewController.swift
// Assignment1
//
// Created by Name: Sarthak Vashistha on 2022-09-24
//Student ID: 301245284
//Name: Neha patel
//Student ID:301280513
//This is the UI design of a calculator which has all the buttons in iphone 13 pro for IOS 16.0 in Xcode 14.0
import UIKit
enum Operation:String {
case Add = "+"
case Subtract = "-"
case divide = "/"
case Multiply = "x"
case equal = "="
case Null = "Null"
}
class ViewController: UIViewController {
}
//result label
@IBOutlet weak var ResultLabel: UILabel!
var pressedNumber = ""
var LHS = ""
var RHS = ""
var result = ""
var currentOperation:Operation = .Null
var percent = "100"
override func viewDidLoad() {
super.viewDidLoad()
ResultLabel.text = "0"
}
//Event handlers
@IBAction func All_Clear(_ sender: UIButton) {
pressedNumber = ""
LHS = ""
RHS = ""
result = ""
currentOperation = .Null
ResultLabel.text = "0"
}
@IBAction func backButton_Pressed(_ sender: UIButton) {
pressedNumber = String(pressedNumber.dropLast(1))
ResultLabel.text = pressedNumber
if pressedNumber.isEmpty {
pressedNumber = "0"
}
@IBAction func Percent_Press(_ sender: UIButton) {
}
@IBAction func PlusMinus_Press(_ sender: UIButton) {
pressedNumber = String( ResultLabel.text!)
pressedNumber = "-" + pressedNumber
ResultLabel.text = pressedNumber
}
@IBAction func divide(_ sender: UIButton) {
operation(operation: .divide)
}
@IBAction func Multiply(_ sender: UIButton) {
operation(operation: .Multiply)
}
@IBAction func Add(_ sender: UIButton) {
operation(operation: .Add)
}
@IBAction func subtract(_ sender: UIButton) {
operation(operation: .Subtract)
}
@IBAction func Equals(_ sender: UIButton) {
operation(operation: currentOperation)
}
@IBAction func dot(_ sender: UIButton) {
if pressedNumber.count <= 7{
pressedNumber += "."
ResultLabel.text = pressedNumber
}
}
func operation(operation: Operation){
if currentOperation != .Null{
if pressedNumber != "" {
RHS = pressedNumber
pressedNumber = ""
if currentOperation == .Add {
result = "\(Double(LHS)! + Double(RHS)!)"
}else if currentOperation == .Subtract{
result = "\(Double(LHS)! - Double(RHS)!)"
}else if currentOperation == .divide{
result = "\(Double(LHS)! / Double(RHS)!)"
}else if currentOperation == .Multiply{
result = "\(Double(LHS)! * Double(RHS)!)"
}else if currentOperation == .equal{
}
LHS = result
if (Double(result)!.truncatingRemainder(dividingBy: 1) == 0){
result = "\(Int(Double(result)!))"
}
ResultLabel.text = result
}
currentOperation = operation
}else {
LHS = pressedNumber
pressedNumber = ""
currentOperation = operation
}
}
@IBAction func NumberButton_Pressed(_ sender: UIButton) {
if pressedNumber.count <= 8{
pressedNumber += "\(sender.tag)"
ResultLabel.text = pressedNumber
}
}
}