From 6dcca69fa00ab38b4aae0088fcacecd973540246 Mon Sep 17 00:00:00 2001 From: hamx01 Date: Thu, 7 Nov 2024 18:40:31 +0100 Subject: [PATCH] =?UTF-8?q?Zmieniono=20nazw=C4=99=20klasy=20na=20Interpola?= =?UTF-8?q?cja?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zaimplementowano metodę Interpolacja.oblicz() do obliczenia interpolacjI Newtona --- interpolacja/Interpolacja.cpp | 83 +++++++++++++++++++++++++++++++++++ interpolacja/input.txt | 6 +++ main.cpp | 7 +-- newton/Newton.cpp | 62 -------------------------- newton/input.txt | 4 -- 5 files changed, 93 insertions(+), 69 deletions(-) create mode 100644 interpolacja/Interpolacja.cpp create mode 100644 interpolacja/input.txt delete mode 100644 newton/Newton.cpp delete mode 100644 newton/input.txt diff --git a/interpolacja/Interpolacja.cpp b/interpolacja/Interpolacja.cpp new file mode 100644 index 0000000..a158b15 --- /dev/null +++ b/interpolacja/Interpolacja.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include + +using namespace std; + +class Interpolacja +{ +public: + void readFromFile(string filename) + { + string str; + fstream file(filename); + if (!file) + { + cerr << "Nie udalo sie otworzyc pliku o nazwie: " << filename << "\n"; + exit(EXIT_FAILURE); + } + while (getline(file, str)) + { + istringstream iss(str); + if (iss >> a >> b) { + x.push_back(a); + y.push_back(b); + } + } + h = x[1] - x[0]; + liczba_wezlow = x.size(); + file.close(); + } + + void oblicz() + { + tablica_roznic.assign(liczba_wezlow, vector(liczba_wezlow)); + + for (int i = 0; i < liczba_wezlow; i++) { + tablica_roznic[i][0] = y[i]; + } + + for (int j = 1; j < liczba_wezlow; j++) { + for (int i = 0; i < liczba_wezlow - j; i++) { + tablica_roznic[i][j] = (tablica_roznic[i + 1][j - 1] - tablica_roznic[i][j - 1]); + } + } + + suma = tablica_roznic[0][0]; + for (int i = 1; i < liczba_wezlow; i++) { + czynnik = tablica_roznic[0][i]; + for (int j = 0; j < i; j++) { + czynnik *= (x[j] - x[0]); + } + suma += czynnik; + } + } + + void wyswietl_tabela() + { + // nagłowki + cout << setw(5) << "i" << setw(10) << "x" << setw(10) << "y"; + for (int j = 1; j < liczba_wezlow; ++j) + cout << setw(10) << "Δ^" + to_string(j); + cout << endl; + + // wartości + for (int i = 0; i < liczba_wezlow; i++) { + cout << setw(5) << i << setw(10) << x[i]; + for (int j = 0; j < liczba_wezlow - i; j++) { + cout << setw(10) << tablica_roznic[i][j]; + } + cout << endl; + } + + cout << "Interpolowany wynik: " << suma << endl; + } + +private: + float a, b, h, suma, czynnik; + int liczba_wezlow; + vector x, y; + vector > tablica_roznic; +}; diff --git a/interpolacja/input.txt b/interpolacja/input.txt new file mode 100644 index 0000000..d14ce02 --- /dev/null +++ b/interpolacja/input.txt @@ -0,0 +1,6 @@ +1 2 +1.5 2.5 +2 3.5 +2.5 4 +3 5.3 +3.5 6.0 \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4b8ff4f..d568315 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ #include -#include "newton/Newton.cpp" +#include "interpolacja/Interpolacja.cpp" #include "horner/horner_class.cpp" int main() @@ -25,9 +25,10 @@ int main() // Newton test part - Newton first; - first.readFromFile("newton/input.txt"); + Interpolacja first; + first.readFromFile("interpolacja/input.txt"); // first.wyswietl_x_y(); + first.oblicz(); first.wyswietl_tabela(); return 0; diff --git a/newton/Newton.cpp b/newton/Newton.cpp deleted file mode 100644 index 13ce055..0000000 --- a/newton/Newton.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include -#include - -using namespace std; - -class Newton -{ -public: - void readFromFile(string filename) - { - string str; - // stopien = 0; - - fstream file(filename); - if (!file) - { - cerr << "Nie udalo sie otworzyc pliku o nazwie: " << filename << "\n"; - exit(EXIT_FAILURE); - } - while (getline(file, str)) - { - std::istringstream iss(str); - if (iss >> a >> b) { - x.push_back(a); - y.push_back(b); - } - } - file.close(); - } - - void oblicz() - { - - } - - void wyswietl_x_y() - { - for (int i = 0; i < x.size(); i++) - { - cout << "x" << i << ": " << x[i] << "\t \t" << "y" << i << ": " << y[i] << "\n"; - } - } - - void wyswietl_tabela() - { - std::cout << std::setw(5) << "i" << std::setw(10) << "x" << std::setw(10) << "y" << std::endl; // Table headers - std::cout << std::string(26, '-') << std::endl; - - for (size_t i = 0; i < x.size(); ++i) - { - std::cout << std::setw(5) << i << std::setw(10) << x[i] << std::setw(10) << y[i] << std::endl; // Table rows - } - } - - - -private: - float a, b; - vector x ,y; -}; \ No newline at end of file diff --git a/newton/input.txt b/newton/input.txt deleted file mode 100644 index 209ee0e..0000000 --- a/newton/input.txt +++ /dev/null @@ -1,4 +0,0 @@ -1 2 -1.5 2.5 -2 3.5 -2.5 4 \ No newline at end of file