Added template for Newton

And placed horner into horner/ folder
This commit is contained in:
2024-11-06 13:45:02 +01:00
parent 4c2ddd91d3
commit 918246c55d
7 changed files with 75 additions and 0 deletions

83
horner/horner_simple.cpp Normal file
View File

@@ -0,0 +1,83 @@
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
pair<vector<float>,int> read_data() {
string str;
vector<float> a;
int stopien = 0;
fstream file("../dane.txt");
if (!file)
{
cerr << "Nie udalo sie otworzyc pliku\n";
exit(EXIT_FAILURE);
}
while (getline(file, str))
{
a.insert(a.begin(), stof(str));
// cout << "a[" << stopien << "]=" << str << endl;
stopien++;
}
file.close();
if (stopien <= 0)
{
cout << "Plik istnieje, ale jest pusty!\n";
exit(EXIT_FAILURE);
}
return make_pair(a, stopien);
}
vector<float> policz(vector<float> a, int stopien, float x) {
vector<float> b(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;
}
}
return b;
}
int main()
{
float x = 0;
cout << "Program do obliczenia wielomianu schematem hornera\n";
pair<vector<float>,int> para = read_data();
vector<float> a = para.first;
int stopien = para.second;
vector<float> b;
cout << "Podaj x dla ktorego szukamy rozwiazania: ";
cin >> x;
b = policz(a, stopien, x);
for (int i = b.size()-1; i >= 0; i--)
{
cout << "b" << i << ": " << b[i] << "\n";
}
cout << "Rozwiazaniem wielomianu jest: " << b[0] << endl;
return 0;
}