Showing posts with label 標準ストリーム. Show all posts
Showing posts with label 標準ストリーム. Show all posts

Tuesday, May 23, 2017

Swift: Reading from Standard Streams (標準ストリーム)

Command:

$ cat ./Desktop/StandardInput/StandardInput/main.swift


Result:

import Foundation

var string = ""

func showMainMenu(){
    print("1) Walk")
    print("2) Study")
    print("3) Work")
    print("4) Sleep")
    print("> ", terminator: "")
}

func walk(){
    var line = ""
   
    repeat {
        print("Walking...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func work(){
    var line = ""
   
    repeat {
        print("Working...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func study(){
    var line = ""
   
    repeat {
        print("Studying...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func sleep(){
    var line = ""
   
    repeat {
        print("Sleeping...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

showMainMenu()

while let line = readLine(){
    switch line{
    case "1":
        walk()
        break
    case "2":
        study()
        break
    case "3":
        work()
        break
    case "4":
        sleep()
        break
    default:
        print("Enter again")
        break
    }
    showMainMenu()
}


Command:

$ ./Library/Developer/Xcode/DerivedData/StandardInput-eauwzvcnsumxppbvwnagppvlylcg/Build/Products/Debug/StandardInput ; exit;


Result:

1) Walk
2) Study
3) Work
4) Sleep
> 1
Walking...
Enter 'exit' to finish
>
Walking...
Enter 'exit' to finish
>
Walking...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 2
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 3
Working...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 4
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> exit
Enter again
1) Walk
2) Study
3) Work
4) Sleep
> ^C
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]