Advertisement
Guest User

macierz

a guest
May 22nd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <string>
  6. #include "Zespolone.h"
  7. #include "Punkt.h"
  8. using namespace std;
  9.  
  10.  
  11. template<class typ>
  12. class Macierz
  13. {
  14. private:
  15.     int kolumny,wiersze;
  16.  
  17.     vector <vector<typ>> macierz;
  18.  
  19. public:
  20.    
  21.     Macierz(int kol, int wier)
  22.     {
  23.         this->kolumny = kol;
  24.         this->wiersze = wier;
  25.         macierz.resize(wiersze, vector<typ>(kolumny, 0));
  26.        
  27.     }
  28.    
  29.     Macierz operator+(Macierz matrix) //dodawanie macierzy
  30.     {
  31.         Macierz wynik(matrix.kolumny,matrix.wiersze);
  32.         try {
  33.             if (kolumny != matrix.kolumny || wiersze != matrix.wiersze)
  34.             {
  35.                 string blad = "rozmiar macierzy sie rozni";
  36.                 throw(blad);
  37.             }
  38.             for (int i = 0; i < wiersze; i++)
  39.                     {
  40.                         for (int p = 0; p < kolumny; p++)
  41.                         {
  42.                             wynik.macierz[i][p] = macierz[i][p] + matrix.macierz[i][p];
  43.                         }
  44.                     }
  45.         }
  46.         catch(string wyjatek)
  47.         {
  48.             cout << "Blad : " << wyjatek << endl;
  49.         }
  50.  
  51.         return wynik;
  52.     }
  53.  
  54.     Macierz operator-(Macierz matrix) //odejmowanie macierzy
  55.     {
  56.         Macierz wynik(matrix.kolumny, matrix.wiersze);
  57.         try {
  58.             if (kolumny != matrix.kolumny || wiersze != matrix.wiersze)
  59.             {
  60.                 string blad = "rozmiar macierzy sie rozni";
  61.                 throw(blad);
  62.             }
  63.             for (int i = 0; i < wiersze; i++)
  64.             {
  65.                 for (int p = 0; p < kolumny; p++)
  66.                 {
  67.                     wynik.macierz[i][p] = macierz[i][p] - matrix.macierz[i][p];
  68.                 }
  69.             }
  70.         }
  71.         catch (string wyjatek)
  72.         {
  73.             cout << "Blad : " << wyjatek << endl;
  74.         }
  75.  
  76.         return wynik;
  77.     }
  78.  
  79.  
  80.     Macierz operator*(Macierz matrix) //mnozenie macierzy
  81.     {
  82.         Macierz wynik(matrix.kolumny, matrix.wiersze);
  83.         try {
  84.             if (kolumny != matrix.wiersze)
  85.             {
  86.                 string blad = "rozmiar kolumn i wierszy sie rozni";
  87.                 throw(blad);
  88.             }
  89.             for (int i = 0; i < wiersze; ++i)
  90.             {
  91.                 for (int j = 0; j < matrix.kolumny; ++j)
  92.                 {
  93.                     for (int k = 0; k < kolumny; ++k)
  94.                     {
  95.                         wynik.macierz[i][j] = macierz[i][k] * matrix.macierz[k][j];
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.         catch (string wyjatek)
  101.         {
  102.             cout << "Blad : " << wyjatek << endl;
  103.         }
  104.  
  105.         return wynik;
  106.     }
  107.  
  108.  
  109.     Macierz operator*(int N) //mnozenie macierzy przez liczbe
  110.     {
  111.         Macierz wynik(kolumny, wiersze);
  112.        
  113.             for (int i = 0; i < wiersze; i++)
  114.             {
  115.                 for (int j = 0; j < kolumny; j++)
  116.                 {
  117.                     wynik.macierz[i][j] = macierz[i][j] * N;
  118.                 }
  119.             }
  120.        
  121.    
  122.         return wynik;
  123.     }
  124.  
  125.  
  126.     Macierz operator!() //negacja
  127.     {
  128.         Macierz wynik(kolumny, wiersze);
  129.        
  130.         for (int i = 0; i < wiersze; i++)
  131.         {
  132.             for (int p = 0; p < kolumny; p++)
  133.             {
  134.                 wynik.macierz[i][p] = macierz[i][p] * -1;
  135.             }
  136.         }
  137.            
  138.        
  139.         return wynik;
  140.     }
  141. //  bool operator>(Macierz matrix) //negacja
  142. //  {
  143. //      typ suma1,suma2;
  144. //      for (int i = 0; i < wiersze; i++)
  145. //      {
  146. //          for (int p = 0; p < kolumny; p++)
  147. //          {
  148. //              suma1 = macierz[i][p];
  149. //          }
  150. //      }
  151. //      for (int i = 0; i < matrix.wiersze; i++)
  152. //      {
  153. //          for (int p = 0; p < matrix.kolumny; p++)
  154. //          {
  155. //              suma2 = matrix.macierz[i][p];
  156. //          }
  157. //      }
  158. //      if (suma1 >= suma2)
  159. //      {
  160. //          cout << " pierwszy wiekszy" << endl;
  161. //          return true;
  162. //      }
  163. //      else
  164. //      {
  165. //          cout << " druga wieksza" << endl;
  166. //return false;
  167. //      }
  168. //         
  169. //     
  170. //  }
  171. //  bool operator<(Macierz matrix) //negacja
  172. //  {
  173. //     
  174. //      return !(*this > matrix);
  175. //
  176. //  }
  177. //
  178. //
  179.     //wypisywanie
  180.  
  181.     template<class typdanych>
  182.     friend ostream& operator<< (std::ostream& stream, const typdanych& matrix) {
  183.  
  184.         for (int i = 0; i < matrix.wiersze; i++)
  185.         {
  186.             cout << "|";
  187.             for (int p = 0; p < matrix.kolumny; p++)
  188.             {
  189.                 stream << showpos << right;
  190.                 stream << setw(5);
  191.                 stream << matrix.macierz[i][p];
  192.             }
  193.            
  194.             cout << "  |" << endl;
  195.         }
  196.  
  197.         cout << endl;
  198.         return stream;
  199.     }
  200.    
  201.    
  202.     void wypelnij()
  203.     {
  204.         cout << "Wprowadz liczby" << endl;
  205.         for (int i = 0; i < wiersze; i++)
  206.         {
  207.             for (int p = 0; p < kolumny; p++)
  208.             {
  209.                 int x;
  210.                 cin >> x;
  211.                 macierz[i][p] = x;
  212.             }
  213.         }
  214.     }
  215.     void wypelnijPunktami()
  216.     {
  217.         int x, y, z;
  218.  
  219.  
  220.         cout << "Wprowadz liczby" << endl;
  221.         for (int i = 0; i < wiersze; i++)
  222.         {
  223.             for (int p = 0; p < kolumny; p++)
  224.             {
  225.             cout << "Wprowadz x" << endl;
  226.             cin >> x;
  227.             cout << "Wprowadz y" << endl;
  228.             cin >> y;
  229.             cout << "Wprowadz z" << endl;
  230.             cin >> z;
  231.             macierz[i][p] = Punkt(x,y,z);
  232.             }
  233.         }
  234.     }
  235.     void wypelnijZespolone()
  236.     {
  237.         int x, y;
  238.         cout << "Wprowadz liczby" << endl;
  239.         for (int i = 0; i < wiersze; i++)
  240.         {
  241.             for (int p = 0; p < kolumny; p++)
  242.             {
  243.                 cout << "Wprowadz czesc rzeczywista" << endl;
  244.                 cin >> x;
  245.                 cout << "Wprowadz czesc zespolona" << endl;
  246.                 cin >> y;
  247.                
  248.                 macierz[i][p] = Zespolona(x, y);
  249.             }
  250.         }
  251.     }
  252.  
  253.  
  254. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement