-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask012.go
51 lines (48 loc) · 1.29 KB
/
Task012.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
package main
import (
"fmt"
"strconv"
"strings"
"bufio"
"os"
)
func Text2Num(text string) string {
var newLine string = ""
for i, _ := range text {
if (strings.HasPrefix(text[i:], "one")) {
newLine += "1"
} else if (strings.HasPrefix(text[i:], "two")) {
newLine += "2"
} else if (strings.HasPrefix(text[i:], "three")) {
newLine += "3"
} else if (strings.HasPrefix(text[i:], "four")) {
newLine += "4"
} else if (strings.HasPrefix(text[i:], "five")) {
newLine += "5"
} else if (strings.HasPrefix(text[i:], "six")) {
newLine += "6"
} else if (strings.HasPrefix(text[i:], "seven")) {
newLine += "7"
} else if (strings.HasPrefix(text[i:], "eight")) {
newLine += "8"
} else if (strings.HasPrefix(text[i:], "nine")) {
newLine += "9"
} else if (text[i] >= 48 && text[i] <= 57) {
newLine += string(text[i])
}
}
return newLine
}
func main() {
file, _ := os.Open("codes.txt")
scanner := bufio.NewScanner(file)
var sum int = 0
for scanner.Scan() {
var calNum = Text2Num(scanner.Text())
var calCode = calNum[0:1] + calNum[len(calNum)-1:]
calInt, _ := strconv.Atoi(calCode)
fmt.Println(" Value: ", scanner.Text(), " --> ", calInt)
sum += calInt
}
fmt.Println("Sum: ", sum)
}