Advertisement
deko96

Матрица

Apr 5th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Matrica {
  6. private:
  7.     float m[10][10];
  8.     int r;
  9.     int c;
  10. public:
  11.     Matrica() {
  12.         r = 0;
  13.         c = 0;
  14.         for(int i = 0; i < 10; ++i) {
  15.             for(int j = 0; j < 10; ++j)
  16.                 m[i][j] = 0;
  17.         }
  18.     }
  19.     Matrica operator +(int const &n) {
  20.         Matrica t;
  21.         t.r = r;
  22.         t.c = c;
  23.         for(int i = 0; i < r; ++i) {
  24.             for(int j = 0; j < c; ++j)
  25.                 t.m[i][j] = m[i][j] + n;
  26.         }
  27.         return t;
  28.     }
  29.     Matrica operator -(const Matrica &x) {
  30.         Matrica t;
  31.         t.r = x.r;
  32.         t.c = x.c;
  33.         for(int i = 0; i < x.r; ++i) {
  34.             for(int j = 0; j < x.c; ++j)
  35.                 t.m[i][j] = m[i][j] - x.m[i][j];
  36.         }
  37.         return t;
  38.     }
  39.     Matrica operator *(const Matrica &x) {
  40.         Matrica t;
  41.         t.r = x.r;
  42.         t.c = x.c;
  43.         for(int i = 0; i < x.r; ++i) {
  44.             for(int j = 0; j < x.c; ++j) {
  45.                 float tmp = 0;
  46.                 for(int k = 0; k < x.r; ++k)
  47.                     tmp += m[i][k] * x.m[k][j];
  48.                 t.m[i][j] = tmp;
  49.             }
  50.         }
  51.         return t;
  52.     }
  53.     friend istream& operator>>( istream& in, Matrica& x );
  54.     friend ostream& operator<<( ostream& out, Matrica& x );
  55. };
  56. istream& operator>>( istream& in, Matrica& x )
  57. {
  58.     in >> x.r;
  59.     in >> x.c;
  60.     for(int i=0; i<x.r; i++)
  61.         for(int j=0; j<x.c; j++)
  62.             in >> x.m[i][j];
  63.     return in;
  64. }
  65. ostream& operator<<( ostream& out, Matrica& x )
  66. {
  67.     for(int i=0; i<x.r; i++)
  68.     {
  69.         for( int j=0; j<x.c; j++)
  70.             out<<x.m[i][j]<<" ";
  71.         out<<endl;
  72.     }
  73.     return out;
  74. }
  75. int main()
  76. {
  77.     Matrica A,B,C;
  78.     cin>>A>>B>>C;
  79.     Matrica D=B*C;
  80.     Matrica R=A-D+2;
  81.     cout<<R;
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement