Advertisement
NickAndNick

Матрица. Поиск максимума и минимума в колонках

May 16th, 2018
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <vector>
  6. using namespace std;
  7. struct Сonstraints {
  8.     Сonstraints() :
  9.         min_size_rows(1U),
  10.         max_size_rows(100U),
  11.         min_size_columns(1U),
  12.         max_size_columns(100U),
  13.         min_value(-1000),
  14.         max_value(1000) {
  15.     }
  16.     size_t min_size_rows;
  17.     size_t max_size_rows;
  18.     size_t min_size_columns;
  19.     size_t max_size_columns;
  20.     int min_value;
  21.     int max_value;
  22. };
  23. struct Error {
  24.     bool error;
  25.     string message;
  26. };
  27. class Matrix {
  28. public:
  29.     Matrix() : n_(0), m_(0) {}
  30.     Error load(const string& path, const Сonstraints& con) {
  31.         matrix_.clear();
  32.         ifstream file(path);
  33.         Error info{false, "Good"};
  34.         if (file.is_open()) {
  35.             size_t n, m;
  36.             file >> n >> m;
  37.             int value;
  38.             if (n < con.min_size_rows) {
  39.                 info.error = true;
  40.                 info.message = "the number of rows is insufficient";
  41.             } else if (m < con.min_size_columns) {
  42.                 info.error = true;
  43.                 info.message = "the number of columns is insufficient";
  44.             } else if (n > con.max_size_rows) {
  45.                 info.error = true;
  46.                 info.message = "the number of rows is redundant";
  47.             } else if (m > con.max_size_columns) {
  48.                 info.error = true;
  49.                 info.message = "the number of columns is redundant";
  50.             }
  51.             if (info.error) return info;
  52.             n_ = n;
  53.             m_ = m;
  54.             for (size_t i = 0; i < n; ++i) {
  55.                 vector<int> row;
  56.                 for (size_t j = 0; j < m; ++j) {
  57.                     file >> value;
  58.                     if (value < con.min_value || value > con.max_value) {
  59.                         info.error = true;
  60.                         info.message = "file contains invalid values";
  61.                         matrix_.clear();
  62.                         return info;
  63.                     }
  64.                     row.push_back(value);
  65.                 }
  66.                 matrix_.emplace_back(row);
  67.             }
  68.             file.close();
  69.         }
  70.         return info;
  71.     }
  72.     size_t rows()const { return n_; }
  73.     size_t columns()const { return m_; }
  74.     pair<int, int> min_max(const size_t column) {
  75.         auto min = numeric_limits<int>::max();
  76.         auto max = numeric_limits<int>::min();
  77.         for (size_t i = 0; i < n_; ++i) {
  78.             if (min > matrix_[i][column]) min = matrix_[i][column];
  79.             if (max < matrix_[i][column]) max = matrix_[i][column];
  80.         }
  81.         return { min, max };
  82.     }
  83. private:
  84.     vector<vector<int>> matrix_;
  85.     size_t n_;
  86.     size_t m_;
  87. };
  88. int main() {
  89.     const auto path = "file.txt"s;
  90.     const Сonstraints con;
  91.     Matrix matrix;
  92.     const auto status = matrix.load(path, con);
  93.     if (status.error) cout << "Error: " << status.message << '\n';
  94.     else {
  95.         for (size_t i = 0; i < matrix.columns(); ++i) {
  96.             auto [min, max] = matrix.min_max(i);
  97.             cout << max << ' ' << min << '\n';
  98.         }
  99.         cout.put('\n');
  100.     }
  101.     system("pause");
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement