Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- const int MIN_VALUE_FOR_SIZE = 1;
- const int MAX_VALUE_FOR_SIZE = 10;
- string inputPathToFile() {
- bool isIncorrect;
- string path;
- do {
- isIncorrect = false;
- cin >> path;
- ifstream file(path);
- if (!file.is_open()) {
- cout << "Файл не найден! Введите правильную ссылку: ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- int readSizeFromConsole() {
- int size;
- string sizeInput;
- bool isInCorrect;
- cout << "В файле введен некорректный размер матрицы! \nВведите в консоль целое число от " << MIN_VALUE_FOR_SIZE << " до " << MAX_VALUE_FOR_SIZE << ": \n";
- do {
- isInCorrect = false;
- cin >> size;
- if (cin.fail()) {
- isInCorrect = true;
- }
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- if (isInCorrect) {
- cout << "Ошибка! Введите целое число: ";
- }
- if (!isInCorrect && (size < MIN_VALUE_FOR_SIZE || size > MAX_VALUE_FOR_SIZE)) {
- cout << "Ошибка! Пожалуйста, введите число из указанного диапазона! \n";
- isInCorrect = true;
- }
- } while (isInCorrect);
- return size;
- }
- int readSizeFromFile(string path) {
- int size;
- bool isCorrect = true;
- string sizeInput;
- ifstream fin(path);
- fin >> sizeInput;
- cout << '\n' << "Чтение размеров матрицы...\n" << '\n';
- try {
- size = atoi(sizeInput.c_str());
- }
- catch (...) {
- isCorrect = false;
- size = readSizeFromConsole();
- }
- if (isCorrect && (size > MAX_VALUE_FOR_SIZE || size < MIN_VALUE_FOR_SIZE)) {
- size = readSizeFromConsole();
- }
- return size;
- fin.close();
- }
- int** creatingOfMatrix(int size) {
- int** matrix = new int* [size];
- for (int i = 0; i < size; i++)
- matrix[i] = new int[size];
- return matrix;
- }
- void initializationOfMatrix(int size, int** matrix) {
- int k = 1;
- for (int i = 0; i < size; i++) {
- if (i == 0 || i % 2 == 0) {
- for (int j = 0; j < size; j++) {
- matrix[i][j] = k;
- k++;
- }
- }
- if (i != 0 && i % 2 != 0) {
- for (int j = size - 1; j > -1; j--) {
- matrix[i][j] = k;
- k++;
- }
- }
- }
- }
- void outputToFile(int size, int** matrix, string path) {
- ofstream fout(path);
- cout << '\n' << "Происходит запись матрицы в файл... \n" << '\n';
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- fout << matrix[i][j] << '\t';
- }
- fout << endl;
- }
- fout.close();
- cout << "Данные успешно записаны в файл!" << '\n';
- }
- int main() {
- setlocale(LC_ALL, "RUSSIAN");
- int** matrix;
- const int maxValueForSize = 10;
- cout << "Эта программа строит матрицу порядка n, записав ее числа от 1 до n^2 согласно схеме. \n";
- cout << "Введите ссылку на файл для считывания размера матрицы: ";
- string inputFile = inputPathToFile();
- int size = readSizeFromFile(inputFile);
- matrix = creatingOfMatrix(size);
- initializationOfMatrix(size, matrix);
- cout << "Введите ссылку на файл для записи получившейся матрицы : ";
- string outputFile = inputPathToFile();
- outputToFile(size, matrix, outputFile);
- delete[] matrix;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement