Skip to content

Instantly share code, notes, and snippets.

@AppleCEO
Forked from hwj0623/main.swift
Created April 2, 2019 05:59
Show Gist options
  • Save AppleCEO/5fb4a078d2403bafdc5891d11e609979 to your computer and use it in GitHub Desktop.
Save AppleCEO/5fb4a078d2403bafdc5891d11e609979 to your computer and use it in GitHub Desktop.
04/02 pair programming of UnitConverter
//
// 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