-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOManager.cpp
81 lines (59 loc) · 1.61 KB
/
IOManager.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
73
74
75
76
77
78
79
80
81
//
// Created by Gianluca on 24/05/2019.
//
#include "IOManager.h"
#include <fstream>
#include <array>
#include <cmath>
#include <iostream>
std::vector<std::shared_ptr<Solution>> IOManager::readInput(char *filename) {
std::vector<std::shared_ptr<Solution>> sols;
std::ifstream ifs(filename);
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
int n;
//read n rows
ifs >> n;
for(int i=0; i<n; i++){
float objf;
bool feasible;
std::array<bool, 20> array{};
//read objf
ifs >> objf;
//read feasible
ifs >> feasible;
//read element of a solution
for(int j=0; j<20; j++)
ifs >> array[j];
sols.push_back(std::make_shared<Solution>(objf, array, feasible));
}
ifs.close();
return sols;
}
void IOManager::writeOutput(char *filename, std::vector<std::shared_ptr<Solution>> &vect) {
std::ofstream ofs(filename);
ofs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ofs << vect.size() << std::endl;
for(auto& sol : vect){
ofs << sol->to_string();
ofs << std::endl;
}
}
double IOManager::getMaxAbsValue(std::string filename) {
std::ifstream ifs(filename);
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
double max = -1, temp;
while(true){
try {
ifs >> temp; //nothing
}
catch(...){
//end of file
break;
}
ifs >> temp; //value
temp = std::abs(temp);
if(max == -1 || temp > max)
max = temp;
}
return max;
}