Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- void printTask()
- {
- cout << "Эта программа находит количество всех возиожных треугольников по заданным точкам\n\n";
- }
- string readPathFile()
- {
- string pathToFile;
- bool isInCorrect;
- do
- {
- isInCorrect = false;
- cout << "Введите путь к файлу с расширением.txt с количеством точек и координатами точек: ";
- cin >> pathToFile;
- if (pathToFile.size() < 5 || pathToFile[pathToFile.length() - 4] != '.' || pathToFile[pathToFile.length() - 3] != 't' || pathToFile[pathToFile.length() - 2] != 'x' || pathToFile[pathToFile.length() - 1] != 't')
- {
- cout << "Расширение файла не .txt!\n";
- isInCorrect = true;
- }
- } while (isInCorrect);
- return pathToFile;
- }
- bool isNotExists(string& pathToFile)
- {
- bool isRight;
- isRight = true;
- ifstream file(pathToFile);
- if (file.good())
- isRight = false;
- file.close();
- return isRight;
- }
- bool isNotAbleToReading(string& pathToFile)
- {
- bool isRight;
- isRight = true;
- ifstream file(pathToFile);
- if (file.is_open())
- isRight = false;
- file.close();
- return isRight;
- }
- bool isNotAbleToWriting(string& pathToFile)
- {
- bool isRight;
- isRight = true;
- ofstream file(pathToFile, ios::app);
- if (file.is_open())
- isRight = false;
- file.close();
- return isRight;
- }
- bool isEmpty(string& pathToFile)
- {
- bool isRight;
- isRight = false;
- ifstream file(pathToFile);
- if (file.peek() == ifstream::traits_type::eof())
- isRight = true;
- file.close();
- return isRight;
- }
- void getFileNormalReading(string& pathToFile)
- {
- bool isInCorrect;
- do
- {
- isInCorrect = false;
- pathToFile = readPathFile();
- if (isNotExists(pathToFile))
- {
- isInCorrect = true;
- cout << "Проверьте корректность ввода пути к файлу!\n";
- }
- if (isInCorrect && isNotAbleToReading(pathToFile))
- {
- isInCorrect = true;
- cout << "Файл закрыт для чтения!\n";
- }
- if (isInCorrect && isEmpty(pathToFile))
- {
- isInCorrect = true;
- cout << "Файл пуст!\n";
- }
- } while (isInCorrect);
- }
- void getFileNormalWriting(string& pathToFile)
- {
- bool isInCorrect;
- do
- {
- isInCorrect = false;
- pathToFile = readPathFile();
- if (isNotExists(pathToFile))
- {
- isInCorrect = true;
- cout << "Проверьте корректность ввода пути к файлу!\n";
- }
- if (isInCorrect && isNotAbleToWriting(pathToFile))
- {
- isInCorrect = true;
- cout << "Файл закрыт для записи!\n";
- }
- } while (isInCorrect);
- }
- int inPutIntWithText(string s, double min, double max) {
- int n;
- bool isInCorrect;
- do {
- cout << s << " от " << min << " до " << max << " : ";
- isInCorrect = false;
- cin >> n;
- if (cin.fail() || n > max || n < min)
- {
- isInCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- cout << "Неверный ввод" << endl;
- }
- } while (isInCorrect);
- return n;
- }
- int inPutInt(int min, int max) {
- int n;
- bool isInCorrect;
- n = 0;
- isInCorrect = true;
- do {
- isInCorrect = false;
- cin >> n;
- if (cin.fail() || n < min || n > max)
- {
- isInCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- cout << "Неверный ввод" << endl;
- }
- } while (isInCorrect);
- return n;
- }
- double* inPutArrayOfDouble(string s, int countOfPoints, double* points, double min, double max)
- {
- int counter = 0;
- bool isInCorrect;
- points = new double[countOfPoints*2];
- for(int i = 0; i < countOfPoints*2 - 1; i+=2)
- {
- do {
- cout << s << " x" << i + 1 - counter << " от " << min << " до " << max << " : ";
- isInCorrect = false;
- cin >> points[i];
- if (cin.fail() || points[i] > max || points[i] < min)
- {
- isInCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- cout << "Неверный ввод" << endl;
- }
- } while (isInCorrect);
- do {
- cout << s << " y" << i + 1 - counter << " от " << min << " до " << max << " : ";
- isInCorrect = false;
- cin >> points[i+1];
- if (cin.fail() || points[i+1] > max || points[i+1] < min)
- {
- isInCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- cout << "Неверный ввод" << endl;
- }
- } while (isInCorrect);
- counter++;
- }
- return points;
- }
- int readFileInt(string& pathToFile, int min, int max)
- {
- int n;
- bool isInCorrect;
- ifstream file(pathToFile);
- do {
- isInCorrect = false;
- file >> n;
- if (file.fail() || n > max || n < min)
- {
- isInCorrect = true;
- file.clear();
- while (file.get() != '\n');
- cout << "Неверный ввод" << endl;
- }
- } while (isInCorrect);
- file.close();
- return n;
- }
- double* readFileArrayOfDouble(string& pathToFile, int countOfPoints, double* points, double min, double max)
- {
- int nullNumber;
- bool isInCorrect;
- ifstream file(pathToFile);
- file >> nullNumber;
- for(int i = 0; i < countOfPoints*2 - 1 ; i+=2)
- {
- do {
- isInCorrect = false;
- file >> points[i];
- if (file.fail() || points[i] > max || points[i] < min)
- {
- isInCorrect = true;
- file.clear();
- while (file.get() != '\n');
- cout << "Неверный ввод" << endl;
- }
- } while (isInCorrect);
- do {
- isInCorrect = false;
- file >> points[i+1];
- if (file.fail() || points[i+1] > max || points[i+1] < min)
- {
- isInCorrect = true;
- file.clear();
- while (file.get() != '\n');
- cout << "Неверный ввод" << endl;
- }
- } while (isInCorrect);
- }
- file.close();
- return points;
- }
- int solveTheProblem(int countOfPoints, double* points)
- {
- int maxCountOfTriangles;
- double x1, x2, y1, y2;
- maxCountOfTriangles = countOfPoints*(countOfPoints-1)*(countOfPoints-2)/6;
- for (int i=0; i<countOfPoints*2 - 2; i++)
- {
- for (int j = i + 2; j < countOfPoints*2 - 1; j++)
- {
- x1 = points[j] - points[i];
- y1 = points[j+1] - points[i+1];
- for (int k=j+1; k<countOfPoints; k++)
- {
- x2 = points[k] - points[j];
- y2 = points[k+1] - points[j+1];
- if (x1 * y2 == x2 * y1)
- {
- maxCountOfTriangles--;
- }
- }
- }
- }
- return maxCountOfTriangles;
- }
- void writeSolveToFile(string& pathToFile, int n)
- {
- ofstream file(pathToFile);
- file << "Количество возможных треугольников: "<< n << endl;
- file.close();
- }
- int chooseTheInput()
- {
- cout << "Выберите ввод из консоли(1) или из файла(2): ";
- return inPutInt(1, 2 );
- }
- int chooseTheOutput()
- {
- cout << "Выберите вывод в консоль(1) или в файл(2): ";
- return inPutInt(1, 2);
- }
- void inPut(int& countOfPoints, double*& points)
- {
- int intChooseTheInput;
- intChooseTheInput = chooseTheInput();
- if(intChooseTheInput == 1)
- {
- countOfPoints = inPutIntWithText("Введите количество точек",1, 10);
- inPutArrayOfDouble("Введите координату координату", countOfPoints, points, -10.0, 10.0);
- }
- else if(intChooseTheInput == 2)
- {
- string pathToFile;
- ifstream input;
- getFileNormalReading(pathToFile);
- countOfPoints = readFileInt(pathToFile, 1, 10);
- readFileArrayOfDouble(pathToFile, countOfPoints, points, -10.0, 10.0);
- }
- }
- void outPut(int& countOfPoints, double*& points)
- {
- int intChooseTheOutput;
- intChooseTheOutput = chooseTheOutput();
- if(intChooseTheOutput == 1)
- {
- cout << "Количество возможных треугольников: " << solveTheProblem(countOfPoints, points) << endl;
- }
- else if(intChooseTheOutput == 2)
- {
- string pathToFile;
- ofstream output;
- getFileNormalWriting(pathToFile);
- writeSolveToFile(pathToFile, solveTheProblem(countOfPoints, points));
- }
- }
- int main()
- {
- int countOfPoints;
- double* points;
- printTask();
- inPut(countOfPoints, points);
- solveTheProblem(countOfPoints, points);
- outPut(countOfPoints, points);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment