Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- struct matrix
- {
- vector<vector<int> > tab;
- int n;
- int m;
- matrix ( )
- { }
- matrix(int w1, int w2)
- : n{w1}, m{w2}
- {
- tab.resize(w1);
- for(int i=0; i<w1; i++) tab[i].resize(w2);
- }
- };
- // macierz jednostkowa
- matrix jed(const int& n)
- {
- matrix temp=matrix(n,n);
- for(int i=0; i<n; i++) temp.tab[i][i]=1;
- return temp;
- }
- //dodawanie macierzy
- matrix operator + ( const matrix& m1, const matrix& m2 )
- {
- if(m1.n!=m2.n || m1.m!=m2.m) throw runtime_error( "you cannot add two matrixes of different size!" );
- matrix res = matrix(m1.n, m1.m);
- for(int i=0; i<res.n; i++)
- for(int j=0; j<res.m; j++)
- res.tab[i][j]=m1.tab[i][j]+m2.tab[i][j];
- return res;
- }
- // odejmowanie macierzy
- matrix operator - ( const matrix& m1, const matrix& m2 )
- {
- if(m1.n!=m2.n || m1.m!=m2.m) throw runtime_error( "you cannot substract two matrixes of different size!" );
- matrix res= matrix(m1.n, m1.m);
- for(int i=0; i<res.n; i++)
- for(int j=0; j<res.m; j++)
- res.tab[i][j]=m1.tab[i][j]-m2.tab[i][j];
- return res;
- }
- //mnozenie macierzy przez stala
- matrix operator * ( const int& c, const matrix& mat)
- {
- matrix res= matrix(mat.n, mat.m);
- for(int i=0; i<res.n; i++)
- for(int j=0; j<res.m; j++)
- res.tab[i][j]=mat.tab[i][j]*c;
- return res;
- }
- //mnozenie macierzy przez macierz
- matrix operator * ( const matrix& m1, const matrix& m2)
- {
- if(m1.m!=m2.n) throw runtime_error( "you cannot multiply these matrixes!" );
- int x=m1.m;
- matrix res=matrix(m1.n, m2.m);
- for(int i=0; i<res.n; i++)
- for(int j=0; j<res.m; j++)
- for(int k=0; k<x; k++)
- res.tab[i][j]+=m1.tab[i][k]*m2.tab[k][j];
- return res;
- }
- //potegowanie macierzy
- matrix operator ^ (const matrix& mat, const int& wykl)
- {
- if(mat.n!=mat.m) throw runtime_error( "you can only raise a square matrix!" );
- if(!wykl) return jed(mat.n);
- else
- {
- if(wykl%2==1) return mat*(mat^(wykl-1));
- else if(wykl==1) return mat;
- else
- {
- return (mat^(wykl/2))*(mat^(wykl/2));
- }
- }
- }
- // wypisywanie
- ostream& operator << ( ostream& stream, const matrix& mat )
- {
- for(int i=0; i<mat.n; i++)
- {
- for(int j=0; j<mat.m; j++)
- stream << mat.tab[i][j] << " ";
- stream << "\n";
- }
- return stream;
- }
- //wczytywanie
- istream& operator >> ( istream& stream, matrix& mat )
- {
- stream >> mat.n >> mat.m;
- mat=matrix(mat.n, mat.m);
- for(int i=0; i<mat.n; i++)
- {
- for(int j=0; j<mat.m; j++)
- stream >> mat.tab[i][j];
- }
- return stream;
- }
- int main()
- {
- int wykladnik;
- matrix x, y;
- cout << "Podaj wymiary i macierz nr1 \n";
- cin >> x;
- cout << "Podaj wymiary i macierz nr2 \n";
- cin >> y;
- cout << "Podaj wykladnik potegi\n";
- cin >> wykladnik;
- cout << "Potegowanie:\n";
- cout << (x^wykladnik);
- cout << "Dodawanie:\n";
- cout << x+y;
- cout << "Mnozenie:\n";
- cout << x*y;
- cout << "Odejmowanie:\n";
- cout << x-y;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement