Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <filesystem>
- #include <sstream>
- using namespace std;
- int getArraySize()
- {
- int order;
- bool IsNotCorrect;
- string inputLine;
- do
- {
- cout << "Введите размер массива: " << endl;
- IsNotCorrect = false;
- getline(cin, inputLine);
- try
- {
- order = stoi(inputLine);
- }
- catch (...)
- {
- IsNotCorrect = true;
- cout << "Порядок матрицы должен быть положительным числом." << endl;
- }
- if (!IsNotCorrect && (((order < 2) || (order > 10000))))
- {
- cout << "Порядок матрицы должен принадлежать промежутку от 1 до 10000" << endl;
- IsNotCorrect = true;
- }
- } while (IsNotCorrect);
- return order;
- }
- int getArrayElement(int prev)
- {
- int element;
- bool IsNotCorrect;
- string inputLine;
- do
- {
- cout << "Введите элемент массива: " << endl;
- IsNotCorrect = false;
- getline(cin, inputLine);
- try
- {
- element = stoi(inputLine);
- }
- catch (...)
- {
- IsNotCorrect = true;
- cout << "Элемент массива должен быть числом." << endl;
- }
- if (!IsNotCorrect && (((element < prev) || (element > 2147000000))))
- {
- cout << "Элемент массива должен быть больше " << prev << " и меньше 2147000000" << endl;
- IsNotCorrect = true;
- }
- } while (IsNotCorrect);
- return element;
- }
- int* сreateArray(int size)
- {
- int* arr = new int[size];
- arr[0] = getArrayElement(-2147000000);
- for (int i = 1; i < size; i++)
- arr[i] = getArrayElement(arr[i-1]);
- return arr;
- }
- void printArray(int*& arr, int size)
- {
- cout << endl;
- for (int i = 0; i < size; i++) {
- cout << arr[i] << "\t";
- }
- cout << endl;
- }
- void сreateArraysFromFile(int*& a, int& m, int*& b, int& n, string path)
- {
- fstream file(path, fstream::in);
- string inputLine;
- stringstream strstream;
- getline(file, inputLine);
- m = stoi(inputLine);
- a = new int[m];
- getline(file, inputLine);
- strstream << inputLine;
- for (int i = 0; i < m; i++)
- {
- strstream >> a[i];
- }
- getline(file, inputLine);
- n = stoi(inputLine);
- b = new int[n];
- getline(file, inputLine);
- strstream << inputLine;
- for (int i = 0; i < n; i++)
- {
- strstream >> b[i];
- }
- }
- bool isFileCorrect(string path)
- {
- string inputline;
- stringstream strstream;
- int iSize, num, size;
- bool isCorrect = true;
- iSize = 0;
- fstream file(path, std::ios_base::in);
- try
- {
- getline(file, inputline);
- size = stoi(inputline);
- getline(file, inputline);
- strstream << inputline;
- while (!strstream.eof() && iSize < size)
- {
- strstream >> num;
- iSize++;
- }
- if (iSize != size)
- isCorrect = false;
- iSize = 0;
- getline(file, inputline);
- size = stoi(inputline);
- getline(file, inputline);
- strstream << inputline;
- while (!strstream.eof() && iSize < size && isCorrect)
- {
- strstream >> num;
- iSize++;
- }
- if (iSize != size)
- isCorrect = false;
- }
- catch (...)
- {
- isCorrect = false;
- }
- return isCorrect;
- }
- string getTaskFilePath()
- {
- string path;
- bool isIncorrect = true;
- do
- {
- cout << "Введите абсолютный путь к файлу: " << endl;
- cin >> path;
- if (!filesystem::exists(path))
- {
- cout << "Файл не найден. Проверьте введённый путь." << endl;
- }
- else
- {
- if (isFileCorrect(path))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Проверьте данные в файле. Обратите внимание на размеры массивов и их содержимое." << endl;
- }
- }
- } while (isIncorrect);
- return path;
- }
- string getOutputDirectory()
- {
- string patho;
- bool isIncorrect;
- isIncorrect = true;
- do
- {
- cout << "Введите директорию, в которую хотите сохранить результат:" << endl;
- cin >> patho;
- if (filesystem::exists(patho))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
- }
- } while (isIncorrect);
- return patho;
- }
- void printResultToFile(int*& a, int m, int*& b, int n, int*& c, int k)
- {
- string path;
- path = getOutputDirectory();
- ofstream fout;
- fout.open(path + "\output.txt");
- fout << "Для массивов" << endl;
- for (int i = 0; i < m; i++)
- {
- fout << setw(6) << a[i];
- }
- fout << endl;
- for (int i = 0; i < n; i++)
- {
- fout << setw(6) << b[i];
- }
- fout << "Упорядоченный массив без повторений:" << endl;
- for (int i = 0; i < k; i++)
- {
- fout << setw(6) << c[i];
- }
- fout.close();
- cout << "Ответ сохранен по указанному пути.";
- }
- int* calculateArrayByCondition(int*& a, int m, int*& b, int n, int d)
- {
- d = 0;
- int i = 0;
- int j = 0;
- int min = 0;
- int* c = new int[m + n];
- while ((i < n) && (j < m))
- {
- if (a[i] < b[j])
- {
- c[d++] = a[i++];
- }
- else if (a[i] > b[j])
- {
- c[d++] = b[j++];
- }
- else
- {
- c[d++] = a[i++];
- j++;
- }
- }
- while (i < n)
- c[d++] = a[i++];
- while (j < m)
- c[d++] = b[j++];
- bool isContinue = true;
- for (i = 0; i < d; i++)
- {
- for (j = 0; j < d - i; j++)
- {
- if (j + 1 == d)
- isContinue = false;
- if ((c[j] > c[j + 1]) && (isContinue))
- {
- min = c[j];
- c[j] = c[j + 1];
- c[j + 1] = min;
- }
- }
- }
- return c;
- }
- void workWithFile()
- {
- int m,n,k = 0;
- int* a;
- int* b;
- сreateArraysFromFile(a, m, b, n, getTaskFilePath());
- printArray(a, m);
- printArray(b, n);
- int* c = calculateArrayByCondition(a, m, b, n, k);
- cout << "Новый массив:" << endl;
- printArray(c, k);
- printResultToFile(a, m, b, n, c, k);
- delete a, b, c;
- }
- void workWithConsole()
- {
- int m, n, k = 0;
- int* a;
- int* b;
- m = getArraySize();
- a = сreateArray(m);
- n = getArraySize();
- b = сreateArray(n);
- printArray(a, m);
- printArray(b, n);
- int* c = calculateArrayByCondition(a, m, b, n, k);
- cout << "Новый массив:" << endl;
- printArray(c, k);
- printResultToFile(a, m, b, n, c, k);
- delete a, b, c;
- }
- void setProgramWorkType()
- {
- int answer;
- bool isNotCorrect;
- string inputLine;
- do
- {
- cout << "1 - работать с консолью, 2 - с файлом: " << endl;
- isNotCorrect = false;
- getline(cin, inputLine);
- try
- {
- answer = stoi(inputLine);
- }
- catch (...)
- {
- isNotCorrect = true;
- cout << "Введите число!" << endl;
- }
- if (!isNotCorrect && (((answer < 1) || (answer > 2))))
- {
- cout << "Выбирете один из предложенных вариантов!" << endl;
- isNotCorrect = true;
- }
- } while (isNotCorrect);
- if (answer == 1)
- workWithConsole();
- else
- workWithFile();
- }
- int main()
- {
- setlocale(LC_ALL, "ru");
- setProgramWorkType();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement