Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. #include <fstream>
  5.  
  6. struct Point{
  7.     double x;
  8.     double y;
  9.     Point(double x, double y): x(x), y(y) {};
  10. };
  11.  
  12. struct Graph{
  13.     std::vector<Point> points;
  14.  
  15.     void AddPoint(const Point& point) {
  16.       points.emplace_back(point);
  17.     }
  18.  
  19.     void PrintToFile(std::string filename) {
  20.       std::ofstream out;
  21.       out.open(filename);
  22.       for (auto point: points) {
  23.         out << point.x << ' ' << point.y << std::endl;
  24.       }
  25.  
  26.       out.close();
  27.     }
  28.  
  29. };
  30.  
  31.  
  32.  
  33. double CalculateValueGorner(const std::vector<double>& coefficients, double x) {
  34.   double result = coefficients.back();
  35.   if (coefficients.size() == 1) {
  36.     return result;
  37.   }
  38.  
  39.   for (int i = int(coefficients.size()) - 2; i >= 0; --i) {
  40.     result = coefficients[i] + result*x;
  41.   }
  42.  
  43.   return result;
  44. }
  45.  
  46. Graph GetPoints(std::function<double(const std::vector<double>& coefficients, double x)> calculate,
  47.         double start, double end, double step, std::vector<double>& coefficients) {
  48.   Graph result;
  49.   for (;start < end; start += step) {
  50.     result.AddPoint(Point(start, calculate(coefficients, start)));
  51.   }
  52.   return result;
  53. }
  54.  
  55. Graph Check(double start, double end, double step) {
  56.   Graph result;
  57.   for (; start < end; start += step) {
  58.     double current_result = start - 2;
  59.     double multiply = current_result;
  60.     for(int i = 1; i < 9; ++i) {
  61.       current_result *= multiply;
  62.     }
  63.  
  64.     result.AddPoint(Point(start, current_result));
  65.   }
  66.   return result;
  67.  
  68. }
  69.  
  70.  
  71.  
  72. int main() {
  73.   std::cout << "Введите степень многочлена" << std::endl;
  74.   size_t range;
  75.   std::cin >> range;
  76.   std::vector<double> coefficients(range+1);
  77.   std::cout << "Введите коэффициенты, от меньшей степени к большей" << std::endl;
  78.   for (size_t i = 0; i < coefficients.size(); ++i) {
  79.     std::cin >> coefficients[i];
  80.   }
  81.  
  82.   Graph gorner_result = GetPoints(CalculateValueGorner, 1.92, 2.08, 0.0001, coefficients);
  83.  
  84.   Graph true_result = Check(1.92, 2.08, 0.0001);
  85.   gorner_result.PrintToFile("gorner_result.txt");
  86.   true_result.PrintToFile("true_result.txt");
  87. }
  88.  
  89. //ввод для (x-2)^9
  90. //9
  91. //-512 2304 -4608 5376 -4032 2016 -672 144 -18 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement