-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHBUtilities.swift
executable file
·76 lines (55 loc) · 2.04 KB
/
SHBUtilities.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// SHBUtilities.swift
// SeungHoBack_C0663173_Assignment
//
// Created by shoong on 2016-06-15.
// Copyright © 2016 SnowBack. All rights reserved.
//
import Foundation
public func input(range: Int) -> Int {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = NSString(data: keyboard.availableData, encoding: NSUTF8StringEncoding) as! String
if let idx = inputData.characters.indexOf("\n") {
inputData.removeAtIndex(idx)
}
if let ret = Int(inputData) {
if ret < range {
return ret
}
}
return 0
}
public func input() -> Int32 {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = NSString(data: keyboard.availableData, encoding: NSUTF8StringEncoding) as! String
if let idx = inputData.characters.indexOf("\n") {
inputData.removeAtIndex(idx)
}
return Int32(inputData)!
}
public func inputNewStock() -> String {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = NSString(data: keyboard.availableData, encoding: NSUTF8StringEncoding) as! String
if let idx = inputData.characters.indexOf("\n") {
inputData.removeAtIndex(idx)
}
return String(inputData)
}
public func inputNewStock() -> Float {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = NSString(data: keyboard.availableData, encoding: NSUTF8StringEncoding) as! String
if let idx = inputData.characters.indexOf("\n") {
inputData.removeAtIndex(idx)
}
return Float(inputData)!
}
public func formatStock(string: String, fieldWidth: Int) -> String {
var ret_string = string
let spaces: Int = fieldWidth - ret_string.characters.count
let padding = String(count: spaces, repeatedValue: Character(" "))
ret_string += padding
return ret_string
}
func *(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber {
return left.decimalNumberByMultiplyingBy(right)
}