Advertisement
edgarrii

ed.t

Jan 10th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.35 KB | None | 0 0
  1. //CountAnyFunction - тип указателя на функцию, которая принимает 2 параметра
  2. //типа double и сама возвращает значение типа double
  3. #include <iostream>                                                    
  4. #include <conio.h>
  5. #include <windows.h>
  6. #include <iomanip> 
  7.  
  8. using namespace std;
  9.  
  10. void Swap(double*, double*);
  11. double FindModule(double);
  12. double Block();
  13.  
  14. void SelectMenu();
  15. int Block_Switch();
  16. int Block_Int();
  17.  
  18. double CountY(double, int);
  19. double CountS(double, int);
  20. double CountResidual(double, int);
  21.  
  22. typedef double(*CountAnyFunction) (double, int);               
  23. void OutRez(CountAnyFunction, double, double, double, int);
  24.  
  25. void OutRezAll(CountAnyFunction, CountAnyFunction, CountAnyFunction, double, double, double, int);
  26.  
  27.  
  28.  
  29. int main()
  30. {
  31.     system("color 08");
  32.     setlocale(0, "rus");
  33.     HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  34.  
  35.     double a = 0, b = 0, h = 0;
  36.     int n = 0;
  37.  
  38.  
  39.  
  40.     cout << "\n\t---------------------------------------------------------------------------------------------" << endl << endl
  41.          << "\tFor each {x}, varying from {a} to {b} in step {h}: " << endl
  42.          << "\t1.Find function values  Y(x);" << endl
  43.          << "\t2.Amounts S(x);" << endl
  44.          << "\t3.|Y(x)–S(x)|;" << endl
  45.          << "\tThe values {a, b, h, n} are entered from the keyboard" << endl << endl
  46.          << "\t----------------------------------------------------------------------------------------------" << endl << endl;;
  47.  
  48.     do {
  49.  
  50.         cout << "\tEnter the lower limit {a}... ";
  51.         a = Block();
  52.         cout << endl;
  53.  
  54.         cout << "\tEnter the upper limit {b}... ";
  55.         b = Block();
  56.         cout << endl;
  57.  
  58.         if (a > b) {
  59.  
  60.             SetConsoleTextAttribute(console, FOREGROUND_RED);
  61.             cout << "ERROR! You've entered a lowerlimit larger than the upper one, cause of that they'll swap places: ";
  62.             SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
  63.  
  64.             Swap(&a, &b);
  65.  
  66.             cout << "a = " << setprecision(10) << a << ", " << "b = " << setprecision(10) << b << ". " << endl << endl;
  67.            
  68.  
  69.         }
  70.        
  71.         else if (a == b) {
  72.             SetConsoleTextAttribute(console, FOREGROUND_RED);
  73.             cout << "!ERROR You have entered equal limits, the program cannot be executed. Try again... " << endl << endl;
  74.             SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
  75.             continue;
  76.         }
  77.  
  78.         cout << "\tEnter step {h} to change variable {x} from limit {a} to {b}...";
  79.         h = FindModule(Block());
  80.         cout << endl;
  81.  
  82.         cout << "\tEnter the upper limit of the number {n}, and {n} is an integer...";
  83.  
  84.         n = FindModule(Block_Int());
  85.         if (n == 0) {
  86.  
  87.             SetConsoleTextAttribute(console, FOREGROUND_RED);
  88.             cout << "\n !ERROR {n} can't be equal to 0. Try again... " << endl << endl;
  89.             SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
  90.             continue;
  91.         }
  92.         cout << endl;
  93.  
  94.         break;
  95.  
  96.     } while (true);
  97.  
  98.     cout << "\n\t-----------------------------------------------------------------------------------------------" << endl << endl;
  99.  
  100.  
  101.     SelectMenu();
  102.  
  103.     int choice = 0;
  104.  
  105.  
  106.     cout << "\tYour choise:";
  107.     choice = Block_Switch();
  108.  
  109.     switch (choice) {
  110.     case 1:
  111.  
  112.         cout << setw(21) << " Y " << endl;
  113.         OutRez(CountY, a, b, h, n);
  114.         break;
  115.  
  116.     case 2:
  117.         cout << setw(21) << " S " << endl;
  118.         OutRez(CountS, a, b, h, n);
  119.         break;
  120.  
  121.     case 3:
  122.         cout << setw(24) << " |Y - S| " << endl;
  123.         OutRez(CountResidual, a, b, h, n);
  124.         break;
  125.  
  126.     case 4:
  127.         cout << "\t_____________________________________________________________________" << endl
  128.             << setw(10) << "|"
  129.             << setw(10) << "Y"
  130.             << setw(10) << "|"
  131.             << setw(10) << "S"
  132.             << setw(10) << "|"
  133.             << setw(16) << "|Y - S|" << setw(10) << "|" << endl;
  134.  
  135.         OutRezAll(CountY, CountS, CountResidual, a, b, h, n);
  136.         break;
  137.  
  138.     }
  139.  
  140.  
  141.  
  142.     cout << "\n\n\t ----------------------------------------------------------------------------------------------" << endl << endl;
  143.                  
  144.  
  145.     return 0;
  146. }
  147.  
  148. void Swap(double* a, double* b) {
  149.  
  150.     double additional = 0;
  151.     additional = *a;
  152.     *a = *b;
  153.     *b = additional;
  154.  
  155. }
  156.  
  157. double FindModule(double x) {
  158.  
  159.     if (x < 0)
  160.         x = -x;
  161.  
  162.     return x;
  163. }
  164.  
  165. double Block() {
  166.  
  167.     int c = 0;
  168.     int arr[20];
  169.     int size = 0;
  170.     double sumFirst = 0;
  171.     double sumSecond = 0;
  172.     double sum = 0;
  173.  
  174.     int  fullStop = -1;
  175.     bool forMinus = false;
  176.  
  177.     while (c != 13 || size == 0) {          //enter
  178.         c = _getch();
  179.  
  180.         if ((c >= 48 && c <= 57) || (c == 46 && size != 0) || (c == 45)) {
  181.  
  182.             if (c == 46) {          //'.'
  183.                 if (fullStop != -1) {
  184.                     continue;
  185.                 }
  186.                 fullStop = size;
  187.                 arr[size] = c - 48;
  188.                 size++;
  189.                 cout << '.';
  190.             }
  191.  
  192.             else if (c == 45) {         //'-'
  193.                 if (forMinus == true || size != 0) {
  194.                     continue;
  195.                 }
  196.                 forMinus = true;
  197.                 cout << '-';
  198.             }
  199.  
  200.             else {
  201.                 cout << c - 48;
  202.                 arr[size] = c - 48;
  203.                 size++;
  204.             }
  205.         }
  206.     }
  207.  
  208.  
  209.     for (int i = 0; i < size; i++)
  210.     {
  211.         if (i < fullStop)
  212.             sumFirst += arr[i] * pow(10, fullStop - i - 1);
  213.         else if (i == fullStop)
  214.             continue;
  215.         else if (i > fullStop)
  216.             sumSecond += arr[i] * pow(10, size - i - 1);
  217.  
  218.  
  219.     }
  220.  
  221.     for (int i = 0; i < size; i++) {
  222.         if (fullStop != -1)
  223.             sum = sumFirst + sumSecond / (pow(10, size - fullStop - 1));
  224.         else            //(fullStop = -1)
  225.             sum = sumSecond;
  226.  
  227.  
  228.     }
  229.  
  230.     if (forMinus == true)
  231.         sum = -sum;
  232.  
  233.     cout << endl;
  234.  
  235.     return sum;
  236.  
  237. }
  238.  
  239. void SelectMenu() {
  240.  
  241.     cout << "\n\tTo display different expressions on the screen, type from the keyboard: " << endl << endl
  242.          << "\t\t1.To derive a number Y(x) " << endl
  243.          << "\t\t2.To derive a number S(x) " << endl
  244.          << "\t\t3.To derive a number |Y(x) - S(x)| " << endl
  245.          << "\t\t4.To get all the expressions out " << endl << endl;
  246.  
  247. }
  248.  
  249. int Block_Switch() {
  250.  
  251.     int c;
  252.  
  253.     while (true) {
  254.         c = _getch();
  255.         if (c >= '1' && c <= '4') {
  256.             cout << c - 48 << endl;
  257.             break;
  258.         }
  259.     }
  260.  
  261.     cout << endl;
  262.     return c - 48;
  263.  
  264. }
  265.  
  266. int Block_Int() {
  267.  
  268.     int c = 0;
  269.     int arr[20];
  270.     int size = 0;
  271.     int sum = 0;
  272.  
  273.     bool forMinus = false;
  274.  
  275.     while (c != 13 || size == 0) {          //enter
  276.         c = _getch();
  277.  
  278.         if ((c >= 48 && c <= 57) || (c == 45)) {
  279.  
  280.             if (c == 45) {          //'-'
  281.                 if (forMinus == true || size != 0) {
  282.                     continue;
  283.                 }
  284.                 forMinus = true;
  285.                 cout << '-';
  286.             }
  287.             else {
  288.                 cout << c - 48;
  289.                 arr[size] = c - 48;
  290.                 size++;
  291.             }
  292.  
  293.         }
  294.     }
  295.  
  296.     for (int i = 0; i < size; i++)
  297.     {
  298.  
  299.         sum += arr[i] * pow(10, size - i - 1);
  300.  
  301.     }
  302.  
  303.     if (forMinus == true)
  304.         sum = -sum;
  305.     cout << "   ";
  306.     return sum;
  307.  
  308. }
  309.  
  310. double CountY(double x, int n) {
  311.  
  312.     double Y = 0;
  313.     Y = (1 + x * x) / 2.0 * atan(x) - x / 2.0;
  314.     return Y;
  315.  
  316. }
  317.  
  318. double CountS(double x, int n) {
  319.  
  320.     double E = 1, p = 0, S = 1;
  321.  
  322.     for (int k = 1; k <= n; k++) {
  323.  
  324.         E = -E * (x * x) / (2.0 * k * (2.0 * k - 1));
  325.         p = (2.0 * k * k + 1) * E;
  326.  
  327.         S += p;
  328.  
  329.     }
  330.  
  331.     return S;
  332. }
  333.  
  334. double CountResidual(double x, int n) {
  335.  
  336.     double residual = CountY(x, n) - CountS(x, n);
  337.  
  338.     return FindModule(residual);
  339.  
  340. }
  341.  
  342. void OutRez(CountAnyFunction anyFunction, double a, double b, double h, int n) {
  343.  
  344.     for (double x = a; x <= b; x += h) {
  345.  
  346.         cout << setw(22) << setprecision(5) << fixed << anyFunction(x, n) << endl;
  347.  
  348.     }
  349.  
  350. }
  351.  
  352. void OutRezAll(CountAnyFunction anyFunctionY, CountAnyFunction anyFunctionS, CountAnyFunction anyFunctionYS, double a, double b, double h, int n) {
  353.  
  354.     for (double x = a; x <= b; x += h) {
  355.  
  356.         cout << setw(22) << setprecision(5) << CountY(x, n)
  357.              << setw(20) << setprecision(5) << CountS(x, n)
  358.              << setw(23) << setprecision(5) << fixed << CountResidual(x, n) << endl;
  359.            
  360.  
  361.     }
  362.  
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement