Last active
April 4, 2019 09:28
-
-
Save O-O-wl/271a5a5bd01167fb4bdecf8af058af06 to your computer and use it in GitHub Desktop.
STEP 2- 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum Unit : String { | |
case cm = "cm" | |
case m = "m" | |
case inch = "inch" | |
case yard = "yard" | |
case g = "g" | |
case kg = "kg" | |
case oz = "oz" | |
case lb = "lb" | |
func defaultUnit ()->Unit{ | |
switch self { | |
case .cm,.m,.inch,.yard: | |
return .m | |
case .g,.kg,.oz,.lb: | |
return .kg | |
} | |
} | |
} | |
typealias value = Double | |
typealias UnitRatio = Double | |
typealias beforeUnit = Unit | |
typealias afterUnit = Unit | |
func getRatio( units: (beforeUnit,afterUnit)) ->(Double){ | |
var unitRatioTable : [beforeUnit:[afterUnit:UnitRatio]] = | |
[ | |
.m: | |
[ .cm: 100, | |
.inch: 0.393701, | |
.yard: 0.0109361 | |
], | |
.cm: | |
[ .m: 0.01, | |
.inch: 39.3701, | |
.yard: 1.09361 | |
], | |
.inch: | |
[ | |
.m: 0.0254, | |
.cm: 2.54, | |
.yard: 0.0277778 | |
], | |
.yard: | |
[ | |
.cm: 91.44, | |
.m: 0.9144, | |
.inch: 36 | |
], | |
.g: | |
[ | |
.kg: 1000, | |
.oz: 0.035274, | |
.lb: 0.00220462 | |
], | |
.kg: | |
[ | |
.g: 1000, | |
.oz: 35.274, | |
.lb: 2.20462 | |
], | |
.oz: | |
[ | |
.g: 28.3495, | |
.kg: 0.0283495, | |
.lb: 0.0625 | |
], | |
.lb:[ | |
.g: 453.592, | |
.kg: 0.453592, | |
.oz: 16 | |
] | |
] | |
let (beforeUnit,afterUnit) = units | |
let ratio = beforeUnit == afterUnit ? 1 :(unitRatioTable[beforeUnit])![afterUnit] | |
return ratio! | |
} | |
func convert(userInput:String)->String{ | |
let (value,units) = recognize(userInput: userInput) | |
let ratio = getRatio(units:units) | |
return "\(calculatedValueString(value: value, ratio: ratio))\(units.1)" | |
} | |
func calculatedValueString(value:Double,ratio:Double)->String{ | |
let result = value * ratio | |
return confirmNumber(number: result) | |
} | |
func confirmNumber(number:Double)->String{ | |
if(number.remainder(dividingBy: 1)==0){ | |
return "\(Int.init(number))" | |
}else{ | |
return "\(number)" | |
} | |
} | |
func recognize(userInput : String)->(value,(beforeUnit,afterUnit)){ | |
let value = getValue(userInput: userInput) | |
let units = getUnits(userInput: userInput) | |
return (value,units) | |
} | |
func getValue (userInput:String)->Double{ | |
let pattern = "[0-9.]+" | |
let value = usingRegex(userInput: userInput, pattern: pattern) | |
return Double.init(value)! | |
} | |
func getUnits(userInput:String)->(beforeUnit,afterUnit){ | |
let pattern = "[^0-9.]+" | |
let units = usingRegex(userInput: userInput, pattern: pattern) | |
let unitArray = units.split(separator: " ") | |
let beforeUnit = Unit.init(rawValue: String(unitArray[0])) | |
let afterUnit:Unit | |
if unitArray.count == 2 { | |
afterUnit = Unit.init(rawValue: String(unitArray[1]))! | |
}else{ | |
afterUnit = beforeUnit!.defaultUnit() | |
} | |
return (beforeUnit!,afterUnit) | |
} | |
func usingRegex( userInput:String ,pattern:String ) -> String{ | |
let regexUnits = try! NSRegularExpression.init(pattern: pattern, options: []) | |
let matchs = regexUnits.firstMatch(in: userInput, options: [], range: NSRange.init(location: 0, length: userInput.count)) | |
let part = NSString.init(string: userInput).substring(with: (matchs?.range)!) | |
return part | |
} | |
func unitConverter(){ | |
let quitCommends = ["q","quit"] | |
while(true){ | |
print("변환할 값을 입력하세요") | |
let inputValue = readLine() | |
for quitCommend in quitCommends { | |
if(quitCommend == inputValue){ | |
break | |
} | |
} | |
print(convert(userInput: inputValue!)) | |
} | |
} | |
unitConverter() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
enum Unit
이라는 엄청난 열거형 타입이 만들어졌군요! 좋네요.다음 단계까지 진행해보세요. 다만 그러면서 코드가 바뀌는 부분이 어디어디인지 유심히 관찰해보세요.