Działająca wersja zrobiona na dwóch funkcjach
This commit is contained in:
@@ -4,54 +4,38 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int stopien = 0;
|
||||
float x = 0;
|
||||
pair<vector<float>,int> read_data() {
|
||||
string str;
|
||||
|
||||
vector<string> temp;
|
||||
|
||||
cout << "Program do obliczenia wielomianu schemat hornera\n";
|
||||
vector<float> a;
|
||||
int stopien = 0;
|
||||
|
||||
fstream file("dane.txt");
|
||||
if (!file)
|
||||
{
|
||||
cerr << "Nie udalo sie otworzyc plik\n";
|
||||
return -2;
|
||||
cerr << "Nie udalo sie otworzyc pliku\n";
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while (getline(file, str))
|
||||
{
|
||||
temp.push_back(str);
|
||||
a.insert(a.begin(), stof(str));
|
||||
// cout << "a[" << stopien << "]=" << str << endl;
|
||||
stopien++;
|
||||
}
|
||||
file.close();
|
||||
|
||||
if (stopien <= 0)
|
||||
{
|
||||
cout << "Podano niepoprawny stopien!";
|
||||
return -1;
|
||||
cout << "Plik istnieje, ale jest pusty!\n";
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// cout << "stopien = " << stopien << endl;
|
||||
vector<float> a(stopien);
|
||||
return make_pair(a, stopien);
|
||||
}
|
||||
|
||||
vector<float> policz(vector<float> a, int stopien, float x) {
|
||||
vector<float> b(stopien);
|
||||
|
||||
for (int i = stopien - 1, j = 0; i >= 0; i--, j++)
|
||||
{
|
||||
a[i] = stof(temp[j]);
|
||||
// cout << "temp[" << i << "]=" << temp[j] << endl;
|
||||
}
|
||||
|
||||
// for (int i = 0; i < a.size(); i++)
|
||||
// {
|
||||
// cout << "a[" << i << "]=" << a[i] << endl;
|
||||
// }
|
||||
|
||||
cout << "Podaj x dla ktorego szukamy rozwiazania: ";
|
||||
cin >> x;
|
||||
|
||||
for (int i = stopien - 1; i >= 0; i--)
|
||||
{
|
||||
if (i == stopien)
|
||||
@@ -66,10 +50,29 @@ int main()
|
||||
{
|
||||
b[i] = a[i] + b[i + 1] * x;
|
||||
}
|
||||
// cout << i << endl;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
for (int i = 0; i < b.size(); i++)
|
||||
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";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user