negtab

2.19.3 C++

Oct 5th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void printTask()
  7. {
  8.     cout << "Эта программа находит количество всех возиожных треугольников по заданным точкам\n\n";
  9. }
  10.  
  11. string readPathFile()
  12. {
  13.     string pathToFile;
  14.     bool isInCorrect;
  15.     do
  16.     {
  17.         isInCorrect = false;
  18.         cout << "Введите путь к файлу с расширением.txt с количеством точек и координатами точек: ";
  19.         cin >> pathToFile;
  20.         if (pathToFile.size() < 5 || pathToFile[pathToFile.length() - 4] != '.' || pathToFile[pathToFile.length() - 3] != 't' || pathToFile[pathToFile.length() - 2] != 'x' || pathToFile[pathToFile.length() - 1] != 't')
  21.         {
  22.             cout << "Расширение файла не .txt!\n";
  23.             isInCorrect = true;
  24.         }
  25.     } while (isInCorrect);
  26.     return pathToFile;
  27. }
  28.  
  29. bool isNotExists(string& pathToFile)
  30. {
  31.     bool isRight;
  32.     isRight = true;
  33.     ifstream file(pathToFile);
  34.     if (file.good())
  35.         isRight = false;
  36.     file.close();
  37.     return isRight;
  38. }
  39. bool isNotAbleToReading(string& pathToFile)
  40. {
  41.     bool isRight;
  42.     isRight = true;
  43.     ifstream file(pathToFile);
  44.     if (file.is_open())
  45.         isRight = false;
  46.     file.close();
  47.     return isRight;
  48. }
  49. bool isNotAbleToWriting(string& pathToFile)
  50. {
  51.     bool isRight;
  52.     isRight = true;
  53.     ofstream file(pathToFile, ios::app);
  54.     if (file.is_open())
  55.         isRight = false;
  56.     file.close();
  57.     return isRight;
  58. }
  59. bool isEmpty(string& pathToFile)
  60. {
  61.     bool isRight;
  62.     isRight = false;
  63.     ifstream file(pathToFile);
  64.     if (file.peek() == ifstream::traits_type::eof())
  65.         isRight = true;
  66.     file.close();
  67.     return isRight;
  68. }
  69.  
  70. void getFileNormalReading(string& pathToFile)
  71. {
  72.     bool isInCorrect;
  73.     do
  74.     {
  75.         isInCorrect = false;
  76.         pathToFile = readPathFile();
  77.         if (isNotExists(pathToFile))
  78.         {
  79.             isInCorrect = true;
  80.             cout << "Проверьте корректность ввода пути к файлу!\n";
  81.         }
  82.         if (isInCorrect && isNotAbleToReading(pathToFile))
  83.         {
  84.             isInCorrect = true;
  85.             cout << "Файл закрыт для чтения!\n";
  86.         }
  87.         if (isInCorrect && isEmpty(pathToFile))
  88.         {
  89.             isInCorrect = true;
  90.             cout << "Файл пуст!\n";
  91.         }
  92.     } while (isInCorrect);
  93. }
  94. void getFileNormalWriting(string& pathToFile)
  95. {
  96.     bool isInCorrect;
  97.     do
  98.     {
  99.         isInCorrect = false;
  100.         pathToFile = readPathFile();
  101.         if (isNotExists(pathToFile))
  102.         {
  103.             isInCorrect = true;
  104.             cout << "Проверьте корректность ввода пути к файлу!\n";
  105.         }
  106.         if (isInCorrect && isNotAbleToWriting(pathToFile))
  107.         {
  108.             isInCorrect = true;
  109.             cout << "Файл закрыт для записи!\n";
  110.         }
  111.     } while (isInCorrect);
  112. }
  113.  
  114.  
  115.  
  116. int inPutIntWithText(string s, double min, double max) {
  117.     int n;
  118.     bool isInCorrect;
  119.  
  120.     do {
  121.         cout << s << " от " << min << " до " << max << " : ";
  122.         isInCorrect = false;
  123.         cin >> n;
  124.         if (cin.fail() || n > max || n < min)
  125.         {
  126.             isInCorrect = true;
  127.             cin.clear();
  128.             while (cin.get() != '\n');
  129.             cout << "Неверный ввод" << endl;
  130.         }
  131.     } while (isInCorrect);
  132.  
  133.     return n;
  134. }
  135.  
  136. int inPutInt(int min, int max) {
  137.     int n;
  138.     bool isInCorrect;
  139.  
  140.     n = 0;
  141.     isInCorrect = true;
  142.  
  143.     do {
  144.         isInCorrect = false;
  145.         cin >> n;
  146.         if (cin.fail() || n < min || n > max)
  147.         {
  148.             isInCorrect = true;
  149.             cin.clear();
  150.             while (cin.get() != '\n');
  151.             cout << "Неверный ввод" << endl;
  152.         }
  153.     } while (isInCorrect);
  154.  
  155.     return n;
  156. }
  157.  
  158. double* inPutArrayOfDouble(string s, int countOfPoints, double* points, double min, double max)
  159. {
  160.     int counter = 0;
  161.     bool isInCorrect;
  162.     points = new double[countOfPoints*2];
  163.     for(int i = 0; i < countOfPoints*2 - 1; i+=2)
  164.     {
  165.         do {
  166.             cout << s << " x" << i + 1 - counter << " от " << min << " до " << max << " : ";
  167.             isInCorrect = false;
  168.             cin >> points[i];
  169.             if (cin.fail() || points[i] > max || points[i] < min)
  170.             {
  171.                 isInCorrect = true;
  172.                 cin.clear();
  173.                 while (cin.get() != '\n');
  174.                 cout << "Неверный ввод" << endl;
  175.             }
  176.         } while (isInCorrect);
  177.  
  178.         do {
  179.             cout << s << " y" << i + 1 - counter << " от " << min << " до " << max << " : ";
  180.             isInCorrect = false;
  181.             cin >> points[i+1];
  182.             if (cin.fail() || points[i+1] > max || points[i+1] < min)
  183.             {
  184.                 isInCorrect = true;
  185.                 cin.clear();
  186.                 while (cin.get() != '\n');
  187.                 cout << "Неверный ввод" << endl;
  188.             }
  189.         } while (isInCorrect);
  190.         counter++;
  191.     }
  192.  
  193.     return points;
  194. }
  195.  
  196. int readFileInt(string& pathToFile, int min, int max)
  197. {
  198.     int n;
  199.     bool isInCorrect;
  200.     ifstream file(pathToFile);
  201.     do {
  202.         isInCorrect = false;
  203.         file >> n;
  204.         if (file.fail() || n > max || n < min)
  205.         {
  206.             isInCorrect = true;
  207.             file.clear();
  208.             while (file.get() != '\n');
  209.             cout << "Неверный ввод" << endl;
  210.         }
  211.     } while (isInCorrect);
  212.  
  213.     file.close();
  214.     return n;
  215. }
  216.  
  217. double* readFileArrayOfDouble(string& pathToFile, int countOfPoints, double* points, double min, double max)
  218. {
  219.     int nullNumber;
  220.     bool isInCorrect;
  221.     ifstream file(pathToFile);
  222.     file >> nullNumber;
  223.     for(int i = 0; i < countOfPoints*2 - 1 ; i+=2)
  224.     {
  225.         do {
  226.             isInCorrect = false;
  227.             file >> points[i];
  228.             if (file.fail() || points[i] > max || points[i] < min)
  229.             {
  230.                 isInCorrect = true;
  231.                 file.clear();
  232.                 while (file.get() != '\n');
  233.                 cout << "Неверный ввод" << endl;
  234.             }
  235.         } while (isInCorrect);
  236.  
  237.         do {
  238.             isInCorrect = false;
  239.             file >> points[i+1];
  240.             if (file.fail() || points[i+1] > max || points[i+1] < min)
  241.             {
  242.                 isInCorrect = true;
  243.                 file.clear();
  244.                 while (file.get() != '\n');
  245.                 cout << "Неверный ввод" << endl;
  246.             }
  247.         } while (isInCorrect);
  248.     }
  249.     file.close();
  250.     return points;
  251. }
  252. int solveTheProblem(int countOfPoints, double* points)
  253. {
  254.     int maxCountOfTriangles;
  255.     double x1, x2, y1, y2;
  256.  
  257.     maxCountOfTriangles = countOfPoints*(countOfPoints-1)*(countOfPoints-2)/6;
  258.     for (int i=0; i<countOfPoints*2 - 2; i++)
  259.     {
  260.         for (int j = i + 2; j < countOfPoints*2 - 1; j++)
  261.         {
  262.             x1 = points[j] - points[i];
  263.             y1 = points[j+1] - points[i+1];
  264.             for (int k=j+1; k<countOfPoints; k++)
  265.             {
  266.                 x2 = points[k] - points[j];
  267.                 y2 = points[k+1] - points[j+1];
  268.                 if (x1 * y2 == x2 * y1)
  269.                 {
  270.                     maxCountOfTriangles--;
  271.                 }
  272.             }
  273.         }
  274.     }
  275.     return maxCountOfTriangles;
  276. }
  277. void writeSolveToFile(string& pathToFile, int n)
  278. {
  279.     ofstream file(pathToFile);
  280.     file << "Количество возможных треугольников: "<< n << endl;
  281.     file.close();
  282. }
  283. int chooseTheInput()
  284. {
  285.     cout << "Выберите ввод из консоли(1) или из файла(2): ";
  286.     return inPutInt(1, 2 );
  287. }
  288.  
  289. int chooseTheOutput()
  290. {
  291.     cout << "Выберите вывод в консоль(1) или в файл(2): ";
  292.     return inPutInt(1, 2);
  293. }
  294.  
  295. void inPut(int& countOfPoints, double*& points)
  296. {
  297.     int intChooseTheInput;
  298.     intChooseTheInput = chooseTheInput();
  299.     if(intChooseTheInput == 1)
  300.     {
  301.         countOfPoints = inPutIntWithText("Введите количество точек",1, 10);
  302.  
  303.         inPutArrayOfDouble("Введите координату координату", countOfPoints, points, -10.0, 10.0);
  304.     }
  305.     else if(intChooseTheInput == 2)
  306.     {
  307.         string pathToFile;
  308.         ifstream input;
  309.  
  310.         getFileNormalReading(pathToFile);
  311.  
  312.         countOfPoints = readFileInt(pathToFile, 1, 10);
  313.         readFileArrayOfDouble(pathToFile, countOfPoints, points, -10.0, 10.0);
  314.     }
  315. }
  316.  
  317. void outPut(int& countOfPoints, double*& points)
  318. {
  319.     int intChooseTheOutput;
  320.     intChooseTheOutput = chooseTheOutput();
  321.     if(intChooseTheOutput == 1)
  322.     {
  323.         cout << "Количество возможных треугольников: " << solveTheProblem(countOfPoints, points) << endl;
  324.     }
  325.     else if(intChooseTheOutput == 2)
  326.     {
  327.         string pathToFile;
  328.         ofstream output;
  329.  
  330.         getFileNormalWriting(pathToFile);
  331.         writeSolveToFile(pathToFile, solveTheProblem(countOfPoints, points));
  332.     }
  333.  
  334. }
  335.  
  336.  
  337. int main()
  338. {
  339.     int countOfPoints;
  340.     double* points;
  341.  
  342.     printTask();
  343.  
  344.     inPut(countOfPoints, points);
  345.  
  346.     solveTheProblem(countOfPoints, points);
  347.  
  348.     outPut(countOfPoints, points);
  349.  
  350.     return 0;
  351. }
Advertisement
Add Comment
Please, Sign In to add comment