Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. struct matrix
  8. {
  9.  
  10.     vector<vector<int> > tab;
  11.     int n;
  12.     int m;
  13.  
  14.     matrix ( )
  15.     {    }
  16.     matrix(int w1, int w2)
  17.     : n{w1}, m{w2}
  18.     {
  19.         tab.resize(w1);
  20.         for(int i=0; i<w1; i++) tab[i].resize(w2);
  21.     }
  22. };
  23.  
  24. // macierz jednostkowa
  25. matrix jed(const int& n)
  26. {
  27.     matrix temp=matrix(n,n);
  28.     for(int i=0; i<n; i++) temp.tab[i][i]=1;
  29.     return temp;
  30. }
  31. //dodawanie macierzy
  32. matrix operator + ( const matrix& m1, const matrix& m2 )
  33. {
  34.     if(m1.n!=m2.n || m1.m!=m2.m) throw runtime_error( "you cannot add two matrixes of different size!" );
  35.  
  36.     matrix res = matrix(m1.n, m1.m);
  37.  
  38.     for(int i=0; i<res.n; i++)
  39.         for(int j=0; j<res.m; j++)
  40.             res.tab[i][j]=m1.tab[i][j]+m2.tab[i][j];
  41.  
  42.     return res;
  43. }
  44. // odejmowanie macierzy
  45. matrix operator - ( const matrix& m1, const matrix& m2 )
  46. {
  47.     if(m1.n!=m2.n || m1.m!=m2.m) throw runtime_error( "you cannot substract two matrixes of different size!" );
  48.  
  49.     matrix res= matrix(m1.n, m1.m);
  50.  
  51.     for(int i=0; i<res.n; i++)
  52.         for(int j=0; j<res.m; j++)
  53.             res.tab[i][j]=m1.tab[i][j]-m2.tab[i][j];
  54.  
  55.     return res;
  56. }
  57.  
  58. //mnozenie macierzy przez stala
  59. matrix operator * ( const int& c, const matrix& mat)
  60. {
  61.     matrix res= matrix(mat.n, mat.m);
  62.  
  63.  
  64.     for(int i=0; i<res.n; i++)
  65.         for(int j=0; j<res.m; j++)
  66.             res.tab[i][j]=mat.tab[i][j]*c;
  67.  
  68.     return res;
  69. }
  70.  
  71. //mnozenie macierzy przez macierz
  72. matrix operator * ( const matrix& m1, const matrix& m2)
  73. {
  74.     if(m1.m!=m2.n) throw runtime_error( "you cannot multiply these matrixes!" );
  75.     int x=m1.m;
  76.     matrix res=matrix(m1.n, m2.m);
  77.  
  78.     for(int i=0; i<res.n; i++)
  79.         for(int j=0; j<res.m; j++)
  80.             for(int k=0; k<x; k++)
  81.                 res.tab[i][j]+=m1.tab[i][k]*m2.tab[k][j];
  82.  
  83.     return res;
  84. }
  85.  
  86.  
  87. //potegowanie macierzy
  88. matrix operator ^ (const matrix& mat, const int& wykl)
  89. {
  90.     if(mat.n!=mat.m) throw runtime_error( "you can only raise a square matrix!" );
  91.  
  92.     if(!wykl) return jed(mat.n);
  93.     else
  94.     {
  95.         if(wykl%2==1) return mat*(mat^(wykl-1));
  96.         else if(wykl==1) return mat;
  97.         else
  98.         {
  99.             return (mat^(wykl/2))*(mat^(wykl/2));
  100.         }
  101.     }
  102. }
  103.  
  104. // wypisywanie
  105. ostream& operator << ( ostream& stream, const matrix& mat )
  106. {
  107.     for(int i=0; i<mat.n; i++)
  108.     {
  109.         for(int j=0; j<mat.m; j++)
  110.             stream << mat.tab[i][j] << " ";
  111.         stream << "\n";
  112.     }
  113.   return stream;
  114. }
  115. //wczytywanie
  116. istream& operator >> ( istream& stream, matrix& mat )
  117. {
  118.  
  119.     stream >> mat.n >> mat.m;
  120.     mat=matrix(mat.n, mat.m);
  121.     for(int i=0; i<mat.n; i++)
  122.     {
  123.         for(int j=0; j<mat.m; j++)
  124.             stream >> mat.tab[i][j];
  125.     }
  126.   return stream;
  127. }
  128.  
  129.  
  130. int main()
  131. {
  132.     int wykladnik;
  133.     matrix x, y;
  134.     cout << "Podaj wymiary i macierz nr1 \n";
  135.     cin >> x;
  136.     cout << "Podaj wymiary i macierz nr2 \n";
  137.     cin >> y;
  138.     cout << "Podaj wykladnik potegi\n";
  139.     cin >> wykladnik;
  140.     cout << "Potegowanie:\n";
  141.     cout << (x^wykladnik);
  142.     cout << "Dodawanie:\n";
  143.     cout << x+y;
  144.     cout << "Mnozenie:\n";
  145.     cout << x*y;
  146.     cout << "Odejmowanie:\n";
  147.     cout << x-y;
  148.  
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement