C++ Bc. 13 cpp: Porovnání verzí
Bez shrnutí editace |
mBez shrnutí editace |
||
(Není zobrazeno 6 mezilehlých verzí od 2 dalších uživatelů.) | |||
Řádek 3: | Řádek 3: | ||
double polynom(double x, const std::vector<double>& a); | double polynom(double x, const std::vector<double>& a); | ||
void soucet (const std::vector<double>& q, const std::vector<double>& r, | |||
std::vector<double>& p); | |||
int main() | int main() | ||
Řádek 15: | Řádek 17: | ||
p.push_back(7); | p.push_back(7); | ||
for (double x=0; x<1.05; x+=0.1) | |||
{ | |||
cout << x << "\t" << polynom(x, p) << endl; | |||
} | |||
cout << endl; | |||
vector<double> q, r; | |||
q.push_back( 3); | |||
q.push_back(-1); | |||
q.push_back( 2); | |||
r.push_back(6); | |||
r.push_back(3); | |||
r.push_back(3); | |||
r.push_back(2); | |||
r.push_back(7); | |||
soucet(q, r, p); | |||
for (double x=0; x<1.05; x+=0.1) | for (double x=0; x<1.05; x+=0.1) | ||
{ | { | ||
Řádek 25: | Řádek 45: | ||
{ | { | ||
int n = a.size(); | int n = a.size(); | ||
if (n == 0) return 0; // prazdny polynom neni definovan | |||
double h = a[--n]; | double h = a[--n]; | ||
do | do while (n) | ||
{ | { | ||
h *= x; | h *= x; | ||
h += a[--n]; | h += a[--n]; | ||
} | } | ||
return h; | return h; | ||
} | |||
void soucet (const std::vector<double>& q, const std::vector<double>& r, | |||
std::vector<double>& p) | |||
{ | |||
// ve vystupnim kontejneru 'p' musime nejprve zrusit vsechny jeho prvky | |||
p.clear(); | |||
const int nq = q.size(); | |||
const int nr = r.size(); | |||
const int max = std::max(nq, nr); | |||
for (int i=0; i<max; i++) | |||
{ | |||
double s = 0; | |||
if (i < nq) s += q[i]; | |||
if (i < nr) s += r[i]; | |||
p.push_back(s); | |||
} | |||
} | } | ||
Řádek 46: | Řádek 87: | ||
0.9 20.9007 | 0.9 20.9007 | ||
1 25 | 1 25 | ||
0 9 | |||
0.1 9.2527 | |||
0.2 9.6272 | |||
0.3 10.1607 | |||
0.4 10.9072 | |||
0.5 11.9375 | |||
0.6 13.3392 | |||
0.7 15.2167 | |||
0.8 17.6912 | |||
0.9 20.9007 | |||
1 25 | |||
[ [[C++ Bc. 13|Zpět]] ] | |||
[ [[C | [[Kategorie:Programování]] | ||
[[Kategorie:C++]] |
Aktuální verze z 1. 4. 2016, 07:42
#include <iostream> #include <vector> double polynom(double x, const std::vector<double>& a); void soucet (const std::vector<double>& q, const std::vector<double>& r, std::vector<double>& p); int main() { using namespace std; vector<double> p; p.push_back(9); p.push_back(2); p.push_back(5); p.push_back(2); p.push_back(7); for (double x=0; x<1.05; x+=0.1) { cout << x << "\t" << polynom(x, p) << endl; } cout << endl; vector<double> q, r; q.push_back( 3); q.push_back(-1); q.push_back( 2); r.push_back(6); r.push_back(3); r.push_back(3); r.push_back(2); r.push_back(7); soucet(q, r, p); for (double x=0; x<1.05; x+=0.1) { cout << x << "\t" << polynom(x, p) << endl; } } double polynom(double x, const std::vector<double>& a) { int n = a.size(); if (n == 0) return 0; // prazdny polynom neni definovan double h = a[--n]; do while (n) { h *= x; h += a[--n]; } return h; } void soucet (const std::vector<double>& q, const std::vector<double>& r, std::vector<double>& p) { // ve vystupnim kontejneru 'p' musime nejprve zrusit vsechny jeho prvky p.clear(); const int nq = q.size(); const int nr = r.size(); const int max = std::max(nq, nr); for (int i=0; i<max; i++) { double s = 0; if (i < nq) s += q[i]; if (i < nr) s += r[i]; p.push_back(s); } }
0 9 0.1 9.2527 0.2 9.6272 0.3 10.1607 0.4 10.9072 0.5 11.9375 0.6 13.3392 0.7 15.2167 0.8 17.6912 0.9 20.9007 1 25 0 9 0.1 9.2527 0.2 9.6272 0.3 10.1607 0.4 10.9072 0.5 11.9375 0.6 13.3392 0.7 15.2167 0.8 17.6912 0.9 20.9007 1 25
[ Zpět ]