-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex23.4.go
57 lines (50 loc) · 1.22 KB
/
ex23.4.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
package main
import (
"bufio"
"errors"
"fmt"
"strconv"
"strings"
)
func MultipleFromString(str string) (int, error) {
scanner := bufio.NewScanner(strings.NewReader(str)) // 스캐너 생성
scanner.Split(bufio.ScanWords) // 한 단어씩 끊어 읽기
pos := 0
a, n, err := readNextInt(scanner)
if err != nil {
return 0, fmt.Errorf("Failed to readNextInt(), pos:%d err:%w", pos, err)
}
pos += n + 1
b, n, err := readNextInt(scanner)
if err != nil {
return 0, fmt.Errorf("Failed to readNextInt(), pos:%d err:%w", pos, err)
}
return a * b, nil
}
func readNextInt(scanner *bufio.Scanner) (int, int, error) {
if !scanner.Scan() {
return 0, 0, fmt.Errorf("Failed to scan")
}
word := scanner.Text()
number, err := strconv.Atoi(word)
if err != nil {
return 0, 0, fmt.Errorf("Failed to convert word to int, word:%s err:%w", word, err)
}
return number, len(word), nil
}
func readEq(eq string) {
rst, err := MultipleFromString(eq)
if err != nil {
fmt.Println(rst)
} else {
fmt.Println(err)
var numErr *strconv.NumError
if errors.As(err, &numErr) { // 감싸진 에러가 NumErr 인지 확인
fmt.Println("NumberError: ", numErr)
}
}
}
func main() {
readEq("123 3")
readEq("123 abc")
}