Advertisement
oshige_san

Untitled

Apr 29th, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <conio.h>
  5. #include <string>
  6. #include <sstream>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     setlocale(LC_ALL, "ru");
  14.  
  15.     do {
  16.         fstream f1;
  17.         string fname;
  18.         cout << "Введите имя файла с массивом или  \"*\", чтобы выйти из программы: ";
  19.         getline(cin, fname);
  20.         if (fname == "*")
  21.             return 0;
  22.  
  23.         f1.open(fname);
  24.         if (!f1) {
  25.             cout << "Не удалось открыть файл " << "\"" << fname << "\"!" << endl;
  26.             continue;
  27.         }
  28.  
  29.         bool er = false, not_rect = false;
  30.         int cols = 0, rows = 0, other_rows = 0, t;
  31.         streampos p;
  32.  
  33.         do {
  34.             p = f1.tellg();
  35.             char c = f1.get();
  36.             switch (c)
  37.             {
  38.             case ' ': case '\t':
  39.                 continue;
  40.  
  41.             case EOF:
  42.                 if (other_rows == 0) break;
  43.  
  44.             case '\n':
  45.                 if (other_rows == 0)
  46.                     continue;
  47.                 if (cols == 0)
  48.                     cols = other_rows;
  49.                 else if (cols != other_rows) {
  50.                     not_rect = true;
  51.                     break;
  52.                 }
  53.                 rows++;
  54.                 other_rows = 0;
  55.                 continue;
  56.  
  57.             default:
  58.                 f1.unget();
  59.                 f1 >> t;
  60.                 if (f1.fail() || ((c = f1.peek()) != ' ' && c != '\t' && c != '\n' && c != EOF)) {
  61.                     er = true;
  62.                     break;
  63.                 }
  64.                 other_rows++;
  65.                 continue;
  66.             }  
  67.             break;
  68.         } while (true);
  69.  
  70.         if (cols == 0) {
  71.             f1.close();
  72.             cout << "Файл " << "\"" << fname << "\"" <<  " пуст!" << endl;
  73.             continue;
  74.         }
  75.  
  76.  
  77.         if (not_rect) {
  78.             f1.close();
  79.             cout << "Матрица не является прямоугольной, начиная со строки " << rows + 1 <<"!\n";
  80.             continue;
  81.         }
  82.  
  83.         if (er) {
  84.             f1.clear();
  85.             f1.seekg(p);
  86.             string el;
  87.             f1 >> el;
  88.             f1.close();
  89.             cout << "Неверное значение элемента матрицы в позиции [" << rows + 1 << "; " << cols + 1 << "]!" << endl;
  90.             cout << "Последовательность некорректных символов: " << el << endl;
  91.             continue;
  92.         }
  93.  
  94.         int** A = NULL; int i;
  95.         try {
  96.             A = new int*[rows];
  97.             for (i = 0; i < rows; i++)
  98.                 A[i] = new int[cols];
  99.         }
  100.         catch (...) {
  101.             for (int k = 0; k < i; k++)
  102.                 delete[]A[k];
  103.             delete[]A;
  104.             f1.close();
  105.             if (!A)
  106.                 cout << "Ошибка выделения памяти!";
  107.             continue;
  108.         }
  109.  
  110.         f1.clear();
  111.         f1.seekg(0);
  112.  
  113.         cout << "Ниже приведена начальная матрица.\n";
  114.  
  115.         for (int i = 0; i < rows; i++) {
  116.             cout << '[' << i + 1 << "]: " << setw(5);
  117.             for (int j = 0; j < cols; j++) {
  118.                 f1 >> *(*(A + i) + j);
  119.                 cout << setw(5) << A[i][j];
  120.             }
  121.             cout << endl;
  122.         }
  123.         f1.close();
  124.  
  125.         int* sumA = new (nothrow) int[cols];
  126.         if (!sumA) {
  127.             cout << "Ошибка выделения памяти!" << endl;
  128.             continue;
  129.         }
  130.  
  131.         for (int i = 0; i < cols; i++)
  132.             sumA[i] = A[0][i];
  133.  
  134.         if (rows > 1)
  135.             for (int i = 1; i < rows; i++)
  136.                 for (int j = 0; j < cols; j++)
  137.                     sumA[j] += A[i][j];
  138.  
  139.  
  140.         int min = sumA[0];
  141.         for (int i = 1; i < cols; i++)
  142.             if (sumA[i] < min)
  143.                 min = sumA[i];
  144.  
  145.  
  146.         delete[] sumA;
  147.         cout << "Минимальное значение сумм столбцов = " << min << ".\n";
  148.         cout << "Ниже приведена преобразованная матрица.\n";
  149.  
  150.         for (int i = 0; i < rows; i++) {
  151.             cout << '[' << i + 1 << "]: " << setw(5);
  152.             for (int j = 0; j < cols; j++) {
  153.                 A[i][j] -= min;
  154.                 cout << setw(5) << A[i][j];
  155.             }
  156.             cout << endl;
  157.         }
  158.         f1.close();
  159.  
  160.         for (int i = 0; i < rows; i++)
  161.             delete[]A[i];
  162.         delete[]A;
  163.  
  164.     } while (true);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement