-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadfiles.go
105 lines (81 loc) · 1.89 KB
/
readfiles.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
package main
import (
"bufio"
"log"
"os"
"strconv"
"strings"
)
// ReadFiles read matrices data from a file
func ReadFiles(nameFile string, sizeMatrix int) ([][]int, [][]int, [][]int) {
matrixA := make([][]int, sizeMatrix)
for key := range matrixA {
matrixA[key] = make([]int, sizeMatrix)
}
matrixB := make([][]int, sizeMatrix)
for key := range matrixB {
matrixB[key] = make([]int, sizeMatrix)
}
matrixSolution := make([][]int, sizeMatrix)
for key := range matrixSolution {
matrixSolution[key] = make([]int, sizeMatrix)
}
file, err := os.Open(nameFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
matrixReading := ""
contRow := 0
const MatrixA, MatrixB, Solution = "Matrix-A", "Matrix-B", "Solution"
for scanner.Scan() {
if scanner.Text() == MatrixA {
matrixReading = MatrixA
continue
}
if scanner.Text() == MatrixB {
matrixReading = MatrixB
contRow = 0
continue
}
if scanner.Text() == Solution {
matrixReading = Solution
contRow = 0
continue
}
switch matrixReading {
case MatrixA:
for ind, value := range strings.Fields(scanner.Text()) {
parsedValue, errParseInt := strconv.Atoi(value)
if errParseInt != nil {
log.Fatal(err)
}
matrixA[contRow][ind] = parsedValue
}
contRow++
case MatrixB:
for ind, value := range strings.Fields(scanner.Text()) {
parsedValue, errParseInt := strconv.Atoi(value)
if errParseInt != nil {
log.Fatal(err)
}
matrixB[contRow][ind] = parsedValue
}
contRow++
case Solution:
for ind, value := range strings.Fields(scanner.Text()) {
parsedValue, errParseInt := strconv.Atoi(value)
if errParseInt != nil {
log.Fatal(err)
}
matrixSolution[contRow][ind] = parsedValue
}
contRow++
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return matrixA, matrixB, matrixSolution
}