-
-
Save AppleCEO/5fb4a078d2403bafdc5891d11e609979 to your computer and use it in GitHub Desktop.
04/02 pair programming of UnitConverter
This file contains hidden or 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
// | |
// main.swift | |
// UnitConverter | |
// | |
// Created by Doran & Dominic on 02/04/2019. | |
// Copyright © 2019 hw. All rights reserved. | |
// | |
import Foundation | |
let str = readLine() | |
func convertCmToM (str: String) -> Void { | |
let doubleValue : Double = NSString(string: str as NSString).doubleValue | |
let result = Double(doubleValue/100) | |
print ("\(result)m") | |
} | |
func convertMToCm (str: String ) -> Void { | |
let doubleValue : Double = NSString(string: str as NSString).doubleValue | |
let result = Double(doubleValue*100) | |
print ("\(result)cm") | |
} | |
func unitConverter (input: String) -> Void { | |
let strArr = input.split(separator: " ") | |
if strArr.count == 0{ | |
return | |
}else{ | |
for str in strArr { | |
// Grab the last two or one characters | |
if str.suffix(2) == "cm" { | |
convertCmToM(str: String(str)) | |
} | |
else { | |
convertMToCm(str: String(str)) | |
} | |
} | |
} | |
} | |
if let test = str as? String { | |
unitConverter(input: test) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment