Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <cstdlib>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. class Matrix
  13. {
  14.  public:
  15.     int n, m, x;
  16.     vector < vector <int> > macierz;
  17.     Matrix();
  18.     ~Matrix();
  19.     Matrix(int, int);
  20.     void Row(int, int);
  21.     void Col(int, int);
  22.     void Inc();
  23.     void Dec();
  24.     void Transpose();
  25.  
  26.     friend ostream& operator<< (ostream&, Matrix const&);
  27.     friend istream& operator>> (istream&, Matrix&);
  28. };
  29.  
  30.  
  31.  
  32.  
  33.  
  34. int main()
  35. {
  36.     int num_of_cases, num_of_oper;
  37.     int n, m;
  38.     string operacja;
  39.     vector <Matrix> macierze;
  40.  
  41.     cin >> num_of_cases;
  42.     macierze.resize(num_of_cases);
  43.  
  44.     for(int i = 0; i < num_of_cases; i++)
  45.     {
  46.         cin >> macierze[i];
  47.         cin >> num_of_oper;
  48.         for(int j = 0; j < num_of_oper; j++)
  49.         {
  50.             cin >> operacja;
  51.             if(operacja == "row")
  52.             {
  53.                 cin >> n >> m;
  54.                 macierze[i].Row(n, m);
  55.             }
  56.             else if(operacja == "col")
  57.             {
  58.                 cin >> n >> m;
  59.                 macierze[i].Col(n, m);
  60.             }
  61.             else if(operacja == "dec")
  62.             {
  63.                 macierze[i].Dec();
  64.             }
  65.             else if(operacja == "inc")
  66.             {
  67.                 macierze[i].Inc();
  68.             }
  69.             else if(operacja == "transpose")
  70.             {
  71.                 macierze[i].Transpose();
  72.             }
  73.         }
  74.         cout << "Case #" << i+1 << endl;
  75.         cout << macierze[i];
  76.         cout << endl;
  77.     }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.     return 0;
  84. }
  85.  
  86.  
  87. Matrix::Matrix()
  88. {
  89.     n = 0;
  90.     m = 0;
  91. }
  92.  
  93. Matrix::~Matrix()
  94. {
  95.     macierz.clear();
  96. }
  97.  
  98.  
  99. Matrix::Matrix(int a, int b)
  100. {
  101.     n = a;
  102.     m = b;
  103. }
  104.  
  105. void Matrix::Row(int a, int b)
  106. {
  107.     a--;
  108.     b--;
  109.     vector <int> tmp = macierz[a];
  110.     macierz[a] = macierz[b];
  111.     macierz[b] = tmp;
  112.  
  113. }
  114.  
  115. void Matrix::Col(int a, int b)
  116. {
  117.     a--;
  118.     b--;
  119.     int tmp;
  120.     for(int i = 0; i < n; i++)
  121.     {
  122.         tmp = macierz[i][a];
  123.         macierz[i][a] = macierz[i][b];
  124.         macierz[i][b] = tmp;
  125.     }
  126.  
  127. }
  128.  
  129. void Matrix::Inc()
  130. {
  131.     for(int i = 0; i < n; i++)
  132.     {
  133.         for(int j = 0; j < m; j++)
  134.         {
  135.             macierz[i][j]++;
  136.             while(macierz[i][j] > 9)
  137.             {
  138.                 macierz[i][j] -= 10;
  139.             }
  140.         }
  141.     }
  142. }
  143.  
  144. void Matrix::Dec()
  145. {
  146.     for(int i = 0; i < n; i++)
  147.     {
  148.         for(int j = 0; j < m; j++)
  149.         {
  150.             macierz[i][j]--;
  151.             while(macierz[i][j] < 0)
  152.             {
  153.                 macierz[i][j] += 10;
  154.             }
  155.         }
  156.     }
  157. }
  158.  
  159. void Matrix::Transpose()
  160. {
  161.  
  162.     vector < vector <int> > tmp = macierz;
  163.     for(int i = 0; i < n; i++)
  164.     {
  165.         for(int j = 0; j < n; j++)
  166.         {
  167.             macierz[i][j] = tmp[j][i];
  168.         }
  169.     }
  170.  
  171.  
  172. }
  173. ostream& operator<< (ostream &wyjscie, Matrix const& mat)
  174. {
  175.     for(int i = 0; i < mat.n; i++)
  176.     {
  177.         for(int j = 0; j < mat.m; j++)
  178.         {
  179.            wyjscie << mat.macierz[i][j];
  180.         }
  181.         wyjscie << endl;
  182.     }
  183.  
  184.     return wyjscie;
  185. }
  186.  
  187. istream& operator>> (istream &wejscie, Matrix& mat)
  188. {
  189. //    wejscie >> mat.n >> mat.m;   <--- do macierzy prostokatnej
  190.     wejscie >> mat.n;         //   <--- do macierzy kwadratowej
  191.     mat.m = mat.n;            //   <---
  192.  
  193.     /////
  194.     string wiersz;
  195.     string cyfra;
  196.     int liczba;
  197.     /////
  198.  
  199.     for(int i = 0; i < mat.n; i++)
  200.     {
  201.         vector <int> temp;
  202.         cin >> wiersz;
  203.         for(int j = 0; j < mat.m; j++)
  204.         {
  205.             cyfra = wiersz[j];
  206.             mat.x = atoi(cyfra.c_str());
  207.             temp.push_back(mat.x);;
  208.         }
  209.         mat.macierz.push_back(temp);
  210.     }
  211.  
  212.  
  213.     return wejscie;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement