From 6eea0acaeabaa9eb6b1cadbfeb99a4f01ca64a9b Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Tue, 21 Jan 2025 21:01:07 -0800 Subject: [PATCH] auto-update(nvim): 2025-01-21 21:01:07 --- meson.build | 1 + src/chapter_2/program_1/main.cpp | 117 ++++++++++++++++++++++++++++ src/chapter_2/program_1/tunnel1.dat | 17 ++++ src/chapter_2/program_1/tunnel2.dat | 17 ++++ 4 files changed, 152 insertions(+) create mode 100644 src/chapter_2/program_1/main.cpp create mode 100644 src/chapter_2/program_1/tunnel1.dat create mode 100644 src/chapter_2/program_1/tunnel2.dat diff --git a/meson.build b/meson.build index ea4d3e1..6fcaab6 100644 --- a/meson.build +++ b/meson.build @@ -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_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) diff --git a/src/chapter_2/program_1/main.cpp b/src/chapter_2/program_1/main.cpp new file mode 100644 index 0000000..2686061 --- /dev/null +++ b/src/chapter_2/program_1/main.cpp @@ -0,0 +1,117 @@ +using namespace std; +#include +#include +#include +#include +#include + +void readData(const string &, vector &, vector &); +double interpolation(double, vector &, vector &); +bool isOrdered(const vector &); +void reorder(vector &, vector &); + +void readData(const string &filename, vector &angles, + vector &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 &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 &angles, vector &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 &angles, + vector &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 angles; + vector 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"; + } +} diff --git a/src/chapter_2/program_1/tunnel1.dat b/src/chapter_2/program_1/tunnel1.dat new file mode 100644 index 0000000..b08f159 --- /dev/null +++ b/src/chapter_2/program_1/tunnel1.dat @@ -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 diff --git a/src/chapter_2/program_1/tunnel2.dat b/src/chapter_2/program_1/tunnel2.dat new file mode 100644 index 0000000..6d37066 --- /dev/null +++ b/src/chapter_2/program_1/tunnel2.dat @@ -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 \ No newline at end of file