Advertisement
edgarrii

lb4

Jan 12th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 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.  
  10. double Count_Y(double, int);
  11. double Count_S(double, int);
  12. double Count_Ost(double, int);
  13.  
  14. typedef double(*CAF) (double, int);
  15. void   Print_Rez(CAF, double, double, double, int);
  16. void   Print_Rez_All(CAF, CAF, CAF, double, double, double, int);
  17.  
  18.  
  19.  
  20. int    main()
  21. {
  22.     system("color 08");
  23.     setlocale(0, "rus");
  24.     HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  25.  
  26.     double a = 0, b = 0, h = 0;
  27.     int n = 0;
  28.  
  29.  
  30.  
  31.     cout << "\nFor each {x}, varying from {a} to {b} in step {h}: "  << endl
  32.          << "1.Find function values  Y(x);" << endl
  33.          << "2.Amounts S(x);" << endl
  34.          << "3.|Y(x)–S(x)|;"  << endl
  35.          << "The values {a, b, h, n} are entered from the keyboard." << endl << endl;
  36.  
  37.     do {
  38.  
  39.         cout << "Enter the lower limit {a}... ";
  40.         cin >> a;
  41.         cout << endl;
  42.  
  43.         cout << "Enter the upper limit {b}... ";
  44.         cin >> b;
  45.         cout << endl;
  46.  
  47.         if (a == b) {
  48.             SetConsoleTextAttribute(console, FOREGROUND_RED);
  49.             cout << "\t!ERROR You have entered equal limits, the program cannot be executed. Try again... " << endl << endl;
  50.             SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
  51.             continue;
  52.         }
  53.  
  54.         cout << "Enter step {h} to change variable {x} from limit {a} to {b}...";
  55.         cin >> h;
  56.         cout << endl;
  57.  
  58.         cout << "Enter the upper limit of the number {n}, and {n} is an integer...";
  59.         cin >> n;
  60.         cout << endl;
  61.  
  62.  
  63.         if (n == 0) {
  64.  
  65.             SetConsoleTextAttribute(console, FOREGROUND_RED);
  66.             cout << "\n\t!ERROR {n} can't be equal to 0. Try again... " << endl << endl;
  67.             SetConsoleTextAttribute(console, FOREGROUND_INTENSITY);
  68.             continue;
  69.         }
  70.         cout << endl;
  71.  
  72.         break;
  73.  
  74.     } while (true);
  75.  
  76.     cout << endl << endl;
  77.  
  78.  
  79.     cout << "To display different expressions on the screen, type from the keyboard: " << endl << endl
  80.          << "1.To derive a number Y(x) "          << endl
  81.          << "2.To derive a number S(x) "          << endl
  82.          << "3.To derive a number |Y(x) - S(x)| " << endl
  83.          << "4.To get all the expressions out "   << endl << endl;
  84.  
  85.     int choice = 0;
  86.     cout << "Your choice: ";
  87.     cin >> choice;
  88.     cout << endl;
  89.  
  90.     switch (choice) {
  91.     case 1:
  92.  
  93.         cout << setw(21) << " Y(x) " << endl;
  94.         Print_Rez(Count_Y, a, b, h, n);
  95.         break;
  96.  
  97.     case 2:
  98.  
  99.         cout << setw(21) << " S(x) " << endl;
  100.         Print_Rez(Count_S, a, b, h, n);
  101.         break;
  102.  
  103.     case 3:
  104.  
  105.         cout << setw(26) << " |Y(x) - S(x)| " << endl;
  106.         Print_Rez(Count_Ost, a, b, h, n);
  107.         break;
  108.  
  109.     case 4:
  110.  
  111.         cout << setw(22) <<"Y(x)" << setw(20) << "S(x)" << setw(27) << "|Y(x) - S(x)|" << endl;
  112.         Print_Rez_All(Count_Y, Count_S, Count_Ost, a, b, h, n);
  113.         break;
  114.  
  115.     }
  116.  
  117.  
  118.  
  119.     cout << endl << endl;
  120.  
  121.  
  122.     return 0;
  123. }
  124.  
  125.  
  126.  
  127. double Count_Y(double x, int n) {
  128.  
  129.     double Y = 0;
  130.     Y = (1 + x * x) / 2.0 * atan(x) - (x / 2.0);
  131.     return Y;
  132.  
  133. }
  134.  
  135. double Count_S(double x, int n) {
  136.  
  137.     double E = 1, p = 0, S = 1;
  138.     S = x * x * x / 3.0;
  139.  
  140.  
  141.     for (int k = 2; k <= n; k++) {
  142.  
  143.         E = -E * (x * x);
  144.         p = E * (x * x * x) / ((2.0 * k - 1) * (2.0 * k + 1));;
  145.  
  146.         S += p;
  147.  
  148.     }
  149.  
  150.     return S;
  151. }
  152.  
  153. double Count_Ost(double x, int n) {
  154.  
  155.     double ost = Count_Y(x, n) - Count_S(x, n);
  156.  
  157.     return fabs(ost);
  158.  
  159. }
  160.  
  161. void Print_Rez(CAF aF, double a, double b, double h, int n) {
  162.  
  163.     for (double x = a; x <= b; x += h) {
  164.  
  165.         cout << setw(22) << setprecision(5) << fixed << aF(x, n) << endl;
  166.  
  167.     }
  168.  
  169. }
  170.  
  171. void Print_Rez_All(CAF aF_Y, CAF aF_S, CAF aF_YS, double a, double b, double h, int n) {
  172.  
  173.     for (double x = a; x <= b; x += h) {
  174.  
  175.         cout << setw(22) << setprecision(5) << fixed << Count_Y(x, n)
  176.             << setw(20) << setprecision(5) << Count_S(x, n)
  177.             << setw(23) << setprecision(5) << fixed << Count_Ost(x, n) << endl;
  178.  
  179.  
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement