-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.go
101 lines (78 loc) · 3.27 KB
/
calc.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
package main
import (
"fmt"
"strings"
"strconv"
)
func main() {
fmt.Print("Math problem: ")
var res1 string
fmt.Scanln(&res1)
if strings.HasPrefix(res1, "1") || strings.HasPrefix(res1, "2") || strings.HasPrefix(res1, "3") || strings.HasPrefix(res1, "4") || strings.HasPrefix(res1, "5") || strings.HasPrefix(res1, "6") || strings.HasPrefix(res1, "7") || strings.HasPrefix(res1, "8") || strings.HasPrefix(res1, "9"){
if strings.Contains(res1, "+") {
// get index of where "+" is located
plusLocatedAt := strings.Index(res1, "+")
// replace "+" with "" so math can be done
mathToDo := (strings.ReplaceAll(res1, "+", ""))
// get what the first number to do math is
firstNumber := (mathToDo[0:plusLocatedAt])
firstNumberInt, _ := strconv.Atoi(firstNumber)
// get second number to do math with
mathLength := len(mathToDo)
secondNumberInt, _ := strconv.Atoi(mathToDo[plusLocatedAt:mathLength])
// print out the completed problem
mathSum := firstNumberInt + secondNumberInt
fmt.Print("gobot: The sum of the two numbers: ")
fmt.Print(mathSum)
} else if strings.Contains(res1, "*") {
// get index of where "*" is located
xLocatedAt := strings.Index(res1, "*")
// replace "+" with "" so math can be done
mathToDo := (strings.ReplaceAll(res1, "*", ""))
// get what the first number to do math is
firstNumber := (mathToDo[0:xLocatedAt])
firstNumberInt, _ := strconv.Atoi(firstNumber)
// get second number to do math with
mathLength := len(mathToDo)
secondNumberInt, _ := strconv.Atoi(mathToDo[xLocatedAt:mathLength])
// print out the completed problem
mathSum := firstNumberInt * secondNumberInt
fmt.Print("gobot: The product of the two numbers: ")
fmt.Print(mathSum)
} else if strings.Contains(res1, "/") {
// get index of where "*" is located
slashLocatedAt := strings.Index(res1, "/")
// replace "+" with "" so math can be done
mathToDo := (strings.ReplaceAll(res1, "/", ""))
// get what the first number to do math is
firstNumber := (mathToDo[0:slashLocatedAt])
firstNumberInt, _ := strconv.Atoi(firstNumber)
// get second number to do math with
mathLength := len(mathToDo)
secondNumberInt, _ := strconv.Atoi(mathToDo[slashLocatedAt:mathLength])
if secondNumberInt == 0 {
fmt.Println("gobot: I can't divide by zero!")
} else {
// print out the completed problem
mathSum := firstNumberInt / secondNumberInt
fmt.Print("gobot: The quotient of the two numbers: ")
fmt.Print(mathSum)
}
} else if strings.Contains(res1, "-") {
// get index of where "*" is located
minusLocatedAt := strings.Index(res1, "-")
// replace "+" with "" so math can be done
mathToDo := (strings.ReplaceAll(res1, "-", ""))
// get what the first number to do math is
firstNumber := (mathToDo[0:minusLocatedAt])
firstNumberInt, _ := strconv.Atoi(firstNumber)
// get second number to do math with
mathLength := len(mathToDo)
secondNumberInt, _ := strconv.Atoi(mathToDo[minusLocatedAt:mathLength])
// print out the completed problem
mathSum := firstNumberInt - secondNumberInt
fmt.Print("gobot: The difference of the two numbers: ")
fmt.Print(mathSum)
}
}
}