Advertisement
edgarrii

11

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