Advertisement
nikitaxe132

Untitled

Oct 29th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct point {
  7.     double x;
  8.     double y;
  9. };
  10. struct matrix {
  11.     double tgs [10][10];
  12. };
  13. void getdata(int value) {
  14.     bool isntCorrect = false;
  15.     do {
  16.         cout << "Enter number of points: ";
  17.         cin >> value;
  18.     if ((value > 2147483647) || (value < -2147483647)) {
  19.         cout << "Invalid file data. Value from " << 2147483647 << "to " << -2147483647 << " expected";
  20.         isntCorrect = true;
  21.     }
  22.     } while (isntCorrect);
  23. }
  24.  
  25. void getInput(point points [10], int size) {
  26.     bool isntCorrect = false;
  27.     for (int i = 0; i < size; i++) {
  28.         do {
  29.             cout << "Enter Point(x, y) #" << (i + 1) << ": ";
  30.             cin >> points[i].x;
  31.             cin >> points[i].y;
  32.             if ((points[i].x < -200000) || (points[i].x > 200000) || (points[i].y < -20000) || (points[i].y > 200000)) {
  33.                 isntCorrect = true;
  34.                 cout << "Invalid point. Please enter this data again";
  35.             }
  36.         } while(isntCorrect);
  37.     }
  38. }
  39.  
  40. int compareDouble(double a, double b) {
  41.     double e = 0.001;
  42.     if ((a == INFINITY) && (b == INFINITY)) {
  43.         return  0;
  44.     }
  45. }
  46. double getTg(point p1, point p2) {
  47.     if (compareDouble(p2.x, p1.x) == 0 ) {
  48.         return INFINITY;
  49.     } else {
  50.         return  (p2.y - p1.y) / (p2.x - p1.x);
  51.     }
  52. }
  53.  
  54. matrix getTgs(point points [10], int size) {
  55.     matrix tgs;
  56.     tgs.tgs[0][0] = 0;
  57.     for (int i = 0; i < size; i++) {
  58.         for (int j = i + 1; j < size; j++) {
  59.             tgs.tgs[i][j] = getTg(points[i],points[j]);
  60.         }
  61.     }
  62.     return tgs;
  63. }
  64.  
  65. int getPath(int map [10][10], matrix tgs, int lineindex, double tg, int step) {
  66.     int counter = 1;
  67.     int size = sizeof(map);
  68.     for (int i = lineindex + 1; i < size; i++) {
  69.         if (map [lineindex] [i] == step) {}
  70.             else {
  71.                 if (compareDouble(tgs.tgs[lineindex][i],tg) == 0) {
  72.                     map [lineindex] [i] = step;
  73.                     counter = 1 + getPath(map,tgs,lineindex,tg,step);
  74.                 }
  75.             }
  76.         }
  77.     return counter;
  78. }
  79.  
  80. int getLongestPath(matrix tgs,int size) {
  81.     int step = 0;
  82.     int maxpath = 0;
  83.     int path;
  84.     int map [10][10];
  85.     for (int i = 0; i < size; i++) {
  86.         for (int j = i + 1; j < size; j++) {
  87.             step++;
  88.             path = getPath(map,tgs,i,tgs.tgs[i][j],step);
  89.             if (path > maxpath) {
  90.                 maxpath = path;
  91.             }
  92.         }
  93.     }
  94.     return  maxpath;
  95. }
  96.  
  97. void body() {
  98.     cout << "This program counts points on same line";
  99.     int n = 0;
  100.     matrix tgs;
  101.     point points [10];
  102.     getdata(n);
  103.     getInput(points,n);
  104.     tgs = getTgs (points [10], n);
  105.     cout << "Number of points: " << getLongestPath(tgs,n);
  106. }
  107.  
  108. int main() {
  109.     body();
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement