This repository has been archived on 2025-01-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
MO_pracadomowa/horner/horner_simple.cpp
hamx01 918246c55d Added template for Newton
And placed horner into horner/ folder
2024-11-06 13:45:02 +01:00

83 lines
1.5 KiB
C++

#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;
}