Advertisement
Lavig

Другий семестр. Лабораторна робота №9-10 (Завдання 2)

Mar 23rd, 2025
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <vector>
  4. #include <fstream>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     SetConsoleOutputCP(1251);
  11.     SetConsoleCP(1251);
  12.     ifstream input_file("D:/files/input.txt");
  13.     if (!input_file) {
  14.         cout << "Файлу, з якого мають зчитуватися числа, не існує!" << endl;
  15.         return 0;
  16.     }
  17.     vector<vector<vector<int>>> all_matrices;
  18.     vector<vector<int>> matrix;
  19.     string line;
  20.     while (getline(input_file, line)) {
  21.         if (line.empty()) {
  22.             if (!matrix.empty()) {
  23.                 all_matrices.push_back(matrix);
  24.                 matrix.clear();
  25.             }
  26.         }
  27.         else {
  28.             stringstream ss(line);
  29.             vector<int> row;
  30.             int num;
  31.             while (ss >> num) {
  32.                 row.push_back(num);
  33.             }
  34.             matrix.push_back(row);
  35.         }
  36.     }
  37.     if (!matrix.empty()) {
  38.         all_matrices.push_back(matrix);
  39.     }
  40.     input_file.close();
  41.     if (all_matrices.empty()) {
  42.         cout << "У файлі немає матриць!" << endl;
  43.         return 0;
  44.     }
  45.     ofstream output_file("D:/files/input.txt");
  46.     ofstream odd_file("D:/files/output.txt");
  47.     if (!output_file || !odd_file) {
  48.         cout << "Помилка відкриття вихідних файлів!" << endl;
  49.         return 0;
  50.     }
  51.     bool has_odd_sum_matrix = false;
  52.     for (int m = 0; m < all_matrices.size(); m++) {
  53.         matrix = all_matrices[m];
  54.         int sum = 0;
  55.         for (int i = 0; i < matrix.size(); i++) {
  56.             for (int j = 0; j < matrix[i].size(); j++) {
  57.                 if (matrix[i][j] < 0 && matrix[i][j] % 2 != 0) {
  58.                     sum += matrix[i][j];
  59.                 }
  60.             }
  61.         }
  62.         if (sum % 2 != 0) {
  63.             has_odd_sum_matrix = true;
  64.             for (int i = 0; i < matrix.size(); i++) {
  65.                 for (int j = 0; j < matrix[i].size(); j++) {
  66.                     odd_file << matrix[i][j] << " ";
  67.                 }
  68.                 odd_file << "\n";
  69.             }
  70.             odd_file << "\n";
  71.             for (int i = 0; i < matrix.size(); i++) {
  72.                 for (int j = 0; j < matrix[i].size(); j++) {
  73.                     if (i == j) {
  74.                         matrix[i][j] = 1;
  75.                     }
  76.                     else {
  77.                         matrix[i][j] = 0;
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.         for (int i = 0; i < matrix.size(); i++) {
  83.             for (int j = 0; j < matrix[i].size(); j++) {
  84.                 output_file << matrix[i][j] << " ";
  85.             }
  86.             output_file << "\n";
  87.         }
  88.         output_file << "\n";
  89.     }
  90.     output_file.close();
  91.     odd_file.close();
  92.     if (!has_odd_sum_matrix) {
  93.         cout << "Матриць із непарною сумою від'ємних чисел не знайдено." << endl;
  94.     }
  95.     cout << "Файли успішно оновлені!" << endl;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement