auto-update(nvim): 2025-01-21 21:01:07
This commit is contained in:
parent
11712ab019
commit
6eea0acaea
4 changed files with 152 additions and 0 deletions
|
@ -18,3 +18,4 @@ c_2_lab_1_arg = executable(
|
||||||
|
|
||||||
c_2_lab_2 = executable('c_2_lab_2', 'src/chapter_2/lab_2/main.cpp', install: true)
|
c_2_lab_2 = executable('c_2_lab_2', 'src/chapter_2/lab_2/main.cpp', install: true)
|
||||||
c_2_lab_3 = executable('c_2_lab_3', 'src/chapter_2/lab_3/main.cpp', install: true)
|
c_2_lab_3 = executable('c_2_lab_3', 'src/chapter_2/lab_3/main.cpp', install: true)
|
||||||
|
c_2_program_1 = executable('c_2_program_1', 'src/chapter_2/program_1/main.cpp', install: true)
|
||||||
|
|
117
src/chapter_2/program_1/main.cpp
Normal file
117
src/chapter_2/program_1/main.cpp
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
using namespace std;
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void readData(const string &, vector<double> &, vector<double> &);
|
||||||
|
double interpolation(double, vector<double> &, vector<double> &);
|
||||||
|
bool isOrdered(const vector<double> &);
|
||||||
|
void reorder(vector<double> &, vector<double> &);
|
||||||
|
|
||||||
|
void readData(const string &filename, vector<double> &angles,
|
||||||
|
vector<double> &coefficients) {
|
||||||
|
double angle;
|
||||||
|
double coefficient;
|
||||||
|
string line;
|
||||||
|
stringstream line_s;
|
||||||
|
fstream csv = fstream(filename);
|
||||||
|
|
||||||
|
if (!csv.is_open()) {
|
||||||
|
cout << "Error opening " << filename << endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (getline(csv, line)) {
|
||||||
|
line_s = stringstream(line);
|
||||||
|
line_s >> angle;
|
||||||
|
line_s >> coefficient;
|
||||||
|
angles.push_back(angle);
|
||||||
|
coefficients.push_back(coefficient);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isOrdered(const vector<double> &angles) {
|
||||||
|
if (angles.size() <= 1)
|
||||||
|
return true;
|
||||||
|
for (unsigned int i = 1; i < angles.size(); i++) {
|
||||||
|
if (angles.at(i) < angles.at(i - 1)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reorder(vector<double> &angles, vector<double> &coefficients) {
|
||||||
|
unsigned int i;
|
||||||
|
unsigned int j;
|
||||||
|
double temp;
|
||||||
|
if (angles.size() <= 1)
|
||||||
|
return;
|
||||||
|
for (i = 0; i < angles.size(); i++) {
|
||||||
|
for (j = i + 1; j < angles.size(); j++) {
|
||||||
|
if (angles.at(j) < angles.at(i)) {
|
||||||
|
temp = angles.at(j);
|
||||||
|
angles.at(j) = angles.at(i);
|
||||||
|
angles.at(i) = temp;
|
||||||
|
temp = coefficients.at(j);
|
||||||
|
coefficients.at(j) = coefficients.at(i);
|
||||||
|
coefficients.at(i) = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double interpolation(const double target_angle, vector<double> &angles,
|
||||||
|
vector<double> &coefficients) {
|
||||||
|
unsigned int upper = 0;
|
||||||
|
unsigned int lower = 0;
|
||||||
|
|
||||||
|
if (!isOrdered(angles)) {
|
||||||
|
reorder(angles, coefficients);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (angles.back() < target_angle) {
|
||||||
|
lower = angles.size() - 2;
|
||||||
|
upper = angles.size() - 1;
|
||||||
|
} else if (angles.at(0) > target_angle) {
|
||||||
|
lower = 0;
|
||||||
|
upper = 1;
|
||||||
|
} else {
|
||||||
|
unsigned int i = 0;
|
||||||
|
while (i < angles.size() && angles.at(i) <= target_angle) {
|
||||||
|
if (angles.at(i) == target_angle) {
|
||||||
|
return coefficients.at(i);
|
||||||
|
}
|
||||||
|
lower = i;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
upper = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return coefficients.at(lower) +
|
||||||
|
(((target_angle - angles.at(lower)) /
|
||||||
|
(angles.at(upper) - angles.at(lower))) *
|
||||||
|
(coefficients.at(upper) - coefficients.at(lower)));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
vector<double> angles;
|
||||||
|
vector<double> coefficients;
|
||||||
|
double target_angle;
|
||||||
|
bool shouldRepeat = true;
|
||||||
|
string userResponse;
|
||||||
|
string filename = argv[1];
|
||||||
|
|
||||||
|
readData(filename, angles, coefficients);
|
||||||
|
|
||||||
|
while (shouldRepeat) {
|
||||||
|
cout << "Enter an angle:" << endl;
|
||||||
|
cin >> target_angle;
|
||||||
|
cout << interpolation(target_angle, angles, coefficients) << endl;
|
||||||
|
cout << "Want to enter another angle? (y/n)" << endl;
|
||||||
|
cin >> userResponse;
|
||||||
|
shouldRepeat = userResponse == "Yes";
|
||||||
|
}
|
||||||
|
}
|
17
src/chapter_2/program_1/tunnel1.dat
Normal file
17
src/chapter_2/program_1/tunnel1.dat
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
-4.0 -0.182
|
||||||
|
-2.0 -0.056
|
||||||
|
0.0 0.097
|
||||||
|
2.0 0.238
|
||||||
|
4.0 0.421
|
||||||
|
6.0 0.479
|
||||||
|
8.0 0.654
|
||||||
|
10.0 0.792
|
||||||
|
12.0 0.924
|
||||||
|
14.0 1.035
|
||||||
|
15.0 1.076
|
||||||
|
16.0 1.103
|
||||||
|
17.0 1.120
|
||||||
|
18.0 1.121
|
||||||
|
19.0 1.121
|
||||||
|
20.0 1.099
|
||||||
|
21.0 1.059
|
17
src/chapter_2/program_1/tunnel2.dat
Normal file
17
src/chapter_2/program_1/tunnel2.dat
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
-2 -0.056
|
||||||
|
21 1.059
|
||||||
|
4 0.421
|
||||||
|
2 0.238
|
||||||
|
20 1.099
|
||||||
|
12 0.924
|
||||||
|
6 0.479
|
||||||
|
-4 -0.182
|
||||||
|
8 0.654
|
||||||
|
10 0.792
|
||||||
|
0 0.097
|
||||||
|
14 1.035
|
||||||
|
16 1.103
|
||||||
|
18 1.121
|
||||||
|
15 1.076
|
||||||
|
19 1.121
|
||||||
|
17 1.120
|
Loading…
Reference in a new issue