Dodano metodę do obliczenia wielomianu poprzez rekurencję

This commit is contained in:
2024-10-15 15:28:28 +02:00
parent d16410c95b
commit 4c2ddd91d3

View File

@@ -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<float> a;
vector<float> 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;
}