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

7
horner/CMakeLists.txt Normal file
View File

@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.28)
project(MO_pracadomowa)
set(CMAKE_CXX_STANDARD 14)
add_executable(MO_pracadomowa
horner_simple.cpp)

5
horner/dane.txt Normal file
View File

@@ -0,0 +1,5 @@
1
-3.5
0
2
-1

104
horner/horner_class.cpp Normal file
View File

@@ -0,0 +1,104 @@
#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;
};
int main()
{
float x = 0;
Wielomian wielomian;
cout << "Program do obliczenia wielomianu schematem hornera\n";
cout << "Podaj x dla ktorego szukamy rozwiazania: ";
cin >> x;
wielomian.readFromFile("dane.txt");
wielomian.setX(x);
wielomian.oblicz();
wielomian.wyswietl_wynik();
wielomian.reverse_a();
cout << "\nWynik rekurencyjny: " << wielomian.oblicz_rekurencyjnie(wielomian.getStopien() - 1) << endl;
return 0;
}

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