Advertisement
Dimique

Untitled

Dec 11th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.04 KB | None | 0 0
  1. //Определение области на плоскости, в которую попадает точка,
  2. //и площади этой области аналитическим и програмным способом
  3. //методом криволинейных трапеций (средних прямоугольников)
  4. //Рекурсия. Передача имен функций в качестве параметров
  5. #include<iostream>
  6. #include<math.h>
  7. #include<cmath>
  8. #include<locale.h>
  9. #define M_PI 3.14159265358979323846
  10.  
  11. using namespace std;
  12.  
  13. typedef double(*TFun)(double);
  14. double integral(double, double, double, double, TFun, double);
  15. double ydlya2(double x) { return 2; };
  16. double ydlya1(double x) { return 1; };
  17. double ydlyam1(double x) { return -1; };
  18. double ydlyam2(double x) { return -2; };
  19.  
  20. double yupup(double x) {
  21.     return sqrt(1 - (x*x)) + 1;
  22. }
  23. double ydownup(double x) {
  24.     return -sqrt(1 - (x*x)) + 1;
  25. }
  26. double yupleft(double x) {
  27.     return sqrt(1 - ((x + 1)*(x + 1)));
  28. }
  29. double ydownleft(double x) {
  30.     return -sqrt(1 - ((x + 1)*(x + 1)));
  31. }
  32. double yupright(double x) {
  33.     return sqrt(1 - ((x - 1)*(x - 1)));
  34. }
  35. double ydownright(double x) {
  36.     return -sqrt(1 - ((x - 1)*(x - 1)));
  37. }
  38. double yupdown(double x) {
  39.     return sqrt(1 - (x*x)) - 1;
  40. }
  41. double ydowndown(double x) {
  42.     return -sqrt(1 - (x*x)) - 1;
  43. }
  44. double yupcentral(double x) {
  45.     return sqrt(1 - (x*x));
  46. }
  47. double ydowncentral(double x) {
  48.     return -sqrt(1 - (x*x));
  49. }
  50.  
  51. const double eps = 0.00001; //точность вычисления
  52. const double startStep = 0.001; //начальный шаг вычисления
  53.  
  54. int main()
  55. {
  56.     setlocale(LC_ALL, "Rus");
  57.     int place = 0; //номер области
  58.     double x = 0;
  59.     double y = 0;
  60.  
  61.     cout << "Введите координаты точки x: ";
  62.     cin >> x;
  63.     cout << "Введите координаты точки y: ";
  64.     cin >> y;
  65.  
  66.     if ((x*x) + (y - 1)*(y - 1) > 1 && (x - 1)*(x - 1) + (y*y) > 1 && x > -2 && y < 2 && x < 0 && y>0) {
  67.         cout << "Точка в области M1. " << endl;
  68.         place = 1;
  69.     }
  70.     else
  71.         if ((((x*x) + (y*y)) < 1) && (x*x) + (y - 1)*(y - 1) < 1 && x < 0)
  72.         {
  73.             cout << "Точка в области М2. " << endl;
  74.             place = 2;
  75.         }
  76.         else
  77.             if (((x - 1)*(x - 1) + y * y < 1) && (x*x) + (y*y) > 1 && y < 0 && x < 1)
  78.             {
  79.                 cout << "Точка в области М3. " << endl;
  80.                 place = 3;
  81.             }
  82.             else
  83.                 if (((x*x) + (y*y) > 1) && x < 0 && (x*x) + (y + 1)*(y + 1) < 1)
  84.                 {
  85.                     cout << "Точка в области М4. " << endl;
  86.                     place = 4;
  87.                 }
  88.                 else
  89.                     if (y < -1 && (x*x) + (y + 1)*(y + 1) > 1 && x > 0 && x < 2 && y > -2)
  90.                     {
  91.                         cout << "Точка в области М5. " << endl;
  92.                         place = 5;
  93.                     }
  94.                     else
  95.                         cout << "Точка вне выделенных областей";
  96.  
  97.     cout.precision(8); //число знаков после дес. точки
  98.     switch (place)
  99.     {
  100.     case 1:
  101.         cout << "Формула: S1 = " << 3 - M_PI / 2 << endl;
  102.         cout << "Интеграл: S1 = " << integral(-2, 0, startStep, eps, ydlya2, 0)
  103.             - integral(-2, -1, startStep, eps, yupleft, 0)
  104.             - integral(-1, 0, startStep, eps, yupup, 0) << endl;
  105.         break;
  106.     case 2:
  107.         cout << "Формула: S2 = " << 2 - M_PI / 2 << endl;
  108.         cout << "Интеграл: S2 = " << integral(-1, 1, startStep, eps, ydlya1, 0)
  109.             - integral(-1, 0, startStep, eps, yupleft, 0)
  110.             - integral(0, 1, startStep, eps, yupright, 0) << endl;
  111.         break;
  112.     case 3:
  113.         cout << "Формула: S3 = " << 1 - 2 * (1 - M_PI / 4) - (sqrt(3) / 2 + M_PI / 3 - M_PI / 4 - 1) << endl;
  114.         cout << "Интеграл: S3 = " << integral(-0.5, 0, startStep, eps, ydownleft, 0)
  115.             + integral(-sqrt(3) / 2, -0.5, startStep, eps, ydowncentral, 0)
  116.             - integral(-sqrt(3) / 2, 0, startStep, eps, yupdown 0);
  117.         break;
  118.     case 4:
  119.         cout << "Формула: S4 = " << 1 << endl;
  120.         cout << "Интеграл: S4 = " << integral(1, 2, startStep, eps, ydownright, 0)
  121.             + integral(0, 1, startStep, eps, yupdown, 0) << endl;
  122.         break;
  123.     case 5:
  124.         cout << "Формула: S5 = " << 2 - M_PI / 4 << endl;  
  125.         cout << "Интеграл: S5 = " << integral(0, 2, startStep, eps, ydlya2, 0) - integral(0, 1, startStep, eps, ydlya1, 0)
  126.             - integral(0, 1, startStep, eps, ydowndown, 0);
  127.     }  system("pause");  return 0;
  128. }
  129.  
  130.  
  131.  
  132. //нахождение определенного интеграла методом левых прямоугольников
  133.  
  134. double integral(double begin, double end, double shag, double eps, TFun fun, double previousArea)
  135.  
  136. {
  137.     double currentArea = 0;
  138.     size_t countSteps = (end - begin) / shag;
  139.     double sumOfY = 0;
  140.     for (int i = 0; i < countSteps; i++) {
  141.         double currentX = begin + shag * i;
  142.         sumOfY += (fabs(fun(currentX)));  // sumOfY += (fabs(fun(begin + shag * i)));
  143.     }
  144.     currentArea = sumOfY * shag;
  145.  
  146.     if (fabs(currentArea - previousArea) > eps) //разница между интегралом с текущим шагом и интегралом с шагом в двое
  147.         return integral(begin, end, shag / 2, eps, fun, currentArea);
  148.     return currentArea;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement