Advertisement
Alysik

Untitled

Oct 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. constexpr auto Size = 3;
  4. using namespace std;
  5.  
  6. struct Dot {
  7.     double x;
  8.     double y;
  9.     void createDot() {
  10.         cout << "Enter your x : ";
  11.         cin >> x;
  12.         cout << "Enter your y : ";
  13.         cin >> y;
  14.     }
  15. };
  16.  
  17. void printDot(Dot* a, int n) {
  18.     for (int i = 0; i < n; i++) {
  19.         cout <<i <<" point with coordinates = ( " << a[i].x << " , " << a[i].y <<" ) "<< endl;
  20.     }
  21. }
  22.  
  23.  
  24. double getDistance(Dot* a,int n) {
  25.     double t = 0;
  26.     for (int i = 0; i < n; i++) {
  27.         t = t + abs( sqrt((pow(a[i+1].x, 2) - pow(a[i].x, 2)) + (pow(a[i+1].y, 2) - pow(a[i].y, 2))));
  28.     }
  29.     cout << "t" << " = " << t << endl;
  30.     return t;
  31. }
  32.  
  33. double getMaxDot(Dot* a, int n) {
  34.     double maxX = 0, maxY = 0, d = 0;
  35.     double First = 0;
  36.     for (int i = 1; i < n; i++) {
  37.         double d = abs(sqrt((pow(a[i+1].x, 2) - pow(a[i].x, 2)) + (pow(a[i+1].y, 2) - pow(a[i].y, 2))));
  38.         if (d > First) {
  39.             d = First;
  40.             maxX = a[i].x  ;
  41.             maxY = a[i].y  ;
  42.         }
  43.     }
  44.     cout << "Max x = " << maxX << endl;
  45.     cout << "Max y = " << maxY << endl;
  46.     return maxX && maxY;
  47. }
  48.  
  49. void insertionSort(Dot* a, int n) {
  50.     double sortX, sortY, distance1;
  51.     double distance;
  52.     for (int j = 0; j < n - 1;j++) {
  53.         for (int i = 0; i < n-j-1; i++) {
  54.             distance = abs(sqrt((pow(a[i].x, 2) * pow(a[i].x, 2)) + (pow(a[i].y, 2) * pow(a[i].y, 2))));
  55.             distance1 = abs(sqrt((pow(a[i + 1].x, 2) - pow(a[i].x, 2)) + (pow(a[i + 1].y, 2) - pow(a[i].y, 2))));
  56.             if (distance < distance1) {
  57.                 sortX = a[i].x;
  58.                 a[i].x = a[i + 1].x;
  59.                 a[i + 1].x = sortX;
  60.                 sortY = a[i].y;
  61.                 a[i].y = a[i + 1].y;
  62.                 a[i + 1].y = sortY;
  63.             }
  64.         }
  65.     }
  66.     for (int i = 0; i < n; i++) {
  67.         cout << "After sort " << i << " = ( " << a[i].x << " , " << a[i].y << " ) " << endl;
  68.     }
  69. }
  70.  
  71. int main()
  72. {
  73.     Dot mas[5];
  74.     for (int i = 0; i < Size; i++) {
  75.         mas[i].createDot();
  76.     }
  77.     printDot(mas, Size);
  78.     getDistance(mas, Size);
  79.     getMaxDot(mas, Size);
  80.     insertionSort(mas, Size);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement