Advertisement
Guest User

Matrica

a guest
Apr 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class Matrica{
  5. private:
  6.     float matrica[10][10];
  7.     int n;
  8.     int k;
  9. public:
  10.     Matrica(){
  11.     n=0;
  12.     k=0;
  13.     for(int i=0;i<10;i++){
  14.         for(int j=0;j<10;j++){
  15.             matrica[i][j]=0;
  16.         }
  17.     }
  18.     }
  19.     void setVrednost(int i,int j,int vrednost){
  20.      matrica[i][j]=vrednost;
  21.     }
  22.     void setN(int n1){
  23.     n=n1;
  24.     }
  25.     void setK(int k1){
  26.     k=k1;
  27.     }
  28.  
  29.     int getVrednost(int i,int j){
  30.     return matrica[i][j];
  31.     }
  32.  
  33.    friend istream& operator>>(istream &in, Matrica &m){
  34.     in>>m.n>>m.k;
  35.     for(int i=0;i<m.n;i++){
  36.         for(int j=0;j<m.k;j++){
  37.             in>>m.matrica[i][j];
  38.         }
  39.     }
  40.     return in;
  41.    }
  42.    friend ostream& operator<<(ostream &out,const Matrica &m){
  43.     for(int i=0;i<m.n;i++){
  44.         for(int j=0;j<m.k;j++){
  45.             out<<m.matrica[i][j]<<" ";
  46.         }
  47.         out<<endl;
  48.     }
  49.     return out;
  50.    }
  51.   Matrica operator +(const Matrica &m){
  52.     Matrica novaMatrica;
  53.     novaMatrica.setK(k);
  54.     novaMatrica.setN(n);
  55.     for(int i=0;i<n;i++){
  56.         for(int j=0;j<k;j++){
  57.             novaMatrica.setVrednost(i,j,matrica[i][j]+m.matrica[i][j]);
  58.         }
  59.  
  60.     }
  61.    return novaMatrica;
  62.   }
  63.   Matrica operator +(int vrednost){
  64.     Matrica novaMatrica;
  65.     novaMatrica.setK(k);
  66.     novaMatrica.setN(n);
  67.     for(int i=0;i<n;i++){
  68.         for(int j=0;j<k;j++){
  69.             novaMatrica.setVrednost(i,j,matrica[i][j]+vrednost);
  70.         }
  71.  
  72.     }
  73.    return novaMatrica;
  74.   }
  75.     Matrica operator -(const Matrica &m){
  76.     Matrica novaMatrica;
  77.     novaMatrica.setK(k);
  78.     novaMatrica.setN(n);
  79.     for(int i=0;i<n;i++){
  80.         for(int j=0;j<k;j++){
  81.             novaMatrica.setVrednost(i,j,matrica[i][j]-m.matrica[i][j]);
  82.         }
  83.  
  84.     }
  85.    return novaMatrica;
  86.   }
  87.  
  88.      Matrica operator *(const Matrica &m){
  89.     Matrica novaMatrica;
  90.     novaMatrica.setK(k);
  91.     novaMatrica.setN(n);
  92.     for(int i=0;i<n;i++){
  93.         for(int j=0;j<k;j++){
  94.                 for(int j1=0;j1<k;j1++){
  95.             novaMatrica.setVrednost(i,j,novaMatrica.getVrednost(i,j)+(matrica[i][j1]*m.matrica[j1][j]));
  96.         }
  97.         }
  98.  
  99.     }
  100.    return novaMatrica;
  101.   }
  102.  
  103.  
  104. };
  105. int main()
  106. {
  107.  Matrica A,B,C;
  108.  cin>>A>>B>>C;
  109.  Matrica D=B*C;
  110.  Matrica R=A-D+2;
  111.  cout<<R;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement