89 lines
1.6 KiB
C++
89 lines
1.6 KiB
C++
#if WIN32
|
|
#include <algorithm>
|
|
#endif
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
class Wielomian
|
|
{
|
|
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))
|
|
{
|
|
a.insert(a.begin(), stof(str));
|
|
stopien++;
|
|
}
|
|
file.close();
|
|
}
|
|
|
|
void setX(float x)
|
|
{
|
|
this->x = x;
|
|
}
|
|
|
|
int getStopien() {
|
|
return this->stopien;
|
|
}
|
|
|
|
void oblicz()
|
|
{
|
|
b.resize(stopien);
|
|
for (int i = stopien - 1; i >= 0; i--)
|
|
{
|
|
if (i == stopien)
|
|
{
|
|
b[i] = a[i];
|
|
}
|
|
else if (i > 0)
|
|
{
|
|
b[i] = a[i] + b[i + 1] * x;
|
|
}
|
|
else if (i == 0)
|
|
{
|
|
b[i] = a[i] + b[i + 1] * x;
|
|
}
|
|
}
|
|
}
|
|
|
|
float oblicz_rekurencyjnie(int n) {
|
|
if (n == 0) {
|
|
return a[0];
|
|
}
|
|
|
|
return a[n] + x * oblicz_rekurencyjnie(n - 1);
|
|
}
|
|
|
|
void wyswietl_wynik()
|
|
{
|
|
for (int i = b.size() - 1; i >= 0; i--)
|
|
{
|
|
cout << "b" << i << ": " << b[i] << "\n";
|
|
}
|
|
|
|
cout << "Rozwiazaniem wielomianu jest: " << b[0] << endl;
|
|
}
|
|
|
|
void reverse_a() {
|
|
reverse(a.begin(), a.end());
|
|
}
|
|
|
|
private:
|
|
vector<float> a;
|
|
vector<float> b;
|
|
int stopien;
|
|
float x;
|
|
}; |