Last active
April 4, 2019 02:50
-
-
Save O-O-wl/0d89f3838399ce0a46536f573482edb5 to your computer and use it in GitHub Desktop.
STEP2-5
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 | |
func recognize( input : String ) ->(Double,String,String){ | |
let pattern = "[^0-9.]+" | |
let regex = try! NSRegularExpression.init(pattern: pattern, options: []) | |
let matchRange = regex.firstMatch(in: input, options: [], range: NSRange.init(location: 0, length: input.count)) | |
let units = NSString.init(string: input).substring(with: matchRange!.range) | |
let beforeUnit = units.split(separator: " ")[0] | |
let afterUnit = units.split(separator: " ").count == 2 ? String(units.split(separator: " ")[1]):"m" | |
let value = input.filter{ | |
c in | |
if(units.contains(c)){ | |
return false | |
} | |
return true | |
} | |
return (Double.init(value)! ,String(beforeUnit),String(afterUnit)) | |
} | |
func convert(input:String) -> String { | |
let (value ,beforeUnit, afterUnit) = recognize(input: input) | |
var magnification :Double | |
switch ((beforeUnit,afterUnit) ) { | |
case ("cm","m"): | |
magnification = 0.01 | |
case ("cm","inch"): | |
magnification = 0.393701 | |
case ("cm","yard"): | |
magnification = 0.0109361 | |
case ("m","cm"): | |
magnification = 100 | |
case ("m","inch"): | |
magnification = 39.3701 | |
case ("m","yard"): | |
magnification = 1.09361 | |
case ("inch","cm"): | |
magnification = 2.54 | |
case ("inch","m"): | |
magnification = 0.0254 | |
case ("inch","yard"): | |
magnification = 0.0277778 | |
case ("yard","cm"): | |
magnification = 91.44 | |
case ("yard","m"): | |
magnification = 0.9144 | |
case ("yard","inch"): | |
magnification = 36 | |
default: | |
return "지원하지않는 단위입니다" | |
} | |
let result = "\(convertValueType(value: value * magnification))\(afterUnit)" | |
return result | |
} | |
func convertValueType( value : Double)->String{ | |
if(value.remainder(dividingBy: 1) == 0){ | |
return "\(Int.init(value))" | |
}else{ | |
return "\(value)" | |
} | |
} | |
func start(){ | |
while(true) | |
{ | |
print("변환할 값과 단위를 입력해주세요") | |
let inputValue = readLine() | |
let quit = ["q","quit"] | |
if(quit.contains(inputValue!)){ | |
break | |
} | |
print(convert(input: inputValue!)) | |
} | |
} | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
감사합니다 JK 개선해서 진행해보겠습니다.