diff --git a/Renal/NextCalculator.cpp b/Renal/NextCalculator.cpp index fedc59a..51e4794 100644 --- a/Renal/NextCalculator.cpp +++ b/Renal/NextCalculator.cpp @@ -23,6 +23,7 @@ #include "NextException.h" #include +#include namespace Renal { @@ -63,4 +64,18 @@ namespace Renal { coords->clear(); coeffs->clear(); } + + double NextCalculator::CalculateInPoint(double x) { + if (coeffs->size() < 1) + throw NextException("I have no coefficients."); + + double result = 0; + int exponent = coeffs->size()-1; + for (std::vector::iterator ite = coeffs->begin(); ite != coeffs->end()-1; ++ite, --exponent) + result += (*ite * pow(x, exponent)); + + result += coeffs->data()[coeffs->size()-1]; + + return result; + } } diff --git a/Renal/NextCalculator.h b/Renal/NextCalculator.h index 5786173..cca7aa6 100644 --- a/Renal/NextCalculator.h +++ b/Renal/NextCalculator.h @@ -38,6 +38,7 @@ namespace Renal { size_t CountCoords(); std::vector GetPolynom(); void Clear(); + double CalculateInPoint(double x); protected: std::vector > *coords; diff --git a/Sensitive.cpp b/Sensitive.cpp index 2397818..8868408 100644 --- a/Sensitive.cpp +++ b/Sensitive.cpp @@ -29,8 +29,7 @@ Renal::NextCalculator *generic_calculator; -void TestWithCoords(std::vector > coords_vector) -{ +void TestWithCoords(std::vector > coords_vector) { generic_calculator->Clear(); for (std::vector >::iterator ite = coords_vector.begin(); ite != coords_vector.end(); ++ite) @@ -43,7 +42,7 @@ void TestWithCoords(std::vector > coords_vector) printf("\t\tf(x) = "); for (std::vector::iterator ite = polynom.begin(); ite != polynom.end(); ++ite) printf("%.2f ", *ite); - printf("\n\n\n\n"); /* 0 1 1 = x + 1 */ + printf("\n"); } } @@ -53,9 +52,14 @@ void TestWithCoords(std::vector > coords_vector) } +void PrintOut(double x) { + double result = generic_calculator->CalculateInPoint(x); -int main() -{ + printf("\t\tf(%.f) = %.f\n\n\n\n", x, result); +} + + +int main() { generic_calculator = new Renal::LaGrangeCalculator(); std::vector > coords; @@ -65,30 +69,35 @@ int main() coords.push_back(std::pair(2., 3.)); coords.push_back(std::pair(3., 4.)); TestWithCoords(coords); /* 0 1 1 = x + 1 */ + PrintOut(8); /* 9 */ coords.clear(); coords.push_back(std::pair(1., 7.)); coords.push_back(std::pair(3., 31.)); coords.push_back(std::pair(5., 71.)); TestWithCoords(coords); /* 2 4 1 = 2x^2 + 4x + 1 */ + PrintOut(7); /* 127 */ coords.clear(); coords.push_back(std::pair(2., 1.)); coords.push_back(std::pair(4., 2.)); coords.push_back(std::pair(8., 4.)); TestWithCoords(coords); /* 0 0.50 0 = x/2 */ + PrintOut(14); /* 7 */ coords.clear(); coords.push_back(std::pair(1., 3.)); coords.push_back(std::pair(2., 7.)); coords.push_back(std::pair(3., 13.)); TestWithCoords(coords); /* 1 1 1 = x^2 + x + 1 */ + PrintOut(5); /* 31 */ coords.clear(); coords.push_back(std::pair(1., 30.)); coords.push_back(std::pair(3., 296.)); coords.push_back(std::pair(4., 525.)); TestWithCoords(coords); /* 32 5 -7 = 32x^2 + 5x - 7 */ + PrintOut(2); /* 131 */ coords.clear(); coords.push_back(std::pair(1., 2.)); @@ -96,6 +105,27 @@ int main() coords.push_back(std::pair(3., 6.)); coords.push_back(std::pair(4., 8.)); TestWithCoords(coords); /* 0 0 2 0 = 2x */ + PrintOut(16); /* 32 */ + + coords.clear(); + coords.push_back(std::pair(1., 1.)); + coords.push_back(std::pair(2., 2.)); + TestWithCoords(coords); /* 1 0 = x */ + PrintOut(9); /* 9 */ + + coords.clear(); + coords.push_back(std::pair(2., 4.)); + coords.push_back(std::pair(4., 8.)); + coords.push_back(std::pair(8., 16.)); + coords.push_back(std::pair(3., 6.)); + coords.push_back(std::pair(5., 10.)); + coords.push_back(std::pair(25., 50.)); + coords.push_back(std::pair(9., 18.)); + coords.push_back(std::pair(11., 22.)); + coords.push_back(std::pair(1., 2.)); + coords.push_back(std::pair(7., 14.)); + TestWithCoords(coords); /* 0 .. 0 2 0 = 2x */ + PrintOut(15); /* 30 */ delete(generic_calculator);