Advertisement
anhnv

Bài 2

Feb 23rd, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define MATRIX_SIZE 3
  5.  
  6. class MyMatrix{
  7. private:
  8.     int m_matrix[MATRIX_SIZE][MATRIX_SIZE];
  9. public:
  10.     MyMatrix():m_matrix{}{
  11.  
  12.     }
  13.     ~MyMatrix(){
  14.  
  15.     }
  16.  
  17.     void Intput(){
  18.         cout << "input\ntwo_dimemsion_array =\n " << endl;
  19.  
  20.         for(int i = 0; i<MATRIX_SIZE; i++){
  21.             cout << "Row " << i << ": \n";
  22.             for(int j = 0; j<MATRIX_SIZE; j++){
  23.                 cin >> m_matrix[i][j];
  24.             }
  25.         }
  26.     }
  27.  
  28.     void OutputToConsole(){
  29.         for(int i = 0; i<MATRIX_SIZE; i++){
  30.             cout << "\t[";
  31.             for(int j = 0; j<MATRIX_SIZE; j++){
  32.                 cout << m_matrix[i][j];
  33.                 if(j + 1 < MATRIX_SIZE){
  34.                     cout << ", ";
  35.                 }
  36.             }
  37.             cout << "],\n";
  38.         }
  39.         cout << "]" << endl;
  40.     }
  41.  
  42.     void Rotate90Clockwise(){
  43.         //brute force
  44.         for(int j = 0; j<=MATRIX_SIZE / 2; j++){
  45.             int temp = m_matrix[0][j];
  46.             //left-top replaced by left-bottom
  47.             m_matrix[0][j] = m_matrix [MATRIX_SIZE - 1 - j][0];
  48.             //left-top replaced by right-bottom
  49.             m_matrix[MATRIX_SIZE - 1 - j][0] = m_matrix[MATRIX_SIZE - 1][MATRIX_SIZE - 1 - j];
  50.              //right-bottom replaced by right-top
  51.             m_matrix[MATRIX_SIZE - 1][MATRIX_SIZE - 1 - j] = m_matrix[j][MATRIX_SIZE - 1];
  52.             //temp to right-top
  53.             m_matrix[j][MATRIX_SIZE - 1] = temp;
  54.         }
  55.     }
  56. };
  57. int main(){
  58.     MyMatrix obj;
  59.  
  60.     obj.Intput();
  61.     cout << "#input" << endl;
  62.     obj.OutputToConsole();
  63.     obj.Rotate90Clockwise();
  64.     cout << "#output\n[\n" << endl;
  65.     obj.OutputToConsole();
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement