-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileLoader.cpp
72 lines (60 loc) · 1.3 KB
/
fileLoader.cpp
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
#include "fileLoader.h"
#include <string>
#include <fstream>
fileLoader::fileLoader(std::string n) : name(n) {}
void fileLoader::openOutput() {
if (!opened) {
fileOut.open(name + ".txt");
opened = true;
}
}
void fileLoader::closeOutput() {
if (opened) {
fileOut.close();
opened = false;
}
}
void fileLoader::openInput() {
if (!opened) {
fileIn.open(name + ".txt");
opened = true;
}
}
void fileLoader::closeInput() {
if (opened) {
fileIn.close();
opened = false;
}
}
bool fileLoader::isOpened() { return opened; }
void fileLoader::writeMatrix(matrixImpl matrix) {
for (size_t i = 0; i < matrix.data.size(); i++) {
fileOut << "[";
for (size_t l = 0; l < matrix.data[i].size(); l++) {
if (l + 1 < matrix.data[i].size()) fileOut << matrix.data[i][l] << " ";
else fileOut << matrix.data[i][l];
}
fileOut << "]" << std::endl;
}
}
matrixImpl fileLoader::readMatrix() {
std::vector<std::vector<double>> matrix;
std::string line = "";
std::string number = "";
while (std::getline(fileIn, line)) {
std::vector<double> l = {};
for (char ch : line) {
if (ch == '[' || ch == ' ' || ch == ']') {
if (number.size() > 0) {
l.push_back(std::stod(number));
number = "";
}
}
else {
number += ch;
}
}
matrix.push_back(l);
}
return matrixImpl(matrix);
}