Advertisement
cacodemon665

Лаба 8 Вариант 8

Jan 16th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <math.h>
  4. #include <iomanip>
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. typedef double(*uf)(double, double, int &);
  11. void tabl(double, double, double, double, uf);
  12. double y(double, double, int &);
  13. double s(double, double, int &);
  14.  
  15. int main()
  16. {
  17.     cout << setw(8) << "x" << setw(15) << "y(x)" << setw(10) << "k" << endl;
  18.     tabl(-0.3, 0.4, 0.1, 1e-5, y);
  19.     cout << endl;
  20.     cout << setw(8) << "x" << setw(15) << "s(x)" << setw(10) << "k" << endl;
  21.     tabl(-0.3, 0.4, 0.1, 1e-5, s);
  22.     return 0;
  23. }
  24.  
  25. void tabl(double a, double b, double h, double eps, uf fun)
  26. {
  27.     int k = 0; double sum;
  28.     for (double x = a; x < b + h / 2; x += h)
  29.     {
  30.         if (fabs(x) < 1e-8)
  31.             x = 0;
  32.  
  33.         sum = fun(x, eps, k);
  34.         cout << setw(8) << x << setw(15) << sum << setw(10) << k << endl;
  35.     }
  36. }
  37.  
  38. double y(double x, double eps, int &k)
  39. {
  40.     return 1 / (1 - x);
  41. }
  42.  
  43. double s(double x, double eps, int &k)
  44. {
  45.     double sum = 0, a = 1 / (1 + x), temp = 1;
  46.     k = 1;
  47.     while (fabs(a) > eps)
  48.     {
  49.         sum += a;
  50.         temp *= 2;
  51.        
  52.         a = temp * pow(x, temp - 1) / (1 + pow(x, temp));
  53.         k++;
  54.     }
  55.     return sum;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement