티스토리 뷰
반응형
소스를 작성해보겠습니다.
전체 소스
import UIKit
class ViewController: UIViewController {
var displayNumber = ""
var firstOperand = "" // 첫번째 피연산자
var secondOperand = "" // 새로운 값 두번쨰 피연산자
var result = "" // 결과값
var currentOperation: Operation = .Unkown // 연산자의 값을 저장하는 프로퍼티
@IBOutlet weak var numberLabel: UILabel!
@IBAction func numberBtn(_ sender: UIButton) {
guard let numberValue = sender.title(for: .normal) else { return }
if self.displayNumber.count < 9 {
self.displayNumber += numberValue
self.numberLabel.text = self.displayNumber
}
}
@IBAction func clearBtn(_ sender: UIButton) { // 초기화(AC) 버튼
self.displayNumber = ""
self.firstOperand = ""
self.secondOperand = ""
self.result = ""
self.currentOperation = .Unkown
self.numberLabel.text = "0"
}
@IBAction func dotBtn(_ sender: UIButton) {
if self.displayNumber.count < 8, !self.displayNumber.contains(".") {
self.displayNumber += self.displayNumber.isEmpty ? "0." : "."
self.numberLabel.text = self.displayNumber
}
}
@IBAction func divideBtn(_ sender: UIButton) {
self.operation(.Divide)
}
@IBAction func multiplyBtn(_ sender: UIButton) {
self.operation(.Multiply)
}
@IBAction func minusBtn(_ sender: UIButton) {
self.operation(.Minus)
}
@IBAction func plusBtn(_ sender: UIButton) {
self.operation(.Plus)
}
@IBAction func equalBtn(_ sender: UIButton) {
self.operation(self.currentOperation)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
enum Operation {
case Plus
case Minus
case Multiply
case Divide
case Unkown
}
func operation(_ operation: Operation) {
if self.currentOperation != .Unkown {
if !self.displayNumber.isEmpty {
self.secondOperand = self.displayNumber
self.displayNumber = ""
guard let firstOperand = Double(self.firstOperand) else { return }
guard let secondOperand = Double(self.secondOperand) else { return }
switch self.currentOperation {
case .Plus:
self.result = "\(firstOperand + secondOperand)"
case .Minus:
self.result = "\(firstOperand - secondOperand)"
case .Divide:
self.result = "\(firstOperand / secondOperand)"
case .Multiply:
self.result = "\(firstOperand * secondOperand)"
default:
break
}
if let result = Double(self.result), result.truncatingRemainder(dividingBy: 1) == 0 {
self.result = "\(Int(result))"
}
self.firstOperand = self.result
self.numberLabel.text = self.result
}
self.currentOperation = operation
} else {
self.firstOperand = self.displayNumber
self.currentOperation = operation
self.displayNumber = ""
}
}
}
반응형
'Swift' 카테고리의 다른 글
Swift UITabBarController 알아보기 (0) | 2022.02.01 |
---|---|
Swift MapKit 사용해보기 (0) | 2022.01.30 |
Swift 간단한 계산기 앱 만들기 1 (0) | 2022.01.24 |
Swift 화면 전환 : 네비게이션 컨트롤러 + 소스(push) (0) | 2022.01.22 |
Swift 화면전환 : present (0) | 2022.01.21 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Alamofire
- Firebase
- remote config
- Java
- RxSwift
- 라이트모드
- UIScrollView
- SwiftLint
- Realtime Database
- Android
- 웹뷰
- UIButton
- LazyHStack
- github
- WebView
- swipe
- SwiftUI
- Swift
- UITabBarController
- autolayout
- Observable
- gcd
- subscript
- string
- 다크모드
- ios
- 문자열
- NavigationLink
- Apple
- dictionary
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함