-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule 9.cpp
85 lines (72 loc) · 1.58 KB
/
Module 9.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
82
83
84
85
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
int main(int argc, char* argv[])
{
//9.1 & 9.2 & 9.3
//Check if file name is provided
if (argc == 1) {
std::cerr << "Error: No file name provided\n";
exit(EXIT_FAILURE);
}
//Open input file
std::string strFileName(argv[1]);
std::ifstream ifs(strFileName.c_str());
//Check if input file is open
if (!ifs.is_open()) {
std::cerr << "Error: " << strFileName.c_str()
<< " could not be opened\n";
exit(EXIT_FAILURE);
}
//Read and display measurements
std::vector<double> vec;
for (;;) {
//Read measurements
double x;
ifs >> x;
//If not valid, ignore measurement
if (ifs.fail()) {
ifs.clear();
ifs.ignore();
}
//If valid, display measurement on screen and save in vector
else {
std::cout << x << "\n";
vec.push_back(x);
}
//Stop reading when end of file is reached
if (ifs.eof()) {
break;
}
}
//Close input file
ifs.close();
//Open output file
std::ofstream ofs(strFileName.c_str());
//Check if file is open
if (!ofs.is_open()) {
std::cerr << "Error: " << strFileName.c_str()
<< " could not be opened\n";
exit(EXIT_FAILURE);
}
//Find min and max of measurements
double dMin, dMax;
dMin = vec[0];
dMax = vec[0];
for (int i = 1; i < vec.size(); ++i) {
if (vec[i] > dMax) {
dMax = vec[i];
}
if (vec[i] < dMin) {
dMin = vec[i];
}
}
//Scale values and write them to output file
for (int i = 0; i < vec.size(); ++i) {
ofs << (vec[i] - dMin) / (dMax - dMin) << "\n";
}
//Close output file and terminate programme
ofs.close();
return 0;
}