Created
July 13, 2026 12:01
-
-
Save Klrfl/bddcf5323f8ba7235d89e96bd6d4250e to your computer and use it in GitHub Desktop.
operasi matriks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <vector> | |
| using namespace std; | |
| vector<vector<int>> reflect_matrix( | |
| vector<vector<int>> &matrix, | |
| const int rows_n, | |
| const int cols_m, | |
| string op | |
| ) { | |
| vector<vector<int>> reflected_matrix; | |
| if(op == "_") { | |
| for(int i = rows_n-1; i >= 0; i--) { | |
| reflected_matrix.push_back(matrix.at(i)); | |
| } | |
| } else if (op == "|") { | |
| vector<int> row; | |
| for (int i = 0; i < rows_n; i++) { | |
| row.clear(); | |
| for (int j = cols_m-1; j >= 0; j--) { | |
| row.push_back(matrix.at(i).at(j)); | |
| } | |
| reflected_matrix.push_back(row); | |
| } | |
| } | |
| return reflected_matrix; | |
| } | |
| vector<vector<int>> rotate_matrix( | |
| vector<vector<int>> &matrix, | |
| const int rows_n, | |
| const int cols_m, | |
| const int deg | |
| ) { | |
| vector<vector<int>> rotated_matrix; | |
| vector<int> row; | |
| switch (deg) { | |
| case 90: { | |
| for (int j = 0; j < cols_m; j++) { | |
| row.clear(); | |
| for (int i = rows_n-1; i >= 0; i--) { | |
| row.push_back(matrix.at(i).at(j)); | |
| } | |
| rotated_matrix.push_back(row); | |
| } | |
| break; | |
| } | |
| case 180: { | |
| for(int i = rows_n-1; i >= 0; i--) { | |
| row.clear(); | |
| for (int j = cols_m-1; j >= 0; j--) { | |
| row.push_back(matrix.at(i).at(j)); | |
| } | |
| rotated_matrix.push_back(row); | |
| } | |
| break; | |
| } | |
| case 270: { | |
| for (int j = cols_m-1; j >= 0; j--) { | |
| row.clear(); | |
| for (int i = 0; i < rows_n; i++) { | |
| row.push_back(matrix.at(i).at(j)); | |
| } | |
| rotated_matrix.push_back(row); | |
| } | |
| break; | |
| } | |
| default: | |
| cout << "unsupported \n"; | |
| } | |
| return rotated_matrix; | |
| } | |
| int main() { | |
| int rows_n, cols_m, n_ops; | |
| cin >> rows_n; | |
| cin >> cols_m; | |
| cin >> n_ops; // unused atm | |
| vector<vector<int>> matrix; | |
| // serialize matrix | |
| vector<int> row; | |
| for(int i = 0; i < rows_n; i++) { | |
| row.clear(); | |
| int a; | |
| for(int j = 0; j < cols_m; j++) { | |
| cin >> a; | |
| row.push_back(a); | |
| } | |
| matrix.push_back(row); | |
| } | |
| string op; | |
| for (int i = 0; i < n_ops; i++) { | |
| cin >> op; | |
| if(op == "|" || op == "_") { | |
| matrix = reflect_matrix(matrix, rows_n, cols_m, op); | |
| } else { | |
| matrix = rotate_matrix(matrix, rows_n, cols_m, stoi(op)); | |
| } | |
| } | |
| for (int i = 0; i < rows_n; i++) { | |
| for (int j = 0; j < cols_m; j++) { | |
| cout << matrix.at(i).at(j) << " "; | |
| } | |
| cout << "\n"; | |
| } | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment