Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.71 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define _USE_MATH_DEFINES
  3.  
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <locale.h>
  7. #include <time.h>
  8. #include <math.h>
  9. #include <iostream>
  10. #include <Windows.h>
  11.  
  12. using namespace std;
  13.  
  14. const double PI = 3.1415926535897932384626433832795;
  15.  
  16. typedef double(*functiontype)(double x);
  17.  
  18.  
  19. typedef struct Node
  20. {
  21.     double x, y;
  22. }Node;
  23.  
  24. double Myfunk(double x)
  25. {
  26.     return cos(x);
  27. }
  28. long double R01(long double *v_v[2], long double X, int n)
  29. {
  30.     double x = X - v_v[0][0];
  31.     x *= (X - v_v[0][1]);
  32.     long int fact = 1;
  33.     for (int k = 2; k < n; ++k)
  34.     {
  35.         fact *= k;
  36.         x *= (X - v_v[0][k]);
  37.         x = x / fact;
  38.     }
  39.     return fabs(x);
  40. }
  41.  
  42. long double lag1(long double* v_v[2], long double x, int n)
  43. {
  44.     long double L = 0;
  45.     long double* l = new long double[n];
  46.  
  47.     for (int i = 0; i < n; i++)
  48.         l[i] = 1;
  49.  
  50.     for (int i = 0; i < n; i++)
  51.         for (int j = 0; j < n; j++)
  52.             if (i != j)
  53.                 l[i] *= (x - v_v[0][j]) / (v_v[0][i] - v_v[0][j]);
  54.  
  55.     for (int i = 0; i < n; i++)
  56.         L += v_v[1][i] * l[i];
  57.  
  58.     return L;
  59. }
  60.  
  61. long double lag(Node *Array, long double x, int Count)
  62. {
  63.     long double L = 0;
  64.     long double* l = new long double[Count];
  65.  
  66.     for (int i = 0; i < Count; i++)
  67.         l[i] = 1;
  68.  
  69.     for (int i = 0; i < Count; i++)
  70.         for (int j = 0; j < Count; j++)
  71.             if (i != j)
  72.                 l[i] *= (x - Array[j].x) / (Array[i].x - Array[j].x);
  73.  
  74.     for (int i = 0; i < Count; i++)
  75.         L += Array[i].y * l[i];
  76.  
  77.     return L;
  78. }
  79.  
  80. long double R0(Node *Array, long double X, int Count)
  81. {
  82.     double x = X - Array[0].x;
  83.     x *= (X - Array[1].x);
  84.     long int fact = 1;
  85.     for (int k = 2; k < Count; ++k)
  86.     {
  87.         fact *= k;
  88.         x *= (X - Array[k].x);
  89.         x = x / fact;
  90.     }
  91.     return fabs(x);
  92. }
  93.  
  94. void ValueUniformTable(functiontype *f, Node *Array, double Initial, double End, int Count)
  95. { // Создание равномерной таблицы значений
  96.     double step = abs(Initial - End) / (Count - 1);
  97.     Array[0].x = Initial;
  98.     Array[0].y = (*f)(Array[0].x);
  99.     cout << "   " << "x" << "\t" << "y" << endl;
  100.     cout << "   " << Array[0].x << "\t" << Array[0].y << endl;
  101.     for (int i = 1; i < Count; i++)
  102.     {
  103.         Array[i].x = Array[i - 1].x + step;
  104.         Array[i].y = (*f)(Array[i].x);
  105.         cout << "   " << Array[i].x << "\t" << Array[i].y << endl;
  106.     }
  107.     cout << endl << endl;
  108. }
  109.  
  110. void ValueChebyshevTable( Node *Array, double Initial, double End, int Count)
  111. { // Создание таблицы Чебышевских значений
  112.     cout << "   " << "x" << "\t\t" << "y" << endl;
  113.     cout << "   " << Array[0].x << "\t" << Array[0].y << endl;
  114.     for (int i = 0; i < Count; i++)
  115.     {
  116.         long double coss = cos(M_PI * (2 * (i + 1) - 1) / (2 * Count));
  117.         Array[i].x = 0.5 * (Initial + End) + 0.5 * (End - Initial) * coss;
  118.         Array[i].y = cos(Array[i].x);
  119.         cout << "   " << Array[i].x << "\t" << Array[i].y << endl;
  120.     }
  121. }
  122.  
  123.  
  124.  
  125. double maxt(double Initial, double End, int Count, char ans)
  126. {
  127.     {
  128.         long double* v_v[2];
  129.  
  130.         v_v[0] = new long double[Count];
  131.         v_v[1] = new long double[Count];
  132.  
  133.         if (ans == 'y')
  134.             for (int i = 0; i < Count; i++)
  135.             {
  136.                 long double coss = cos(M_PI * (2 * (i + 1) - 1) / (2 * Count));
  137.                 v_v[0][i] = 0.5 * (Initial + End) + 0.5 * (End - Initial) * coss;
  138.                 v_v[1][i] = cos(v_v[0][i]);
  139.             }
  140.  
  141.         long double a1 = Initial;
  142.         if (ans == 'n')
  143.             for (int i = 0; i < Count; i++)
  144.             {
  145.                 v_v[0][i] = a1;
  146.                 v_v[1][i] = cos(v_v[0][i]);
  147.  
  148.                 a1 += (fabs(Initial) + fabs(End)) / long double(Count - 1);
  149.             }
  150.  
  151.         long double c = R01(v_v, End, Count);
  152.  
  153.         for (long double x = End; x >= Initial; x -= 0.01f) // O(100,85) - center
  154.         {
  155.             if (c < fabs((lag1(v_v, x, Count) - cos(x)))) c = fabs((lag1(v_v, x, Count) - cos(x)));
  156.         }
  157.  
  158.         delete[] v_v[0];
  159.         delete[] v_v[1];
  160.  
  161.         return c;
  162.     }
  163. }
  164.  
  165. double maxf(double Initial, double End, int Count, char ans)
  166. {
  167.     long double* v_v[2];
  168.  
  169.     v_v[0] = new long double[Count];
  170.     v_v[1] = new long double[Count];
  171.  
  172.     if (ans == 'y')
  173.         for (int i = 0; i < Count; i++)
  174.         {
  175.             long double coss = cos(M_PI * (2 * (i + 1) - 1) / (2 * Count));
  176.             v_v[0][i] = 0.5 * (Initial + End) + 0.5 * (End - Initial) * coss;
  177.             v_v[1][i] = cos(v_v[0][i]);
  178.         }
  179.  
  180.     long double a1 = Initial;
  181.     if (ans == 'n')
  182.         for (int i = 0; i < Count; i++)
  183.         {
  184.             v_v[0][i] = a1;
  185.             v_v[1][i] = cos(v_v[0][i]);
  186.  
  187.             a1 += (fabs(Initial) + fabs(End)) / long double(Count - 1);
  188.         }
  189.  
  190.     long double c = R01(v_v, End, Count);
  191.  
  192.     for (long double x = End; x >= Initial; x -= 0.01f) // O(100,85) - center
  193.     {
  194.         if (c < fabs(R01(v_v, x, Count))) c = fabs(R01(v_v, x, Count));
  195.     }
  196.  
  197.     delete[] v_v[0];
  198.     delete[] v_v[1];
  199.  
  200.     return c;
  201. }
  202.  
  203.  
  204.  
  205.  
  206. int main()
  207. {
  208.     setlocale(LC_ALL, "Russian");
  209.     srand(static_cast<int>(time(0)));
  210.  
  211.     long double* v_v[2];
  212.     int CountNodes;
  213.     long double a, b;
  214.     char ans;
  215.     functiontype Func = &Myfunk;
  216.     cout << "Введите количество точек: ";
  217.     cin >> CountNodes;
  218.  
  219.     cout << "Введите промежуток [a, b] через пробел: ";
  220.     cin >> a >> b;
  221.     Node *ArrayNodes = new Node[CountNodes];
  222.  
  223.     cout << "Использовать узлы Чебышёва?(y/n): ";
  224.     cin >> ans;
  225.  
  226.     if (ans == 'y')
  227.         ValueChebyshevTable(ArrayNodes, a, b, CountNodes);
  228.  
  229.     if (ans == 'n')
  230.         ValueUniformTable(&Func, ArrayNodes, a, b, CountNodes);
  231.  
  232.     long double d;
  233.     HDC hDC = GetDC(GetConsoleWindow());
  234.     HPEN PenW =
  235.  
  236.         CreatePen(PS_SOLID, 2, RGB(255, 255, 255));
  237.     HPEN PenR = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  238.     HPEN PenG = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
  239.     HPEN PenB = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
  240.  
  241.     SelectObject(hDC, PenW);
  242.     MoveToEx(hDC, 100 - a * 20, 500, NULL);
  243.     LineTo(hDC, 500 - a * 20, 500);
  244.     MoveToEx(hDC, 300 - a * 20, 300, NULL);
  245.     LineTo(hDC, 300 - a * 20, 700);
  246.  
  247.     for (int i = 0; i < 5; i++)
  248.     {
  249.         MoveToEx(hDC, 300 - a * 20 - (i + 1) * 30, 500 - 3, NULL);
  250.         LineTo(hDC, 300 - a * 20 - (i + 1) * 30, 500 + 3);
  251.         MoveToEx(hDC, 300 - a * 20 - 3, 500 + (i + 1) * 30, NULL);
  252.         LineTo(hDC, 300 - a * 20 + 3, 500 + (i + 1) * 30);
  253.         MoveToEx(hDC, 300 - a * 20 + (i + 1) * 30, 500 - 3, NULL);
  254.         LineTo(hDC, 300 - a * 20 + (i + 1) * 30, 500 + 3);
  255.         MoveToEx(hDC, 300 - a * 20 - 3, 500 - (i + 1) * 30, NULL);
  256.         LineTo(hDC, 300 - a * 20 + 3, 500 - (i + 1) * 30);
  257.     }
  258.  
  259.     for (d = a - 3; d <= b + 3; d += 0.01f) // O(100,85) - center
  260.     {
  261.         MoveToEx(hDC, 30 * d + 300 - a * 20, -30 * cos(d) + 500, NULL);//10 - scale
  262.         LineTo(hDC, 30 * d + 300 - a * 20, -30 * cos(d) + 500);
  263.     }
  264.     SelectObject(hDC, PenR);
  265.     for (d = a - 3; d <= b + 3; d += 0.01f) // O(100,85) - center
  266.     {
  267.         MoveToEx(hDC, 30 * d + 300 - a * 20, -30 * lag(ArrayNodes, d, CountNodes) + 500, NULL);//10 - scale
  268.         LineTo(hDC, 30 * d + 300 - a * 20, -30 * lag(ArrayNodes, d, CountNodes) + 500);
  269.     }
  270.  
  271.     SelectObject(hDC, PenW);
  272.     MoveToEx(hDC, 700 - a * 30, 500, NULL);
  273.     LineTo(hDC, 1100 - a * 30, 500);
  274.     MoveToEx(hDC, 900 - a * 30, 300, NULL);
  275.     LineTo(hDC, 900 - a * 30, 700);
  276.  
  277.  
  278.     for (int i = 0; i < 5; i++)
  279.     {
  280.         MoveToEx(hDC, 900 - a * 30 - (i + 1) * 30, 500 - 3, NULL);
  281.         LineTo(hDC, 900 - a * 30 - (i + 1) * 30, 500 + 3);
  282.         MoveToEx(hDC, 900 - a * 30 - 3, 500 + (i + 1) * 30, NULL);
  283.         LineTo(hDC, 900 - a * 30 + 3, 500 + (i + 1) * 30);
  284.         MoveToEx(hDC, 900 - a * 30 + (i + 1) * 30, 500 - 3, NULL);
  285.         LineTo(hDC, 900 - a * 30 + (i + 1) * 30, 500 + 3);
  286.         MoveToEx(hDC, 900 - a * 30 - 3, 500 - (i + 1) * 30, NULL);
  287.         LineTo(hDC, 900 - a * 30 + 3, 500 - (i + 1) * 30);
  288.     }
  289.  
  290.  
  291.     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  292.  
  293.     SetConsoleTextAttribute(handle, FOREGROUND_RED);
  294.  
  295.     cout << "\t R0 - отн. погр., ";
  296.  
  297.     SelectObject(hDC, PenR);
  298.     for (d = a; d <= b; d += 0.01f) // O(100,85) - center
  299.     {
  300.         MoveToEx(hDC, 30 * d + 900 - a * 30, -30 * fabs(R0(ArrayNodes, d, CountNodes)) + 500, NULL);//10 - scale
  301.         LineTo(hDC, 30 * d + 900 - a * 30, -30 * fabs(R0(ArrayNodes, d, CountNodes)) + 500);
  302.     }
  303.  
  304.     SetConsoleTextAttribute(handle, FOREGROUND_GREEN);
  305.  
  306.     cout << "Rт - точ. погр.";
  307.  
  308.     SelectObject(hDC, PenG);
  309.     for (d = a; d <= b; d += 0.01f) // O(100,85) - center
  310.     {
  311.         MoveToEx(hDC, 30 * d + 900 - a * 30, -30 * fabs(lag(ArrayNodes, d, CountNodes) - cos(d)) + 500, NULL);//10 - scale
  312.         LineTo(hDC, 30 * d + 900 - a * 30, -30 * fabs(lag(ArrayNodes, d, CountNodes) - cos(d)) + 500);
  313.     }
  314.  
  315.     cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nВведите количество проверок: ";
  316.  
  317.     cin >> CountNodes;
  318.  
  319.     int *n1 = new int[CountNodes];
  320.  
  321.     cout << "Введите: ";
  322.  
  323.     for (int i = 0; i < CountNodes; i++)
  324.         cin >> n1[i];
  325.  
  326.     for (int i = 0; i < CountNodes; i++)
  327.     {
  328.         cout << "При n = " << n1[i] << " max R0 = " << maxf(a, b, n1[i], ans) << "\t" << "; max Rт = " << maxt(a, b, n1[i], ans) << endl;
  329.     }
  330.     delete[] n1;
  331.     system("pause");
  332.     return 0;
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement