프로그래머스 (Swift)/기초
[Day18-3] 간단한 식 계산하기
은더기
2024. 4. 30. 09:00
문제
내 정답 코드
import Foundation
func solution(_ binomial:String) -> Int {
let component = binomial.components(separatedBy: " ")
let (a, op, b) = (Int(component[0]), component[1], Int(component[2]))
switch op {
case "+":
return a!+b!
case "-":
return a!-b!
case "*":
return a!*b!
default:
return 0
}
}
#
1. 공백으로 문자열을 분리해주면 a, op, b를 얻을 수 있다.
2. switch문에서 op를 받아 각 연산자에 맞는 계산을 반환한다.
배운 기술