Hyluss

Dziedziczenie macierze

Jul 2nd, 2016
552
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. // DZiedziczeniemaceirze.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <malloc.h>
  6. #include <conio.h>
  7.  
  8.  
  9.  
  10. class Macierz
  11. {
  12. protected:
  13.     int wiersze, kolumny;
  14.     float **nxm;
  15.  
  16. public:
  17.     Macierz(const int k, const int w)
  18.     {
  19.         if ( (w<0) || (k<0) || (w>100) || (k>100) )
  20.         {
  21.                
  22.         }
  23.         else
  24.         {
  25.             wiersze = w;
  26.             kolumny = k;
  27.             nxm = (float**) malloc (sizeof (float*) * kolumny);
  28.             for (int i=0;i<kolumny;i++)
  29.             {
  30.                 nxm[i] = (float*) malloc (sizeof (float) * wiersze);
  31.             }
  32.  
  33.             for (int i=0; i<kolumny;i++)
  34.             {
  35.                 for (int j=0;j<wiersze;j++)
  36.                 {
  37.                     nxm[i][j] = 0;
  38.                 }
  39.             }
  40.  
  41.         }
  42.     }
  43.  
  44.     const int Wiersze () const
  45.     {
  46.         return wiersze;
  47.     }
  48.    
  49.     const int Kolumny () const
  50.     {
  51.         return kolumny;
  52.     }
  53.  
  54.     void Ustaw (const int k,const int w,const float value)
  55.     {
  56.         nxm[k][w] = value;
  57.     }
  58.  
  59.     const float Pobierz (const int k,const int w) const
  60.     {
  61.         return nxm[k][w];
  62.     }
  63.  
  64. };
  65.  
  66.  
  67.  
  68. class MacierzWewy : public Macierz
  69. {
  70. public:
  71.     MacierzWewy (const int k, const int w) : Macierz (k, w)
  72.     {
  73.     }
  74.    
  75.     const void Wprowadz() const
  76.     {
  77.         for (int i=0;i<kolumny;i++)
  78.         {
  79.             for (int j=0;j<wiersze;j++)
  80.             {
  81.                 printf ("tab[%d][%d] - ",i,j);
  82.                 scanf ("%f", & nxm[i][j]);
  83.             }
  84.         }
  85.     }
  86.  
  87.     const void Wyswietl() const
  88.     {
  89.             for (int i=0;i<kolumny;i++)
  90.             {
  91.                 for (int j=0;j<wiersze;j++)
  92.                 {
  93.                     printf ("\ntab[%d][%d] - %.2f",i,j, nxm[i][j]);
  94.                 }
  95.             }
  96.     }
  97.  
  98. };
  99.  
  100.  
  101. class Macierz2x2 : public MacierzWewy
  102. {
  103. public:
  104.     Macierz2x2 (): MacierzWewy (2, 2)
  105.     {
  106.     }
  107.  
  108.     const float Wyznacznik() const
  109.     {
  110.         return ( ( nxm[0][0] * nxm[1][1] ) - ( nxm [0][1] * nxm [1][0] ) );
  111.     }
  112.  
  113.     const void Wyswietl () const
  114.     {
  115.         MacierzWewy::Wyswietl();
  116.         printf ("\nWyznacznik - %.2f", Wyznacznik() );
  117.     }
  118.  
  119. };
  120.  
  121. int _tmain(int argc, _TCHAR* argv[])
  122. {
  123.     Macierz mac(6,6);
  124.     mac.Wiersze();
  125.     mac.Kolumny();
  126.     mac.Ustaw(2,3,55.5);
  127.    
  128.     MacierzWewy wew (4,4);
  129.     wew.Ustaw(3,3,15.67);
  130.     wew.Wprowadz();
  131.     wew.Wyswietl();
  132.  
  133.     Macierz2x2 mala;
  134.     mala.Ustaw(1,1,1);
  135.     mala.Ustaw(0,1,2);
  136.     mala.Ustaw(0,0,3);
  137.     mala.Ustaw(1,0,4);
  138.     printf ("\nWyznacznik - %.10f\n", mala.Wyznacznik());
  139.     mala.Wyswietl();
  140.  
  141.     _getch();
  142.     return 0;
  143. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment