-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalkulator_Go.go
281 lines (229 loc) · 5.67 KB
/
Kalkulator_Go.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
Console "fmt"
"os"
System "os/exec"
"strconv"
"time"
)
func main() {
// Persiapan rand
// source := rand.NewSource(time.Now().UnixNano())
// random := rand.New(source)
// textLength := random.Perm(len(textIntro)) // Panjang random text
// Clear CMD First
clearScreen()
// Showing Text Intro
textIntro := "Welcome in Calculator (Go-Lang)"
showWelcome(textIntro)
// Show Menu
showMenu()
// Looping system if user want to do it again
doAgain := true
for doAgain {
// Program Start
// Input Operator Area
var input string // input string
var operatorInput int // to int
Console.Print("Input Operator = ")
for {
// Check Input must be Int
operatorInput = checkInputInt(input, operatorInput)
var ans string
Console.Print("Are you sure? (y/n)...")
Console.Scan(&ans)
if ans == "y" || ans == "Y" {
// // clear line
// clearLine()
// // clear line
// clearLine()
break
} else {
clearScreen()
showMenu()
Console.Print("Input Operator: ")
}
}
// Displays the selected operator
Console.Println("\n You Choose ", operatorName(operatorInput), "\n")
time.Sleep(2000 * time.Millisecond)
//// Enter Number
// First Number
var num1 int
num1 = showInputNum("Enter your first number")
// Second Number
var num2 int
num2 = showInputNum("Enter your second number")
Console.Println("\nNum 1 =", num1)
Console.Println("Num 2 =", num2)
// Process Number
processAritmethic(operatorInput, num1, num2)
// Condition to do it again/not
var doAgainString string
Console.Print("Do you want to do the operation again?(y/n)")
Console.Scan(&doAgainString)
if doAgainString != "y" {
doAgain = false
break
} else {
// Clear CMD First
clearScreen()
// Show Menu
showMenu()
}
}
// END LINE
// reader := bufio.NewReader(os.Stdin)
// Console.Print("Press Enter to continue...")
// reader.ReadString('k')
Console.Print("\nBye")
time.Sleep(2000 * time.Millisecond)
}
//----//// METHOD AREA ////----//
func clearScreen() {
cmd := System.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
}
func showWelcome(textIntro string) {
// Show Text Animation
for i := 0; i < len(textIntro); i++ {
Console.Print(string(textIntro[i]))
time.Sleep(100 * time.Millisecond)
}
time.Sleep(2000 * time.Millisecond)
// Delete Text Animation
for j := 0; j < len(textIntro); j++ {
Console.Print("\b \b")
time.Sleep(50 * time.Millisecond)
}
time.Sleep(1000 * time.Millisecond)
}
func showMenu() {
Console.Println(".____________________.")
Console.Println("|___TABEL OPERATOR___|")
Console.Println("| 1. Sum |")
Console.Println("| 2. Substraction |")
Console.Println("| 3. Division |")
Console.Println("| 4. Multiplication |")
Console.Println("| 5. Modulus |")
Console.Println("|____________________|")
}
func checkInputInt(inputString string, inputInteger int) int {
// Check Input must be Int
for {
// Scan Input and Parse to Int
_, scannedInput := Console.Scan(&inputString)
inputInteger, scannedInput = strconv.Atoi(inputString)
// Check result is int or string
if scannedInput != nil || inputInteger < 1 || inputInteger > 5 {
clearScreen()
showMenu()
Console.Print("[! Input must be a Number 1 - 5] Input Operator = ")
} else {
break
}
}
return inputInteger
}
func clearLine() {
// Source: https://groups.google.com/g/golang-nuts/c/k6l_LhI8CO0?pli=1
// ClearLine is the CSI sequence to clear the entire of the current line.
const ClearLine = "\033[2K"
// clear line
Console.Printf(ClearLine)
// clear line
Console.Printf(ClearLine)
// the line is cleared but the cursor is in the wrong place. the carriage
// return moves the cursor to the beginning of the line.
Console.Printf("\r")
}
func operatorName(operatorInput int) string {
var operatorInputString string
switch operatorInput {
case 1:
operatorInputString = "Sum"
break
case 2:
operatorInputString = "Substraction"
break
case 3:
operatorInputString = "Division"
break
case 4:
operatorInputString = "Multiplication"
break
case 5:
operatorInputString = "Modulus"
break
}
return operatorInputString
}
func showInputNum(textInput string) int {
var numReturn int
Console.Print(textInput, ": ")
for {
var num1String string
Console.Scan(&num1String)
// Is Input = 0?
if num1String == "0" {
numReturn = 0
break
}
// Whether the input includes letters or numbers
num, errCatch := strconv.Atoi(num1String)
if errCatch != nil {
// // Clear Line
// clearLine()
Console.Print("[! Should be a Number] ", textInput, ": ")
} else {
numReturn = num
break
}
}
return numReturn
}
func processAritmethic(operatorInput int, num1 int, num2 int) {
var operatorInputString string
var numResult int
switch operatorInput {
case 1:
operatorInputString = " + "
numResult = num1 + num2
break
case 2:
operatorInputString = " - "
numResult = num1 - num2
break
case 3:
operatorInputString = " : "
numResult = num1 / num2
break
case 4:
operatorInputString = " x "
numResult = num1 * num2
break
case 5:
operatorInputString = " % "
numResult = num1 % num2
break
}
// Display Aritmethic
Console.Print("\nProcessing:\n", num1, operatorInputString, num2, "\nResult: ")
for i := 1; i <= 2; i++ {
Console.Print(".")
time.Sleep(500 * time.Millisecond)
Console.Print("\b \b")
Console.Print("..")
time.Sleep(500 * time.Millisecond)
Console.Print("\b \b")
Console.Print("\b \b")
Console.Print("...")
time.Sleep(500 * time.Millisecond)
Console.Print("\b \b")
Console.Print("\b \b")
Console.Print("\b \b")
}
Console.Println(numResult)
time.Sleep(1000 * time.Millisecond)
}