Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- void printTask()
- {
- cout << "Эта программа объединяет две последовательности чисел" << endl;
- }
- 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 = 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 << "Проверьте корректность ввода пути к файлу!" << endl;
- }
- if (isInCorrect || isNotAbleToReading(pathToFile))
- {
- isInCorrect = true;
- cout << "Файл закрыт для чтения!" << endl;
- }
- if (isInCorrect || isEmpty(pathToFile))
- {
- isInCorrect = true;
- cout << "Файл пуст!" << endl;
- }
- } while (isInCorrect);
- }
- void getFileNormalWriting(string& pathToFile)
- {
- bool isInCorrect;
- do
- {
- isInCorrect = false;
- pathToFile = readPathFile();
- if (isNotExists(pathToFile))
- {
- isInCorrect = true;
- cout << "Проверьте корректность ввода пути к файлу!" << endl;
- }
- if (isInCorrect && isNotAbleToWriting(pathToFile))
- {
- isInCorrect = true;
- cout << "Файл закрыт для записи!" << endl;
- }
- } while (isInCorrect);
- }
- 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;
- }
- 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* inputIntArray(string s,int min, int max, int size)
- {
- int* array;
- bool isInCorrect;
- array = new int[size];
- for(int i = 0; i < size; i++)
- {
- do {
- cout << s << " от " << min << " до " << max << ": ";
- isInCorrect = false;
- cin >> array[i];
- if (cin.fail() || array[i] < min || array[i] > max)
- {
- isInCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- cout << "Неверный ввод" << endl;
- }
- } while (isInCorrect);
- }
- return array;
- }
- void readFileSizeOfArrays(string& pathToFile, int& size1, int& size2, int min, int max, bool& isEndOfFile)
- {
- string nullNum;
- ifstream file(pathToFile);
- file >> size1;
- if (file.fail() || size1 > max || size1 < min)
- {
- file.clear();
- while (file.get() != '\n');
- size1 = inputIntWithText("Неверный ввод размера первой числовой последовательности, введите с консоли", min, max);
- }
- if(file.eof())
- {
- if(isEndOfFile)
- {
- isEndOfFile = true;
- cout << "В файле не хватает чисел для числовой последовательности";
- }
- }
- else
- {
- for(int i = 0; i < size1; i++)
- {
- if(file.eof())
- {
- if(!isEndOfFile)
- {
- isEndOfFile = true;
- cout << "В файле не хватает чисел для числовой последовательности";
- }
- }
- else
- {
- file >> nullNum;
- }
- }
- if(file.eof())
- {
- if(!isEndOfFile)
- {
- isEndOfFile = true;
- cout << "В файле не хватает чисел для числовой последовательности";
- }
- }
- else
- {
- file >> size2;
- if (file.fail() || size2 > max || size2 < min)
- {
- file.clear();
- while (file.get() != '\n');
- cout << "Неверный ввод размера второй числовой последовательности, введите с консоли: ";
- size2 = inputInt(min, max);
- }
- }
- }
- file.close();
- }
- void readFileIntArray(string& pathToFile, int min, int max, int*& array1, int*& array2, int size1, int size2, bool& isEndOfFile)
- {
- string nullElement;
- ifstream file(pathToFile);
- array1 = new int[size1];
- array2 = new int[size2];
- file >> nullElement;
- for(int i = 0; i < size1; i++)
- {
- file >> array1[i];
- if (file.fail() || array1[i] > max || array1[i] < min)
- {
- file.clear();
- while (file.get() != '\n');
- cout << "Неверный ввод в первой числовой последовательности, введите с консоли: ";
- array1[i] = inputInt(min, max);
- }
- }
- file >> nullElement;
- for(int j = 0; j < size2; j++)
- {
- if(file.eof())
- {
- if(!isEndOfFile)
- {
- cout << "В файле не хватает чисел для числовой последовательности";
- isEndOfFile = true;
- }
- }
- else
- {
- file >> array2[j];
- if (file.fail() || array2[j] > max || array2[j] < min)
- {
- file.clear();
- while (file.get() != '\n');
- cout << "Неверный ввод в первой числовой последовательности, введите с консоли: ";
- array2[j] = inputInt(min, max);
- }
- }
- }
- \
- }
- void sortArray(int*& array, int size)
- {
- for(int i = 0; i < size; i++)
- {
- for(int j = 0; j < size - i - 1; j++)
- {
- if(array[j] > array[j + 1])
- {
- array[j] = array[j] + array[j + 1];
- array[j + 1] = array[j] - array[j + 1];
- array[j] = array[j] - array[j + 1];
- }
- }
- }
- }
- int* removeZeros(int* result, int& size, bool isOneZero)
- {
- int newSize = 0;
- int* newArray;
- for (int i = 0; i < size; i++)
- {
- if (result[i] != 0)
- {
- newSize++;
- }
- }
- if(isOneZero)
- {
- newSize +=1;
- }
- if (newSize == 0)
- {
- newArray = new int[1]{0};
- size = 1;
- }
- else
- {
- newArray = new int[newSize];
- int index = 0;
- for (int i = 0; i < size; i++)
- {
- if(result[i] != 0)
- {
- newArray[index++] = result[i];
- }
- else
- {
- if(result[i] == 0 && isOneZero)
- {
- newArray[index++] = 0;
- isOneZero = false;
- }
- }
- }
- size = newSize;
- }
- return newArray;
- }
- void unificationArray (int* array1, int size1, int* array2, int size2, int*& resultArray, int& resultSize, bool isEndOfFile)
- {
- if(!isEndOfFile)
- {
- bool isUnic;
- bool isFirst;
- isFirst = false;
- resultSize = size1 + size2;
- sortArray(array1, size1);
- sortArray(array2, size2);
- resultArray = new int[resultSize];
- for(int i = 0; i < size1; i++)
- {
- resultArray[i] = array1[i];
- }
- for(int i = 0; i < size2; i++)
- {
- isUnic = true;
- if(!isFirst && array2[i] == 0)
- {
- isFirst = true;
- }
- for(int j = 0; j < size1; j++)
- {
- if(!isFirst && array1[i] == 0)
- {
- isFirst = true;
- }
- if(array1[j] == array2[i])
- {
- isUnic = false;
- }
- }
- if(isUnic)
- {
- resultArray[size1 + i] = array2[i];
- }
- }
- resultArray = removeZeros(resultArray, resultSize, isFirst);
- sortArray(resultArray, resultSize);
- }
- }
- void writeSolveToFile(string& pathToFile, int* array, int size)
- {
- ofstream file(pathToFile);
- file << "Совмещенная последовательность: ";
- for(int i = 0; i < size; i++)
- file << array[i] << " ";
- file.close();
- }
- int chooseTheInput()
- {
- cout << "Выберите ввод из консоли(1) или из файла(2): ";
- return inputInt(1, 2 );
- }
- int chooseTheOutput()
- {
- cout << "Выберите вывод в консоль(1) или в файл(2): ";
- return inputInt(1, 2);
- }
- void input(int& size1, int& size2, int*& array1, int*& array2, bool& isEndOfFile)
- {
- int intChooseTheInput;
- intChooseTheInput = chooseTheInput();
- if(intChooseTheInput == 1)
- {
- size1 = inputIntWithText("Введите количество элементов числовой последовательности",1, 100);
- array1 = inputIntArray("Введите члены числовой последовательности", -10000, 10000, size1);
- size2 = inputIntWithText("Введите количество элементов числовой последовательности", 1, 100);
- array2 = inputIntArray("Введите члены числовой последовательности", -10000, 10000, size2);
- }
- else if(intChooseTheInput == 2)
- {
- isEndOfFile = false;
- string pathToFile;
- getFileNormalReading(pathToFile);
- readFileSizeOfArrays(pathToFile,size1, size2, 1, 100, isEndOfFile);
- readFileIntArray(pathToFile, -10000, 10000, array1, array2, size1, size2, isEndOfFile);
- }
- }
- void output(int size, int* array, bool isEndOfFile)
- {
- if(!isEndOfFile)
- {
- int intChooseTheOutput;
- intChooseTheOutput = chooseTheOutput();
- if(intChooseTheOutput == 1)
- {
- cout<< "Полученная числова последовательность: ";
- for(int i = 0; i < size; i++)
- cout << array[i] << " ";
- }
- else if(intChooseTheOutput == 2)
- {
- string pathToFile;
- getFileNormalWriting(pathToFile);
- cout<< "Полученная числова последовательноть записана в файл равна: ";
- for(int i = 0; i < size; i++)
- cout << array[i] << " ";
- writeSolveToFile(pathToFile, array, size);
- cout << endl;
- }
- }
- }
- int main()
- {
- int size1, size2, size3;
- int *array1, *array2, *array3;
- bool isEndOfFile;
- size1 = 0;
- size2 = 0;
- size3 = 0;
- array1 = new int[1];
- array2 = new int[1];
- array3 = new int[1];
- isEndOfFile = false;
- printTask();
- input(size1, size2, array1, array2, isEndOfFile);
- unificationArray(array1, size1, array2, size2,array3,size3, isEndOfFile);
- output(size3, array3, isEndOfFile);
- delete[] array1;
- delete[] array2;
- delete[] array3;
- }
Advertisement
Add Comment
Please, Sign In to add comment