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