Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. /* Hubert Stawicki */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <cstdlib>
  7.  
  8. class Macierz{
  9. private:
  10.     int kolumny,wiersze;
  11.     double **tab;
  12.     void tworze_tab(int x,int y);
  13. public:
  14.     Macierz();
  15.     Macierz(int x,int y);
  16.     Macierz(Macierz& macierz);
  17.     void set(int x,int y, double wart);
  18.     void set_wiersze(int x)
  19.     {
  20.         wiersze=x;
  21.     }
  22.     void set_kolumny(int y)
  23.     {
  24.         kolumny=y;
  25.     }
  26.     double get(int x,int y);
  27.     int get_wiersze()
  28.     {
  29.         return wiersze;
  30.     }
  31.     int get_kolumny()
  32.     {
  33.         return kolumny;
  34.     }
  35. };
  36. void Macierz::tworze_tab(int wiersze, int kolumny)
  37. {
  38.     int i,j;
  39.     tab = (double **)malloc(wiersze*sizeof(double *));
  40.  
  41.     for(i=0;i<wiersze;i++)
  42.     {
  43.         tab[i] = (double *)malloc( kolumny*sizeof(double ) );
  44.     }
  45. }
  46. Macierz::Macierz()
  47. {
  48.     tworze_tab(1,1);
  49. }
  50. Macierz::Macierz(int wiersze,int kolumny)
  51. {
  52.     tworze_tab(wiersze,kolumny);
  53. }
  54. Macierz::Macierz(Macierz& macierz)
  55. {
  56.     wiersze = macierz.get_wiersze();
  57.     kolumny = macierz.get_kolumny();
  58.     int i,j;
  59.     tworze_tab(macierz.wiersze,macierz.kolumny);
  60.     for(i=0;    i<macierz.wiersze;  i++)
  61.     {
  62.         //tab[i] = (double *)malloc(kolumny*sizeof(double *));
  63.         for(j=0;    j<macierz.kolumny;  j++)
  64.         {
  65.             tab[i][j]=macierz.tab[i][j];
  66.             printf("%.1g  ",tab[i][j]);
  67.         }
  68.         printf("\n");
  69.     }
  70.     /*tworze_tab(macierz.wiersze, macierz.kolumny);
  71.     memcpy(tab, macierz.tab, macierz.wiersze*sizeof(double*));
  72.     for(int i=0; i<macierz.kolumny; i++)
  73.         memcpy(tab[i], macierz.tab[i], macierz.kolumny*sizeof(double));*/
  74. }
  75. void Macierz::set(int x,int y, double wart)
  76. {
  77.     if(x<wiersze && y<kolumny)
  78.         tab[x][y]=wart;
  79.     else
  80.             printf("ZLE WSPOLRZEDNE\n");
  81. }
  82. double Macierz::get(int i,int j)
  83. {
  84.     if(i<wiersze && j<kolumny)
  85.         return tab[i][j];
  86.     else
  87.             printf("ZLE WSPOLRZEDNE\n");
  88. }
  89.  
  90. int main()
  91. {
  92.     int i,j;
  93.     Macierz cos(3,3);
  94.     cos.set_wiersze(3);
  95.     cos.set_kolumny(3);
  96. /*  for(i=0;i<3;i++)
  97.         for(j=0;j<3;j++)
  98.             m1->set(i, j, rand()%3-1);
  99.     for(i=0;i<m1->get_wiersze();i++)
  100.     {
  101.         for(j=0;j<m1->get_kolumny();j++)
  102.             printf("%.1g",m1->get(i,j));
  103.         printf("\n");
  104.     }*/
  105.     for(i=0;i<cos.get_wiersze();i++)
  106.         for(j=0;j<cos.get_kolumny();j++)
  107.             cos.set(i, j, rand()%3-1);
  108.     for(i=0;i<cos.get_wiersze();i++)
  109.     {
  110.         for(j=0;j<cos.get_kolumny();j++)
  111.         {
  112.             printf("%.1lf  ",cos.get(i,j));
  113.         }
  114.         printf("\n");
  115.     }
  116.     for(i=0;i<3;i++)
  117.     {
  118.         for(j=0;j<3;j++)
  119.             cos.set(i,j,5);
  120.     }
  121.     for(i=0;i<3;i++)
  122.     {
  123.         for(j=0;j<3;j++)
  124.         {
  125.             printf("%.1lf  ",cos.get(i,j));
  126.         }
  127.         printf("\n");
  128.     }
  129.     Macierz kopiuj(cos);
  130.     for(i=0;i<kopiuj.get_wiersze();i++)
  131.     {
  132.         for(j=0;j<kopiuj.get_kolumny();j++)
  133.         {
  134.             printf("%.1lf  ",kopiuj.get(i,j));
  135.         }
  136.         printf("\n");
  137.     }
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement