Advertisement
edgarrii

4

Jan 10th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 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   ChangeTheValue(double&, double&);
  10. void   Select_Menu();
  11.  
  12.  
  13.  
  14. double Count_Y(double, int);
  15. double Count_S(double, int);
  16. double Count_Ost(double, int);
  17.  
  18. typedef double(*CAF) (double, int);
  19. void   Print_Rez(CAF, double, double, double, int);
  20. void   Print_Rez_All(CAF, CAF, CAF, double, double, double, int);
  21.  
  22.  
  23.  
  24. int    main()
  25. {
  26.     system("color 08");
  27.     setlocale(0, "rus");
  28.     HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  29.  
  30.     double a = 0, b = 0, h = 0;
  31.     int n = 0;
  32.  
  33.  
  34.  
  35.     cout << "\n\t---------------------------------------------------------------------------------------------" << endl << endl
  36.          << "\tFor each {x}, varying from {a} to {b} in step {h}: " << endl
  37.          << "\t1.Find function values  Y(x);" << endl
  38.          << "\t2.Amounts S(x);" << endl
  39.          << "\t3.|Y(x)–S(x)|;" << endl
  40.          << "\tThe values {a, b, h, n} are entered from the keyboard." << endl << endl
  41.          << "\t----------------------------------------------------------------------------------------------" << endl << endl;;
  42.  
  43.     do {
  44.  
  45.         cout << "\tEnter the lower limit {a}... ";
  46.         cin >> a;
  47.        
  48.  
  49.         cout << "\tEnter the upper limit {b}... ";
  50.         cin >> b;
  51.        
  52.        
  53.  
  54.         if (a > b) {
  55.  
  56.             SetConsoleTextAttribute(console, FOREGROUND_RED);
  57.             cout << "\tERROR! You've entered a lowerlimit larger than the upper one" << endl;
  58.             cout << "\tcause of that they'll swap places: ";   
  59.             SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
  60.  
  61.             ChangeTheValue(a, b);
  62.  
  63.             cout << "a = " << setprecision(10) << a << ", " << "b = " << setprecision(10) << b << ". " << endl << endl;
  64.            
  65.  
  66.         }
  67.        
  68.         else if (a == b) {
  69.             SetConsoleTextAttribute(console, FOREGROUND_RED);
  70.             cout << "\t!ERROR You have entered equal limits, the program cannot be executed. Try again... " << endl << endl;
  71.             SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
  72.             continue;
  73.         }
  74.  
  75.         cout << "\tEnter step {h} to change variable {x} from limit {a} to {b}...";
  76.         cin >> h;
  77.        
  78.         cout << endl;
  79.  
  80.         cout << "\tEnter the upper limit of the number {n}, and {n} is an integer...";
  81.         cin >> n;
  82.  
  83.        
  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.     cin >> choice;
  108.  
  109.  
  110.  
  111.  
  112.     switch (choice) {
  113.     case 1:
  114.  
  115.         cout << setw(21) << " Y(x) " << endl;
  116.         Print_Rez(Count_Y, a, b, h, n);
  117.         break;
  118.  
  119.     case 2:
  120.         cout << setw(21) << " S(x) " << endl;
  121.         Print_Rez(Count_S, a, b, h, n);
  122.         break;
  123.  
  124.     case 3:
  125.         cout << setw(24) << " |Y(x) - S(x)| " << endl;
  126.         Print_Rez(Count_Ost, a, b, h, n);
  127.         break;
  128.  
  129.     case 4:
  130.         cout << "\t_____________________________________________________________________" << endl
  131.             << setw(10) << "|"
  132.             << setw(10) << "Y(x)"
  133.             << setw(10) << "|"
  134.             << setw(10) << "S(x)"
  135.             << setw(10) << "|"
  136.             << setw(16) << "|Y(x) - S(x)|" << setw(10) << "|" << endl;
  137.  
  138.         Print_Rez_All(Count_Y, Count_S, Count_Ost, a, b, h, n);
  139.         break;
  140.  
  141.     }
  142.  
  143.  
  144.  
  145.     cout << "\n\n\t ----------------------------------------------------------------------------------------------" << endl << endl;
  146.                  
  147.  
  148.     return 0;
  149. }
  150.  
  151. void   ChangeTheValue(double& a, double& b) {
  152.  
  153.     double temp = 0;
  154.     temp = a;
  155.     a = b;
  156.     b = temp;
  157.  
  158. }
  159. void   Select_Menu() {
  160.  
  161.     cout << "\n\tTo display different expressions on the screen, type from the keyboard: " << endl << endl
  162.          << "\t\t1.To derive a number Y(x) "          << endl
  163.          << "\t\t2.To derive a number S(x) "          << endl
  164.          << "\t\t3.To derive a number |Y(x) - S(x)| " << endl
  165.          << "\t\t4.To get all the expressions out "   << endl
  166.                                                       << endl;
  167.  
  168. }
  169.  
  170.  
  171.  
  172.  
  173. double Count_Y(double x, int n) {
  174.  
  175.     double Y = 0;
  176.     Y = (1 + x * x) / 2.0 * atan(x) - (x / 2.0);
  177.     return Y;
  178.  
  179. }
  180.  
  181. double Count_S(double x, int n) {
  182.  
  183.     double E = 1, p = 0, S = 1;
  184.  
  185.     for (int k = 1; k <= n; k++) {
  186.  
  187.         E = -E * (x * x) / (2.0 * k * (2.0 * k - 1));
  188.         p = (2.0 * k * k + 1) * E;
  189.  
  190.         S += p;
  191.  
  192.     }
  193.  
  194.     return S;
  195. }
  196.  
  197. double Count_Ost(double x, int n) {
  198.  
  199.     double ost = Count_Y(x, n) - Count_S(x, n);
  200.  
  201.     return fabs(ost);
  202.  
  203. }
  204.  
  205.  
  206.  
  207.  
  208. void Print_Rez(CAF aF, double a, double b, double h, int n) {
  209.  
  210.     for (double x = a; x <= b; x += h) {
  211.  
  212.         cout << setw(22) << setprecision(5) << fixed << aF(x, n) << endl;
  213.  
  214.     }
  215.  
  216. }
  217.  
  218. void Print_Rez_All(CAF aF_Y, CAF aF_S, CAF aF_YS, double a, double b, double h, int n) {
  219.  
  220.     for (double x = a; x <= b; x += h) {
  221.  
  222.         cout << setw(22) << setprecision(5) << fixed << Count_Y(x, n)
  223.              << setw(20) << setprecision(5) << Count_S(x, n)
  224.              << setw(23) << setprecision(5) << fixed << Count_Ost(x, n) << endl;
  225.            
  226.  
  227.     }
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement