Advertisement
chrissj

Matrica

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