Advertisement
lisachu

Untitled

Jan 18th, 2022
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. class Macierz{
  7. private:
  8.     float a;
  9.     float b;
  10.     float c;
  11.     float d;
  12. public:
  13.     Macierz()
  14.     {
  15.         float a=0;
  16.         float b=0;
  17.         float c=0;
  18.         float d=0;
  19.     }
  20.     Macierz(float a,float b, float c, float d): a(a), b(b), c(c), d(d){}
  21.  
  22.     float det();
  23.  
  24.     friend istream& operator>>(istream& in, Macierz& Z);
  25.     friend ostream& operator<<(ostream& out,const Macierz& Z);
  26.     friend Macierz operator*(const Macierz& Z, float k);
  27.     friend Macierz operator*(const Macierz& A, const Macierz& B);
  28.     friend Macierz operator^(const Macierz& Z, int n);
  29.  
  30.  
  31. };
  32.  
  33. int main()
  34. {
  35.     Macierz A(1,2,3,4), B;
  36.     cout<<"Podaj macierz: "<<endl;
  37.     cin>>B;
  38.     cout<<"Det B: "<<B.det()<<endl;
  39.     cout<<"B * k: "<<B * 3<<endl;
  40.     cout<<"A * B: "<<A * B<<endl;
  41.     cout<<"B ^ k: "<<(B ^ 2)<<endl;
  42.     return 0;
  43. }
  44.  
  45. float Macierz::det()
  46. {
  47.     return a*d-b*c;
  48. }
  49.  
  50. istream& operator>>(istream& in, Macierz& Z)
  51. {
  52.     in>>Z.a>>Z.b>>Z.c>>Z.d;
  53.     return in;
  54. }
  55.  
  56. ostream& operator<<(ostream& out,const Macierz& Z)
  57. {
  58.     out<<endl<<Z.a<<" "<<Z.b<<endl<<Z.c<<" "<<Z.d<<endl;
  59.     return out;
  60. }
  61.  
  62. Macierz operator*(const Macierz& Z, float k)
  63. {
  64.     Macierz W;
  65.     W.a=Z.a*k;
  66.     W.b=Z.b*k;
  67.     W.c=Z.c*k;
  68.     W.d=Z.d*k;
  69.  
  70.     return W;
  71. }
  72.  
  73. Macierz operator*(const Macierz& A, const Macierz& B)
  74. {
  75.     Macierz W;
  76.     W.a=(A.a*B.a)+(A.b*B.c);
  77.     W.b=(A.a*B.b)+(A.b*B.d);
  78.     W.c=(A.c*B.a)+(A.d*B.c);
  79.     W.d=(A.c*B.b)+(A.d*B.d);
  80.  
  81.     return W;
  82. }
  83.  
  84. Macierz operator^(const Macierz& Z, int n)
  85. {
  86.     Macierz W;
  87.     W = Z;
  88.     if (n == 0){
  89.         W.a = 1;
  90.         W.b = 0;
  91.         W.c = 0;
  92.         W.d = 1;
  93.     }
  94.     if (n > 1){
  95.         for(int i=0; i<n-1; ++i){
  96.             W = W * Z;
  97.         }
  98.     }
  99.     return W;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement