Advertisement
Hamikadze

Untitled

Dec 13th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.76 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <climits>
  3. #include <cstdlib>
  4. #include <clocale>
  5. #include <string>
  6. #include <cstdarg>
  7. #include <cmath>
  8. using namespace std;
  9.  
  10. inline char* format(const char* fmt, ...) {
  11.     int size = 512;
  12.     char * buffer = new char[size];
  13.     va_list vl;
  14.     va_start(vl, fmt);
  15.     int nsize = vsnprintf(buffer, size, fmt, vl);
  16.     if (size <= nsize) {
  17.         delete[] buffer;
  18.         buffer = new char[nsize + 1];
  19.         vsnprintf(buffer, size, fmt, vl);
  20.     }
  21.     return buffer;
  22. }
  23.  
  24. void Error(char * const text)
  25. {
  26.     static FILE *file_error = nullptr;
  27.     const char* path_error = "err.txt";
  28.     if (file_error != nullptr || fopen_s(&file_error, path_error, "w") == 0)
  29.     {
  30.         printf("Ошибка. %s\n", text);
  31.         fprintf(file_error, "Ошибка. %s\n", text);
  32.     }
  33.     else
  34.     {
  35.         printf("Ошибка. Не удалось открыть файл %s для записи.\n", path_error);
  36.     }
  37. }
  38.  
  39. void Protocol(char * const text)
  40. {
  41.     static  FILE *file_protocol = nullptr;
  42.     const char* path_protocol = "protocol.txt";
  43.     if (file_protocol != nullptr || fopen_s(&file_protocol, path_protocol, "w") == 0)
  44.     {
  45.         printf("%s\n", text);
  46.         fprintf(file_protocol, "%s\n", text);
  47.     }
  48.     else
  49.     {
  50.         Error(format("Не удалось открыть файл %s для записи.", path_protocol));
  51.     }
  52. }
  53.  
  54. void ArrayPrint(int size, double ** A)
  55. {
  56.     Protocol(format("Загружено точек - %d\nЗагруженные точки : ", size));
  57.     for (int i = 0; i < size; i++)
  58.     {
  59.         Protocol(format("(%.1lf;%.1lf)",
  60.             A[i][0], A[i][1]));
  61.     }
  62.     Protocol("");
  63. }
  64.  
  65. bool ArrayRead(int &size, int maxSize, double ** &A)
  66. {
  67.     const char* path_input = "in.txt";
  68.     FILE *file_input;
  69.     if (fopen_s(&file_input, path_input, "r") == 0)
  70.     {
  71.         int w = 2;
  72.         int g;
  73.         int index = 0;
  74.         while ((g = fscanf_s(file_input, "%lf %lf\n", &A[size][0], &A[size][1])) != EOF)
  75.         {
  76.             index++;
  77.             if (size >= maxSize - 1)
  78.             {
  79.                 Error(format("Достигнут предел размерности массива. Загрежено %d из %d возмозжынх.", size + 1, maxSize));
  80.                 break;
  81.             }
  82.             switch (g)
  83.             {
  84.             case 2:
  85.                 size++;
  86.                 break;
  87.             case 0:
  88.                 Error(format("Отсутствуют валидные координаты на строке %d.", index));
  89.                 fscanf_s(file_input, "%*[^\n]\n", NULL);
  90.                 continue;
  91.             case 1:
  92.                 Error(format("Отсутствует одна из координат координаты на строке %d.", index));
  93.                 fscanf_s(file_input, "%*[^\n]\n", NULL);
  94.                 continue;
  95.             default:;
  96.             }
  97.         }
  98.         if (size <= 0)
  99.         {
  100.             Error(format("Не удалось корректно загрузить массив. Загруженый массив имеет размерность [%dx%d].", size, w));
  101.             return false;
  102.         }
  103.         return true;
  104.     }
  105.     Error(format("Не удалось открыть файл %s для чтения.", path_input));
  106.     return false;
  107. }
  108.  
  109. double Hypotenuse(double a1, double a2)
  110. {
  111.     return sqrtf(powf(a1, 2) + powf(a2, 2));
  112. }
  113.  
  114. double Scalar(double x1, double y1, double x2, double y2)
  115. {
  116.     return x1 * x2 + y1 * y2;
  117. }
  118.  
  119. double Angle(double x1, double y1, double x2, double y2)
  120. {
  121.     constexpr auto M_PI = 3.14159265358979323846;
  122.     double angel = roundf((acosf(Scalar(x1, y1, x2, y2) / (Hypotenuse(x1, y1) *Hypotenuse(x2, y2)))* 180.0 / M_PI) * 100 / 100);
  123.     return (angel == 180 ? 0 : angel);
  124. }
  125.  
  126. bool CheckTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
  127. {
  128.     double angles = 0;
  129.     double _x1 = x1 - x3;
  130.     double _y1 = y1 - y3;
  131.     double _x2 = x2 - x3;
  132.     double _y2 = y2 - y3;
  133.     angles += Angle(_x1, _y1, _x2, _y2);
  134.     _x1 = x3 - x2;
  135.     _y1 = y3 - y2;
  136.     _x2 = x1 - x2;
  137.     _y2 = y1 - y2;
  138.     angles += Angle(_x1, _y1, _x2, _y2);
  139.     _x1 = x2 - x1;
  140.     _y1 = y2 - y1;
  141.     _x2 = x3 - x1;
  142.     _y2 = y3 - y1;
  143.     angles += Angle(_x1, _y1, _x2, _y2);
  144.  
  145.     return (angles != 0 && fmod(angles, 180) == 0);
  146. }
  147.  
  148. bool Contains(bool *** array, int i1, int i2, int i3)
  149. {
  150.     return
  151.         array[i1][i2][i3] || array[i1][i3][i2] ||
  152.         array[i2][i1][i3] || array[i2][i3][i1] ||
  153.         array[i3][i1][i2] || array[i3][i2][i1];
  154. }
  155.  
  156. char* StringTriangle(double x1, double y1, double x2, double y2, double x3, double y3, double x, double y)
  157. {
  158.     return format("точка пересечения медиан - (%.1lf;%.1lf), координаты вершин - { (%.1lf;%.1lf) , (%.1lf;%.1lf) , (%.1lf;%.1lf) }\n",
  159.         x, y,
  160.         x1, y1,
  161.         x2, y2,
  162.         x3, y3);
  163. }
  164.  
  165. void FindIntersection(double x1, double y1, double x2, double y2, double x3, double y3, double &x, double &y)
  166. {
  167.     x = (x1 + x2 + x3) / 3.0;
  168.     y = (y1 + y2 + y3) / 3.0;
  169. }
  170.  
  171. int FindTriangles(int size, bool *** &array, int &count, double ** A)
  172. {
  173.     for (int i1 = 0; i1 < size; i1++)
  174.     {
  175.         for (int i2 = 0; i2 < size; i2++)
  176.         {
  177.             if (i1 == i2) continue;
  178.             double x1 = A[i1][0];
  179.             double y1 = A[i1][1];
  180.             double x2 = A[i2][0];
  181.             double y2 = A[i2][1];
  182.             if (x1 == x2 && y1 == y2) continue;
  183.             for (int i3 = 0; i3 < size; i3++)
  184.             {
  185.                 if (i3 == i1 ||
  186.                     i3 == i2) continue;
  187.                 double x3 = A[i3][0];
  188.                 double y3 = A[i3][1];
  189.                 if (x3 == x1 && y3 == y1 ||
  190.                     x3 == x2 && y3 == y2) continue;
  191.                 if (CheckTriangle(x1, y1, x2, y2, x3, y3))
  192.                     if (!Contains(array, i1, i2, i3))
  193.                     {
  194.                         array[i1][i2][i3] = true;
  195.                         double x = 0;
  196.                         double y = 0;
  197.                         FindIntersection(x1, y1, x2, y2, x3, y3, x, y);
  198.                         Protocol(format("Найден треугольник : %s", StringTriangle(x1, y1, x2, y2, x3, y3, x, y)));
  199.                         count++;
  200.                     }
  201.             }
  202.         }
  203.     }
  204.     return 0;
  205. }
  206.  
  207. void main()
  208. {
  209.     setlocale(LC_CTYPE, "Russian");
  210.     /*printf("Программа выполняет поиск точек, являющихся вершинами ромба.\n");
  211.     printf("А так же поиск ромба с максимальным содержанием точек (включая вершины и точки на ребрах).\n");
  212.     printf("Автор : Визгунов Андрей Дмитриевич ФКТИ 7302\n");
  213.     printf("Версия : 7.Б.1\n\n");*/
  214.  
  215.     int size = 0;
  216.     int const maxSize = 1000;
  217.     double** A = new double*[maxSize];
  218.     for (int i = 0; i < maxSize; i++)
  219.     {
  220.         A[i] = new double[2];
  221.     }
  222.  
  223.     if (ArrayRead(size, maxSize, A))
  224.     {
  225.         bool*** triangles = new bool**[maxSize];
  226.         for (int i = 0; i < maxSize; i++)
  227.         {
  228.             triangles[i] = new bool*[maxSize];
  229.             for (int j = 0; j < maxSize; j++)
  230.             {
  231.                 triangles[i][j] = new bool[maxSize];
  232.                 for (int k = 0; k < maxSize; k++)
  233.                     triangles[i][j][k] = false;
  234.             }
  235.         }
  236.         ArrayPrint(size, A);
  237.         int count = 0;
  238.         FindTriangles(size, triangles, count, A);
  239.         Protocol(format("\n===========РЕЗУЛЬТАТ===========\nВсего найдено треугольников - %d\n", count));
  240.     }
  241.     else
  242.     {
  243.         Error(format("Массив не был загружен. Завершение работы программы."));
  244.     }
  245.     system("pause");
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement