Advertisement
Pohuyumer

LAB 2.8

Jun 3rd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. #include <stdio.h> // printf, scanf
  2. #include <conio.h> // getch
  3. #include <math.h> // fabs, pow, ceil\floor, log, log10, exp, sqrt
  4. #include <stdlib.h> // randomize, rand
  5. #include <windows.h> // SetConsoleOutputCP(1251); SetConsoleCP(1251);
  6.  
  7. //-------------------------------------------------------------------------—
  8. const double ee[] = { 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001, 0.00000001, 0.000000001, 0.0000000001 };
  9. const int key_func[] = { 23, 26 };
  10. int input(double& a, double& b, int& n) {
  11.     printf("Введите а = ");
  12.     scanf("%lf", &a);
  13.     if ((a <= 0) || (a >= 2))
  14.     {
  15.         printf("Некорректное значение а \n");
  16.         return 1;
  17.     }
  18.     printf("Введите b = ");
  19.     scanf("%lf", &b);
  20.     if ((b >= 2) || (b <= a))
  21.     {
  22.         printf("Некорректное значение b \n");
  23.         return 1;
  24.     }
  25.     printf("Введите n = ");
  26.     scanf("%d", &n);
  27.     if ((n < 1) || (n > 10))
  28.     {
  29.         printf("Некорректное значение n \n");
  30.         return 1;
  31.     }
  32.     return 0;
  33. }
  34.  
  35. double func23(double x)
  36. {
  37.     double f = 0;
  38.     f = 1.0 / (pow(x, 2.0 / 3) + 0.7 * sin(x) - log(x + 1)) - x;
  39.     return f;
  40. }
  41.  
  42. double func26(double x)
  43. {
  44.     double f = 0;
  45.     f = exp(-sin(x) * sin(x)) + 3 * x / (7 * (1 + sqrt(x))) - x;
  46.     return f;
  47. }
  48.  
  49. double FindResultMetodDelenieOtrezkaPopolam(double a, double b, long double e, double func(double x), int& k1) {
  50.     double x;
  51.     k1 = 0;
  52.     do
  53.     {
  54.         x = (b + a) / 2.0;
  55.         k1++;
  56.         if (func(x) * func(b) < 0) {
  57.             a = x;
  58.         }
  59.         else {
  60.             b = x;
  61.         }
  62.     } while (b - a >= e);
  63.     return x;
  64. }
  65.  
  66. double FindResultMetodSekushih(double a, double b, long double e, double func(double x), int& k2) {
  67.     double x1, x2, x3, p;
  68.     k2 = 0;
  69.     x1 = a;
  70.     x2 = b;
  71.     do
  72.     {
  73.         k2++;
  74.         x3 = x1 - func(x1) / ((func(x2) - func(x1)) / (x2 - x1));
  75.         x1 = x2;
  76.         x2 = x3;
  77.         p = x1 - x3;
  78.     } while (fabs(p) >= e);
  79.     return x3;
  80. }
  81. void FuncOut(const int* key_func, int j, double a, double b, int n, long double e, double func(double x), int& k1, int& k2)
  82. {
  83.     int i;
  84.     printf("|-----------------------------------------------------------------------------|\n");
  85.     printf("|   Функция №%d   |   Деление отрезка пополам    |         Метод секущих      |\n", key_func[j]);
  86.     printf("|-----------------|------------------------------|----------------------------|\n");
  87.     printf("|   Погрешность   |     Корень      |  Итераций  |      Корень     | Итераций |\n");
  88.     printf("|-----------------|-----------------|------------|-----------------|----------|\n");
  89.     for (i = 0; i < n; i++)
  90.     {
  91.         double x1 = FindResultMetodDelenieOtrezkaPopolam(a, b, ee[i], *func, k1);
  92.         double x2 = FindResultMetodSekushih(a, b, ee[i], *func, k2);
  93.         printf("| %-15.*lf | %-15.*lf | %10d | %-15.*lf | %8d |\n"\
  94.             , i + 2, ee[i], i + 2, x1
  95.             , k1, i + 2, x2, k2);
  96.     }
  97.     printf("|-----------------------------------------------------------------------------|\n");
  98.     printf("\n\n");
  99. }
  100.  
  101. int main()
  102. {
  103.     SetConsoleOutputCP(1251);
  104.  
  105.     int i, n, k1, k2, j, f1, f2;
  106.     long double e;
  107.     double a, b;
  108.     double(*func[])(double x) = { func23, func26 };
  109.     int anomaliya = input(a, b, n);
  110.  
  111.     if (anomaliya == 1) {
  112.         printf("Введите правильные исходные данные");
  113.     }
  114.     else
  115.     {
  116.         for ((j = 0); j < 2; j++)
  117.         {
  118.             if (func[j](a) * func[j](b) > 0)
  119.             {
  120.                 printf("Нет корня функции №%d на отрезке [A,B] \n", key_func[j]);
  121.  
  122.             }
  123.             else
  124.             {
  125.                 if (j == 0)
  126.                 {
  127.                     FuncOut(key_func, j, a, b, n, e, func23, k1, k2);
  128.                 }
  129.                 if (j == 1)
  130.                 {
  131.                     FuncOut(key_func, j, a, b, n, e, func26, k1, k2);
  132.                 }
  133.             }
  134.         }
  135.     }
  136.     getch();
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement