-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
159 lines (125 loc) · 5.2 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include "approximation.h"
#include "process.h"
#include "deviation.h"
#include "util.h"
#include "graph.h"
typedef std::pair<std::vector<float>, std::vector<float>> Points;
void labInfo() {
std::cout << "==============================" << std::endl;
std::cout << "CHESNOKOV ARKADY" << std::endl;
std::cout << "P33111 GROUP" << std::endl;
std::cout << "INTERPOLATION AND APPROXIMATION" << std::endl;
std::cout << "==============================" << std::endl;
}
Points readFunctionPointsFromFile(std::string &fileName) {
std::ifstream file(fileName);
if (!file.is_open()) {
throw std::runtime_error("Cannot open the file!");
}
std::string line;
std::getline(file, line);
std::istringstream xs_stream(line);
std::vector<float> xs;
float x;
while (xs_stream >> x) {
xs.push_back(x);
}
std::getline(file, line);
std::istringstream ysStream(line);
std::vector<float> ys;
float y;
while (ysStream >> y) {
ys.push_back(y);
}
return {xs, ys};
}
int main() {
labInfo();
std::string fileName = "test.txt";
std::pair<std::vector<float>, std::vector<float>> points = readFunctionPointsFromFile(fileName);
if (points.first.size() != points.second.size()) {
throw std::runtime_error("The number of points x and y don't match!");
}
std::vector<float> xs = points.first;
std::vector<float> ys = points.second;
bool isNegativeX = hasNegativeNumber(xs);
bool isNegativeY = hasNegativeNumber(ys);
std::vector<float> result;
if (isNegativeX && isNegativeY) {
result.push_back(process_lineal(xs, ys));
result.push_back(process_quadratic(xs, ys));
result.push_back(process_qube(xs, ys));
float minValue = * std::min_element(result.begin(), result.end());
std::cout << "Best approx: " << minValue << std::endl;
if (minValue == result[0]) {
std::cout << "The best approximation is lineal" << std::endl;
} else if (minValue == result[1]) {
std::cout << "The best approximation is quadratic" << std::endl;
} else if (minValue == result[2]) {
std::cout << "The best approximation is qube" << std::endl;
}
plotIfXAndYNeg(xs, ys);
} else if (isNegativeX) {
result.push_back(process_lineal(xs, ys));
result.push_back(process_quadratic(xs, ys));
result.push_back(process_qube(xs, ys));
result.push_back(process_exp(xs, ys));
float minValue = * std::min_element(result.begin(), result.end());
std::cout << "Best approx: " << minValue << std::endl;
if (minValue == result[0]) {
std::cout << "The best approximation is lineal" << std::endl;
} else if (minValue == result[1]) {
std::cout << "The best approximation is quadratic" << std::endl;
} else if (minValue == result[2]) {
std::cout << "The best approximation is qube" << std::endl;
} else if (minValue == result[3]) {
std::cout << "The best approximation is exp" << std::endl;
}
plotIfXNeg(xs, ys);
} else if (isNegativeY) {
result.push_back(process_lineal(xs, ys));
result.push_back(process_quadratic(xs, ys));
result.push_back(process_qube(xs, ys));
result.push_back(process_log(xs, ys));
float minValue = *std::min_element(result.begin(), result.end());
std::cout << "Best approx: " << minValue << std::endl;
if (minValue == result[0]) {
std::cout << "The best approximation is lineal" << std::endl;
} else if (minValue == result[1]) {
std::cout << "The best approximation is quadratic" << std::endl;
} else if (minValue == result[2]) {
std::cout << "The best approximation is qube" << std::endl;
} else if (minValue == result[3]) {
std::cout << "The best approximation is log" << std::endl;
}
plotIfYNeg(xs, ys);
} else {
result.push_back(process_lineal(xs, ys));
result.push_back(process_quadratic(xs, ys));
result.push_back(process_qube(xs, ys));
result.push_back(process_power(xs, ys));
result.push_back(process_exp(xs, ys));
result.push_back(process_log(xs, ys));
float minValue = *std::min_element(result.begin(), result.end());
std::cout << "Best approx: " << minValue << std::endl;
if (minValue == result[0]) {
std::cout << "The best approximation is lineal" << std::endl;
} else if (minValue == result[1]) {
std::cout << "The best approximation is quadratic" << std::endl;
} else if (minValue == result[2]) {
std::cout << "The best approximation is qube" << std::endl;
} else if (minValue == result[3]) {
std::cout << "The best approximation is power" << std::endl;
} else if (minValue == result[4]) {
std::cout << "The best approximation is exp" << std::endl;
} else if (minValue == result[5]) {
std::cout << "The best approximation is log" << std::endl;
}
plotAllGraphs(xs, ys);
}
return 0;
}