Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <iomanip>
- #include <conio.h>
- #include <string>
- #include <sstream>
- #include <fstream>
- using namespace std;
- int main()
- {
- setlocale(LC_ALL, "ru");
- do {
- fstream f1;
- string fname;
- cout << "Введите имя файла с массивом или \"*\", чтобы выйти из программы: ";
- getline(cin, fname);
- if (fname == "*")
- return 0;
- f1.open(fname);
- if (!f1) {
- cout << "Не удалось открыть файл " << "\"" << fname << "\"!" << endl;
- continue;
- }
- bool er = false, not_rect = false;
- int cols = 0, rows = 0, other_rows = 0, t;
- streampos p;
- do {
- p = f1.tellg();
- char c = f1.get();
- switch (c)
- {
- case ' ': case '\t':
- continue;
- case EOF:
- if (other_rows == 0) break;
- case '\n':
- if (other_rows == 0)
- continue;
- if (cols == 0)
- cols = other_rows;
- else if (cols != other_rows) {
- not_rect = true;
- break;
- }
- rows++;
- other_rows = 0;
- continue;
- default:
- f1.unget();
- f1 >> t;
- if (f1.fail() || ((c = f1.peek()) != ' ' && c != '\t' && c != '\n' && c != EOF)) {
- er = true;
- break;
- }
- other_rows++;
- continue;
- }
- break;
- } while (true);
- if (cols == 0) {
- f1.close();
- cout << "Файл " << "\"" << fname << "\"" << " пуст!" << endl;
- continue;
- }
- if (not_rect) {
- f1.close();
- cout << "Матрица не является прямоугольной, начиная со строки " << rows + 1 <<"!\n";
- continue;
- }
- if (er) {
- f1.clear();
- f1.seekg(p);
- string el;
- f1 >> el;
- f1.close();
- cout << "Неверное значение элемента матрицы в позиции [" << rows + 1 << "; " << cols + 1 << "]!" << endl;
- cout << "Последовательность некорректных символов: " << el << endl;
- continue;
- }
- int** A = NULL; int i;
- try {
- A = new int*[rows];
- for (i = 0; i < rows; i++)
- A[i] = new int[cols];
- }
- catch (...) {
- for (int k = 0; k < i; k++)
- delete[]A[k];
- delete[]A;
- f1.close();
- if (!A)
- cout << "Ошибка выделения памяти!";
- continue;
- }
- f1.clear();
- f1.seekg(0);
- cout << "Ниже приведена начальная матрица.\n";
- for (int i = 0; i < rows; i++) {
- cout << '[' << i + 1 << "]: " << setw(5);
- for (int j = 0; j < cols; j++) {
- f1 >> *(*(A + i) + j);
- cout << setw(5) << A[i][j];
- }
- cout << endl;
- }
- f1.close();
- int* sumA = new (nothrow) int[cols];
- if (!sumA) {
- cout << "Ошибка выделения памяти!" << endl;
- continue;
- }
- for (int i = 0; i < cols; i++)
- sumA[i] = A[0][i];
- if (rows > 1)
- for (int i = 1; i < rows; i++)
- for (int j = 0; j < cols; j++)
- sumA[j] += A[i][j];
- int min = sumA[0];
- for (int i = 1; i < cols; i++)
- if (sumA[i] < min)
- min = sumA[i];
- delete[] sumA;
- cout << "Минимальное значение сумм столбцов = " << min << ".\n";
- cout << "Ниже приведена преобразованная матрица.\n";
- for (int i = 0; i < rows; i++) {
- cout << '[' << i + 1 << "]: " << setw(5);
- for (int j = 0; j < cols; j++) {
- A[i][j] -= min;
- cout << setw(5) << A[i][j];
- }
- cout << endl;
- }
- f1.close();
- for (int i = 0; i < rows; i++)
- delete[]A[i];
- delete[]A;
- } while (true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement