Advertisement
fatalryuu

laba

Sep 27th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. const int MIN_VALUE_FOR_SIZE = 1;
  9. const int MAX_VALUE_FOR_SIZE = 10;
  10.  
  11. string inputPathToFile() {
  12.     bool isIncorrect;
  13.     string path;
  14.     do {
  15.         isIncorrect = false;
  16.         cin >> path;
  17.         ifstream file(path);
  18.         if (!file.is_open()) {
  19.             cout << "Файл не найден! Введите правильную ссылку: ";
  20.             isIncorrect = true;
  21.         }
  22.     } while (isIncorrect);
  23.     return path;
  24. }
  25.  
  26. int readSizeFromConsole() {
  27.     int size;
  28.     string sizeInput;
  29.     bool isInCorrect;
  30.     cout << "В файле введен некорректный размер матрицы! \nВведите в консоль целое число от " << MIN_VALUE_FOR_SIZE << " до " << MAX_VALUE_FOR_SIZE << ": \n";  
  31.     do {
  32.         isInCorrect = false;
  33.         cin >> size;
  34.         if (cin.fail()) {
  35.             isInCorrect = true;
  36.         }
  37.         cin.clear();
  38.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  39.         if (isInCorrect) {
  40.             cout << "Ошибка! Введите целое число: ";
  41.         }
  42.         if (!isInCorrect && (size < MIN_VALUE_FOR_SIZE || size > MAX_VALUE_FOR_SIZE)) {
  43.             cout << "Ошибка! Пожалуйста, введите число из указанного диапазона! \n";
  44.             isInCorrect = true;
  45.         }
  46.     } while (isInCorrect);
  47.     return size;
  48. }
  49.  
  50. int readSizeFromFile(string path) {
  51.     int size;
  52.     bool isCorrect = true;
  53.     string sizeInput;
  54.     ifstream fin(path);
  55.     fin >> sizeInput;
  56.     cout << '\n' << "Чтение размеров матрицы...\n" << '\n';
  57.     try {
  58.         size = atoi(sizeInput.c_str());
  59.     }
  60.     catch (...) {
  61.         isCorrect = false;
  62.         size = readSizeFromConsole();
  63.     }
  64.     if (isCorrect && (size > MAX_VALUE_FOR_SIZE || size < MIN_VALUE_FOR_SIZE)) {
  65.         size = readSizeFromConsole();
  66.     }
  67.     return size;
  68.     fin.close();
  69. }
  70.  
  71. int** creatingOfMatrix(int size) {
  72.     int** matrix = new int* [size];
  73.     for (int i = 0; i < size; i++)
  74.         matrix[i] = new int[size];
  75.     return matrix;
  76. }
  77.  
  78. void initializationOfMatrix(int size, int** matrix) {
  79.     int k = 1;
  80.     for (int i = 0; i < size; i++) {
  81.         if (i == 0 || i % 2 == 0) {
  82.             for (int j = 0; j < size; j++) {
  83.                 matrix[i][j] = k;
  84.                 k++;
  85.             }
  86.         }
  87.         if (i != 0 && i % 2 != 0) {
  88.             for (int j = size - 1; j > -1; j--) {
  89.                 matrix[i][j] = k;
  90.                 k++;
  91.             }
  92.  
  93.         }
  94.     }
  95. }
  96.  
  97. void outputToFile(int size, int** matrix, string path) {
  98.     ofstream fout(path);
  99.     cout << '\n' << "Происходит запись матрицы в файл... \n" << '\n';
  100.     for (int i = 0; i < size; i++) {
  101.         for (int j = 0; j < size; j++) {
  102.             fout << matrix[i][j] << '\t';
  103.         }
  104.         fout << endl;
  105.     }
  106.     fout.close();
  107.     cout << "Данные успешно записаны в файл!" << '\n';
  108. }
  109.  
  110. int main() {
  111.     setlocale(LC_ALL, "RUSSIAN");
  112.     int** matrix;
  113.     const int maxValueForSize = 10;
  114.    
  115.     cout << "Эта программа строит матрицу порядка n, записав ее числа от 1 до n^2 согласно схеме. \n";
  116.     cout << "Введите ссылку на файл для считывания размера матрицы: ";
  117.  
  118.     string inputFile = inputPathToFile();
  119.     int size = readSizeFromFile(inputFile);
  120.     matrix = creatingOfMatrix(size);
  121.     initializationOfMatrix(size, matrix);
  122.     cout << "Введите ссылку на файл для записи получившейся матрицы : ";
  123.     string outputFile = inputPathToFile();
  124.     outputToFile(size, matrix, outputFile);
  125.    
  126.     delete[] matrix;
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement