From 4c2ddd91d3a30ceb50ebb1beba59c68edb2a7f52 Mon Sep 17 00:00:00 2001 From: hamx Date: Tue, 15 Oct 2024 15:28:28 +0200 Subject: [PATCH] =?UTF-8?q?Dodano=20metod=C4=99=20do=20obliczenia=20wielom?= =?UTF-8?q?ianu=20poprzez=20rekurencj=C4=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- horner_class.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/horner_class.cpp b/horner_class.cpp index c5775b9..097d699 100644 --- a/horner_class.cpp +++ b/horner_class.cpp @@ -7,7 +7,7 @@ using namespace std; class Wielomian { public: - bool readFromFile(string filename) + void readFromFile(string filename) { string str; stopien = 0; @@ -24,7 +24,6 @@ public: stopien++; } file.close(); - return true; } void setX(float x) @@ -32,7 +31,11 @@ public: this->x = x; } - bool oblicz() + int getStopien() { + return this->stopien; + } + + void oblicz() { b.resize(stopien); for (int i = stopien - 1; i >= 0; i--) @@ -50,7 +53,14 @@ public: b[i] = a[i] + b[i + 1] * x; } } - return true; + } + + float oblicz_rekurencyjnie(int n) { + if (n == 0) { + return a[0]; + } + + return a[n] + x * oblicz_rekurencyjnie(n - 1); } void wyswietl_wynik() @@ -63,6 +73,10 @@ public: cout << "Rozwiazaniem wielomianu jest: " << b[0] << endl; } + void reverse_a() { + reverse(a.begin(), a.end()); + } + private: vector a; vector b; @@ -82,6 +96,9 @@ int main() wielomian.setX(x); wielomian.oblicz(); wielomian.wyswietl_wynik(); + wielomian.reverse_a(); + + cout << "\nWynik rekurencyjny: " << wielomian.oblicz_rekurencyjnie(wielomian.getStopien() - 1) << endl; return 0; } \ No newline at end of file