Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3.  
  4. class Macierz
  5. {
  6.     public:
  7.         Macierz(const int, const int);          //wiersze i kolumny > 1 i < 100
  8.         int getWiersze() const;
  9.         int getKolumny() const;
  10.         void Ustaw(const int, const int, const double);
  11.         double Pobierz(const int, const int);
  12.  
  13.     private:
  14.         int wiersze, kolumny;  
  15.         double ** tab;
  16. };
  17.  
  18. Macierz::Macierz(const int wiersze, const int kolumny)
  19. {
  20.     if (wiersze > 1 && wiersze < 100) this->wiersze = wiersze;
  21.     else
  22.     {
  23.         this->wiersze = 2;
  24.         printf("Zla wartosc! Wiersze ustalone na 2");
  25.     }
  26.  
  27.     if (kolumny > 1 && kolumny < 100) this->kolumny = kolumny;
  28.     else
  29.     {
  30.         this->kolumny = 2;
  31.         printf("Zla wartosc! Kolumny ustalone na 2");
  32.     }
  33.  
  34.     tab = new double * [wiersze];
  35.  
  36.     for (int i = 0; i < wiersze; i++)
  37.     {
  38.         tab[i] = new double [kolumny];
  39.  
  40.         for (int j = 0; j < kolumny; j++)
  41.         {
  42.             tab[i][j] = 0;
  43.         }
  44.     }
  45. }
  46.  
  47. int Macierz::getWiersze() const
  48. {
  49.     return wiersze;
  50. }
  51.  
  52. int Macierz::getKolumny() const
  53. {
  54.     return kolumny;
  55. }
  56.  
  57. void Macierz::Ustaw(const int wiersz, const int kolumna, const double nowaWartosc)
  58. {
  59.     tab[wiersz][kolumna] = nowaWartosc;
  60. }
  61.  
  62. double Macierz::Pobierz(const int wiersz, const int kolumna)
  63. {
  64.     return tab[wiersz][kolumna];
  65. }
  66.  
  67. class MacierzWeWy : public Macierz
  68. {
  69.     public:
  70.         MacierzWeWy(const int, const int);
  71.         void Wprowadz();
  72.         void Wyswietl();
  73. };
  74.  
  75. MacierzWeWy::MacierzWeWy(const int wiersze, const int kolumny) : Macierz(wiersze, kolumny)
  76. {}
  77.  
  78. void MacierzWeWy::Wprowadz()
  79. {
  80.     double nowaWartosc;
  81.     for (int i = 0; i < getWiersze(); i++)
  82.     {
  83.         for (int j = 0; j < getKolumny(); j++)
  84.         {
  85.             printf("tab[%i][%i] = ", i, j);
  86.             scanf("%lf", &nowaWartosc);
  87.             Ustaw(i, j, nowaWartosc);
  88.         }
  89.     }
  90. }
  91.  
  92. void MacierzWeWy::Wyswietl()
  93. {
  94.     for (int i = 0; i < getWiersze(); i++)
  95.     {
  96.         for (int j = 0; j < getKolumny(); j++)
  97.         {
  98.             printf("tab[%i][%i] = %.2lf\n", i, j, Pobierz(i, j));
  99.         }
  100.     }
  101. }
  102.  
  103. int main()
  104. {
  105.     MacierzWeWy macierzWeWy(3, 2);
  106.  
  107.     macierzWeWy.Wprowadz();
  108.     macierzWeWy.Wyswietl();
  109.  
  110.     _getch();
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement