250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- ipad
- 풀스택
- button
- 비전공 개발자
- Animation
- CSS
- jQuery
- SWIFT
- MAC
- 자바스크립트
- image
- html5
- iPhone
- HTML
- hover
- front-end
- xcode
- 비전공자
- IOS
- 개발자
- css3
- 백엔드
- keyframes
- react
- iOS 개발자
- effect
- php
- javascript
- 프론트엔드
- 애니메이션
Archives
- Today
- Total
비전공자 개발일기
Basic Calculator 본문
728x90
SMALL
import UIKit
@IBDesignable
class RoundNumBTN: UIButton {
@IBInspectable var isRound: Bool = false {
didSet {
if isRound {
layer.cornerRadius = frame.height / 2
}
}
}
}
import UIKit
enum Operation {
case Plus
case Substract
case Multiple
case Divide
case unknown
}
class ViewController: UIViewController {
@IBOutlet weak var numOutputLabel: UILabel!
@IBOutlet weak var divideBTN: UIButton!
@IBOutlet weak var multipleBTN: UIButton!
@IBOutlet weak var substractBTN: UIButton!
@IBOutlet weak var plusBTN: UIButton!
var displayNum = ""
var firstOperator = ""
var secondOperator = ""
var result = ""
var currentOperator: Operation = .unknown
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func numBTN(_ sender: UIButton) {
guard let numVal = sender.title(for: .normal) else { return
}
if displayNum.count < 9 {
displayNum += numVal
numOutputLabel.text = displayNum
}
}
@IBAction func clearBTN(_ sender: UIButton) {
displayNum = ""
firstOperator = ""
secondOperator = ""
result = ""
currentOperator = .unknown
numOutputLabel.text = "0"
}
@IBAction func dotBTN(_ sender: UIButton) {
if displayNum.count < 8, !displayNum.contains(".") {
displayNum += displayNum.isEmpty ? "0." : "."
numOutputLabel.text = displayNum
}
}
@IBAction func divideBTN(_ sender: UIButton) {
self.operate(.Divide)
}
@IBAction func multipleBTN(_ sender: UIButton) {
self.operate(.Multiple)
}
@IBAction func substractBTN(_ sender: UIButton) {
self.operate(.Substract)
}
@IBAction func plusBTN(_ sender: UIButton) {
self.operate(.Plus)
}
@IBAction func resultBTN(_ sender: UIButton) {
self.operate(self.currentOperator)
}
func operate(_ operation: Operation) {
if currentOperator != .unknown {
if !displayNum.isEmpty {
secondOperator = displayNum
displayNum = ""
guard let firstNum = Double(firstOperator) else { return }
guard let secondNum = Double(secondOperator) else { return }
switch currentOperator {
case .Plus:
result = "\(firstNum + secondNum)"
case .Substract:
result = "\(firstNum - secondNum)"
case .Multiple:
result = "\(firstNum * secondNum)"
case .Divide:
result = "\(firstNum / secondNum)"
default:
break
}
if let res = Double(result), res.truncatingRemainder(dividingBy: 1) == 0 {
result = "\(Int(res))"
}
firstOperator = result
numOutputLabel.text = result
}
currentOperator = operation
} else {
firstOperator = displayNum
currentOperator = operation
displayNum = ""
}
}
}
728x90
LIST
'SWIFT' 카테고리의 다른 글
Pomodoro Timer (0) | 2022.12.15 |
---|---|
To Do List (0) | 2022.12.14 |
Display Background color & Text Setting (0) | 2022.12.13 |
Quote Generator (0) | 2022.12.12 |
Swift Study - Function, Class, Struct, Closure (0) | 2022.12.10 |