-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (105 loc) · 1.68 KB
/
main.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Item interface {
Label()
Check(key string) bool
Run()
}
type Title struct {
note string
}
func (t Title) Label() {
fmt.Printf("\033[35m%s\033[0m\n", t.note)
}
func (t Title) Check(key string) bool {
return false
}
func (t Title) Run() {}
type SortAlgo struct {
note string
key string
runner func(arr []int)
}
func (a SortAlgo) Label() {
fmt.Printf("%s -- %s\n", a.key, a.note)
}
func (a SortAlgo) Check(key string) bool {
return a.key == key
}
func (a SortAlgo) Run() {
fmt.Printf("\033[32m%s\033[0m\n", a.note)
arr := []int{2, 6, 3, 1, 5, 4, 12, 0, 1}
fmt.Println("Before: ", arr)
a.runner(arr[:])
fmt.Println("After: ", arr)
}
type Op struct {
note string
key string
runner func()
}
func (a Op) Label() {
fmt.Printf("%s -- %s\n", a.key, a.note)
}
func (a Op) Check(key string) bool {
return a.key == key
}
func (a Op) Run() {
a.runner()
}
var ops = []Item {
Title {
note: "Sorts",
},
SortAlgo {
note: "Bubble sort",
key: "1",
runner: Bubble,
},
SortAlgo {
note: "Merge sort",
key: "2",
runner: Merge,
},
SortAlgo {
note: "Quick sort",
key: "3",
runner: Quick,
},
Title {
note: "Other",
},
Op {
note: "Quit",
key: "q",
runner: func() {
fmt.Println("Good bye")
os.Exit(0)
},
},
}
func main() {
reader := bufio.NewReader(os.Stdin)
for {
for i:=0; i<len(ops); i++ {
ops[i].Label()
}
fmt.Print("->");
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
found := false
for i:=0; i<len(ops); i++ {
if ops[i].Check(text) {
ops[i].Run()
}
}
if found == false {
fmt.Printf("%s not found\n", text)
}
}
}