Advertisement
edgarrii

ed

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