Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <cstdlib>
- #include <algorithm>
- using namespace std;
- class Matrix
- {
- public:
- int n, m, x;
- vector < vector <int> > macierz;
- Matrix();
- ~Matrix();
- Matrix(int, int);
- void Row(int, int);
- void Col(int, int);
- void Inc();
- void Dec();
- void Transpose();
- friend ostream& operator<< (ostream&, Matrix const&);
- friend istream& operator>> (istream&, Matrix&);
- };
- int main()
- {
- int num_of_cases, num_of_oper;
- int n, m;
- string operacja;
- vector <Matrix> macierze;
- cin >> num_of_cases;
- macierze.resize(num_of_cases);
- for(int i = 0; i < num_of_cases; i++)
- {
- cin >> macierze[i];
- cin >> num_of_oper;
- for(int j = 0; j < num_of_oper; j++)
- {
- cin >> operacja;
- if(operacja == "row")
- {
- cin >> n >> m;
- macierze[i].Row(n, m);
- }
- else if(operacja == "col")
- {
- cin >> n >> m;
- macierze[i].Col(n, m);
- }
- else if(operacja == "dec")
- {
- macierze[i].Dec();
- }
- else if(operacja == "inc")
- {
- macierze[i].Inc();
- }
- else if(operacja == "transpose")
- {
- macierze[i].Transpose();
- }
- }
- cout << "Case #" << i+1 << endl;
- cout << macierze[i];
- cout << endl;
- }
- return 0;
- }
- Matrix::Matrix()
- {
- n = 0;
- m = 0;
- }
- Matrix::~Matrix()
- {
- macierz.clear();
- }
- Matrix::Matrix(int a, int b)
- {
- n = a;
- m = b;
- }
- void Matrix::Row(int a, int b)
- {
- a--;
- b--;
- vector <int> tmp = macierz[a];
- macierz[a] = macierz[b];
- macierz[b] = tmp;
- }
- void Matrix::Col(int a, int b)
- {
- a--;
- b--;
- int tmp;
- for(int i = 0; i < n; i++)
- {
- tmp = macierz[i][a];
- macierz[i][a] = macierz[i][b];
- macierz[i][b] = tmp;
- }
- }
- void Matrix::Inc()
- {
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < m; j++)
- {
- macierz[i][j]++;
- while(macierz[i][j] > 9)
- {
- macierz[i][j] -= 10;
- }
- }
- }
- }
- void Matrix::Dec()
- {
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < m; j++)
- {
- macierz[i][j]--;
- while(macierz[i][j] < 0)
- {
- macierz[i][j] += 10;
- }
- }
- }
- }
- void Matrix::Transpose()
- {
- vector < vector <int> > tmp = macierz;
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < n; j++)
- {
- macierz[i][j] = tmp[j][i];
- }
- }
- }
- ostream& operator<< (ostream &wyjscie, Matrix const& mat)
- {
- for(int i = 0; i < mat.n; i++)
- {
- for(int j = 0; j < mat.m; j++)
- {
- wyjscie << mat.macierz[i][j];
- }
- wyjscie << endl;
- }
- return wyjscie;
- }
- istream& operator>> (istream &wejscie, Matrix& mat)
- {
- // wejscie >> mat.n >> mat.m; <--- do macierzy prostokatnej
- wejscie >> mat.n; // <--- do macierzy kwadratowej
- mat.m = mat.n; // <---
- /////
- string wiersz;
- string cyfra;
- int liczba;
- /////
- for(int i = 0; i < mat.n; i++)
- {
- vector <int> temp;
- cin >> wiersz;
- for(int j = 0; j < mat.m; j++)
- {
- cyfra = wiersz[j];
- mat.x = atoi(cyfra.c_str());
- temp.push_back(mat.x);;
- }
- mat.macierz.push_back(temp);
- }
- return wejscie;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement