Advertisement
oshige_san

Untitled

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