Qellex

struct 2

Mar 4th, 2022 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <cmath>
  6. #include <locale.h>
  7.  
  8. using namespace std;
  9.  
  10. struct point
  11. {
  12.     float x;
  13.     float y;
  14. };
  15.  
  16. class square
  17. {
  18.     float P;
  19.     float S;
  20.     float ab, bc, cd, da;
  21.     point* mass;
  22.     string type;
  23.  
  24.     void swap_point(point& a1, point& a2)
  25.     {
  26.         point a3;
  27.  
  28.         a3 = a1;
  29.         a1 = a2;
  30.         a2 = a3;
  31.     }
  32.  
  33.     float dlina(point a1, point a2)
  34.     {
  35.         return sqrt((a1.x - a2.x) * (a1.x - a2.x) + (a1.y - a2.y) * (a1.y - a2.y));
  36.     }
  37.  
  38.     float Acos(point a, point b, point c, point d)
  39.     {
  40.         float x1 = a.x - b.x, x2 = c.x - d.x;
  41.         float y1 = a.y - b.y, y2 = c.y - d.y;
  42.         float d1 = sqrt(x1 * x1 + y1 * y1);
  43.         float d2 = sqrt(x2 * x2 + y2 * y2);
  44.         return cos((x1 * x2 + y1 * y2) / (d1 * d2));
  45.     }
  46.  
  47.     int fun1()
  48.     {
  49.         P = -1;
  50.         S = -1;
  51.         for (int i = 0; i < 3; i++)
  52.             for (int j = i + 1; j < 4; j++)
  53.                 if (mass[i].x == mass[j].x && mass[i].y == mass[j].y)
  54.                 {
  55.                     type = "Что-то";
  56.                     return 0;
  57.                 }
  58.  
  59.  
  60.         for (int i = 0; i < 4; i++)
  61.             for (int j = i + 1; j < 4; j++)
  62.                 for (int k = j + 1; k < 4; k++)
  63.                     if (mass[i].x == mass[j].x && mass[j].x == mass[k].x || mass[i].y == mass[j].y && mass[j].y == mass[k].y)
  64.                     {
  65.                         type = "Что-то";
  66.                         return 0;
  67.                     }
  68.  
  69.         for (int i = 0; i < 4; i++)
  70.             for (int j = 0; j < 4; j++)
  71.                 if (mass[i].x < mass[j].x && i != j)
  72.                     swap_point(mass[i], mass[j]);
  73.  
  74.  
  75.  
  76.  
  77.         if (mass[0].y > mass[1].y)
  78.             swap_point(mass[0], mass[1]);
  79.  
  80.         if (mass[3].y > mass[2].y)
  81.             swap_point(mass[3], mass[2]);
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.         float a = dlina(mass[0], mass[1]);
  89.         float b = dlina(mass[1], mass[2]);
  90.         float c = dlina(mass[2], mass[3]);
  91.         float d = dlina(mass[3], mass[0]);
  92.  
  93.         float d1 = dlina(mass[0], mass[2]);
  94.         float d2 = dlina(mass[1], mass[3]);
  95.  
  96.         P = (a + b + c + d);
  97.         S = sqrt((P / 2 - a) * (P / 2 - b) * (P / 2 - c) * (P / 2 - d));
  98.  
  99.         if ((Acos(mass[0], mass[1], mass[3], mass[2]) >= 0.540301 && Acos(mass[0], mass[1], mass[3], mass[2]) <= 0.540303) || (Acos(mass[0], mass[3], mass[1], mass[2]) >= 0.540301 && Acos(mass[0], mass[3], mass[1], mass[2]) <= 0.540303))
  100.             if ((Acos(mass[0], mass[1], mass[3], mass[2]) >= 0.540301 && Acos(mass[0], mass[1], mass[3], mass[2]) <= 0.540303) && (Acos(mass[0], mass[3], mass[1], mass[2]) >= 0.540301 && Acos(mass[0], mass[3], mass[1], mass[2]) <= 0.540303))
  101.             {
  102.                 type = "Параллелограмм";
  103.                 if (a == b && b == c && c == d)
  104.                     if (d1 == d2)
  105.                         type = "Квадрат";
  106.                     else
  107.                         type = "Ромб";
  108.             }
  109.             else
  110.                 type = "Трапеция";
  111.  
  112.         else
  113.             type = "Четырехугольник";
  114.  
  115.         return 1;
  116.     }
  117.  
  118. public:
  119.  
  120.  
  121.     void print_all_inf()
  122.     {
  123.         cout << "P: " << P << endl << "S: " << S << endl << "Type: " << type << endl;
  124.  
  125.         for (int i = 0; i < 4; i++)
  126.             cout << "(" << mass[i].x << ";" << mass[i].y << ")  ";
  127.         cout << endl << endl;
  128.     }
  129.  
  130.  
  131.     square(int choice)
  132.     {
  133.         mass = new point[4];
  134.  
  135.         if (choice != 1 && choice != 2)
  136.         {
  137.             cout << "1) Rand\n2) Input\n\n";
  138.             do
  139.             {
  140.                 cin >> choice;
  141.             } while (choice != 1 && choice != 2);
  142.         }
  143.  
  144.         if (choice == 1)
  145.         {
  146.             for (int i = 0; i < 4; i++)
  147.             {
  148.                 mass[i].x = (float)(rand() % 100);
  149.                 mass[i].y = (float)(rand() % 100);
  150.             }
  151.  
  152.         }
  153.  
  154.         if (choice == 2)
  155.         {
  156.             for (int i = 0; i < 4; i++)
  157.             {
  158.                 cout << "Enter x " << i + 1 << " point: ";
  159.                 cin >> mass[i].x;
  160.                 cout << "Enter y " << i + 1 << " point: ";
  161.                 cin >> mass[i].y;
  162.             }
  163.         }
  164.  
  165.         fun1();
  166.     }
  167.  
  168.  
  169. };
  170.  
  171.  
  172. int main()
  173. {
  174.     setlocale(LC_ALL, "rus");
  175.     srand(time(NULL));
  176.  
  177.     square b(1);
  178.  
  179.     b.print_all_inf();
  180.  
  181.     square c(2);
  182.     c.print_all_inf();
  183.  
  184.     system("pause");
  185.     return 0;
  186. }
Add Comment
Please, Sign In to add comment