SHARE
    TWEET
    Lavig

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

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

    AltStyle によって変換されたページ (->オリジナル) /