Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- #include <vector>
- using namespace std;
- struct Сonstraints {
- Сonstraints() :
- min_size_rows(1U),
- max_size_rows(100U),
- min_size_columns(1U),
- max_size_columns(100U),
- min_value(-1000),
- max_value(1000) {
- }
- size_t min_size_rows;
- size_t max_size_rows;
- size_t min_size_columns;
- size_t max_size_columns;
- int min_value;
- int max_value;
- };
- struct Error {
- bool error;
- string message;
- };
- class Matrix {
- public:
- Error load(const string& path, const Сonstraints& con) {
- matrix_.clear();
- ifstream file(path);
- Error info{false, "Good"};
- if (file.is_open()) {
- size_t n, m;
- file >> n >> m;
- int value;
- if (n < con.min_size_rows) {
- info.error = true;
- info.message = "the number of rows is insufficient";
- } else if (m < con.min_size_columns) {
- info.error = true;
- info.message = "the number of columns is insufficient";
- } else if (n > con.max_size_rows) {
- info.error = true;
- info.message = "the number of rows is redundant";
- } else if (m > con.max_size_columns) {
- info.error = true;
- info.message = "the number of columns is redundant";
- }
- if (info.error) return info;
- for (size_t i = 0; i < n; ++i) {
- vector<int> row;
- for (size_t j = 0; j < m; ++j) {
- file >> value;
- if (value < con.min_value || value > con.max_value) {
- info.error = true;
- info.message = "file contains invalid values";
- matrix_.clear();
- return info;
- }
- row.push_back(value);
- }
- matrix_.emplace_back(row);
- }
- file.close();
- }
- return info;
- }
- bool find_positive_row()const {
- for (const auto &row : matrix_) {
- auto flag = true;
- for (const auto value : row) {
- if (value < 0) {
- flag = false;
- break;
- }
- }
- if (flag) return flag;
- }
- return false;
- }
- private:
- vector<vector<int>> matrix_;
- };
- int main() {
- const auto path = "file.txt"s;
- const Сonstraints con;
- Matrix matrix;
- const auto status = matrix.load(path, con);
- if (status.error) cout << "Error: " << status.message << '\n';
- else {
- if (matrix.find_positive_row()) cout << "YES";
- else cout << "NO";
- cout.put('\n');
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement