Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <vector>
- #include <fstream>
- #include <sstream>
- using namespace std;
- int main() {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- ifstream input_file("D:/files/input.txt");
- if (!input_file) {
- cout << "Файлу, з якого мають зчитуватися числа, не існує!" << endl;
- return 0;
- }
- vector<vector<vector<int>>> all_matrices;
- vector<vector<int>> matrix;
- string line;
- while (getline(input_file, line)) {
- if (line.empty()) {
- if (!matrix.empty()) {
- all_matrices.push_back(matrix);
- matrix.clear();
- }
- }
- else {
- stringstream ss(line);
- vector<int> row;
- int num;
- while (ss >> num) {
- row.push_back(num);
- }
- matrix.push_back(row);
- }
- }
- if (!matrix.empty()) {
- all_matrices.push_back(matrix);
- }
- input_file.close();
- if (all_matrices.empty()) {
- cout << "У файлі немає матриць!" << endl;
- return 0;
- }
- ofstream output_file("D:/files/input.txt");
- ofstream odd_file("D:/files/output.txt");
- if (!output_file || !odd_file) {
- cout << "Помилка відкриття вихідних файлів!" << endl;
- return 0;
- }
- bool has_odd_sum_matrix = false;
- for (int m = 0; m < all_matrices.size(); m++) {
- matrix = all_matrices[m];
- int sum = 0;
- for (int i = 0; i < matrix.size(); i++) {
- for (int j = 0; j < matrix[i].size(); j++) {
- if (matrix[i][j] < 0 && matrix[i][j] % 2 != 0) {
- sum += matrix[i][j];
- }
- }
- }
- if (sum % 2 != 0) {
- has_odd_sum_matrix = true;
- for (int i = 0; i < matrix.size(); i++) {
- for (int j = 0; j < matrix[i].size(); j++) {
- odd_file << matrix[i][j] << " ";
- }
- odd_file << "\n";
- }
- odd_file << "\n";
- for (int i = 0; i < matrix.size(); i++) {
- for (int j = 0; j < matrix[i].size(); j++) {
- if (i == j) {
- matrix[i][j] = 1;
- }
- else {
- matrix[i][j] = 0;
- }
- }
- }
- }
- for (int i = 0; i < matrix.size(); i++) {
- for (int j = 0; j < matrix[i].size(); j++) {
- output_file << matrix[i][j] << " ";
- }
- output_file << "\n";
- }
- output_file << "\n";
- }
- output_file.close();
- odd_file.close();
- if (!has_odd_sum_matrix) {
- cout << "Матриць із непарною сумою від'ємних чисел не знайдено." << endl;
- }
- cout << "Файли успішно оновлені!" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement