Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <ctime>
  4. #include <cstdlib>
  5. namespace Marko
  6. {
  7.     class Complex
  8.     {
  9.         friend bool operator==(const Complex&, const Complex&);
  10.         friend bool operator<(const Complex&,const Complex&);
  11.         friend bool operator!=(const Complex&, const Complex&);
  12.         friend bool operator>(const Complex&, const Complex&);
  13.         friend bool operator<=(const Complex&, const Complex&);
  14.         friend bool operator>=(const Complex&, const Complex&);
  15.         double Re, Im;
  16.     public:
  17.         Complex():Re(0),Im(0){}
  18.         Complex(double Re, double Im):Re(Re),Im(Im){}
  19.         void setRe(double);
  20.         void setIm(double);
  21.         double getRe() const;
  22.         double getIm() const;
  23.         double modul() const;
  24.     };
  25.     void Complex::setRe(double Re)
  26.     {
  27.         this->Re = Re;
  28.     }
  29.     void Complex::setIm(double Im)
  30.     {
  31.         this->Im = Im;
  32.     }
  33.     double Complex::getRe() const
  34.     {
  35.         return Re;
  36.     }
  37.     double Complex::getIm() const
  38.     {
  39.         return Im;
  40.     }
  41.     double Complex::modul() const
  42.     {
  43.         return sqrt(pow(Re, 2) + pow(Im, 2));
  44.     }
  45.     bool operator==(const Complex& ref1,const Complex& ref2)
  46.     {
  47.         return ref1.modul() == ref2.modul();
  48.     }
  49.     bool operator<(const Complex& ref1,const Complex& ref2)
  50.     {
  51.         return ref1.modul() < ref2.modul();
  52.     }
  53.     bool operator!=(const Complex& ref1, const Complex& ref2)
  54.     {
  55.         return !(ref1 == ref2);
  56.     }
  57.     bool operator>(const Complex& ref1, const Complex& ref2)
  58.     {
  59.         return(ref2 < ref1);
  60.     }
  61.     bool operator<=(const Complex& ref1, const Complex& ref2)
  62.     {
  63.         return(ref1 < ref2 || ref1 == ref2);
  64.     }
  65.     bool operator>=(const Complex& ref1, const Complex& ref2)
  66.     {
  67.         return(ref1 > ref2 || ref1 == ref2);
  68.     }
  69. }
  70. namespace Pavicic
  71. {
  72.     class Complex
  73.     {
  74.         friend bool operator==(const Complex&, const Complex&);
  75.         friend bool operator<(const Complex&, const Complex&);
  76.         friend bool operator!=(const Complex&, const Complex&);
  77.         friend bool operator>(const Complex&, const Complex&);
  78.         friend bool operator<=(const Complex&, const Complex&);
  79.         friend bool operator>=(const Complex&, const Complex&);
  80.         double Re, Im;
  81.     public:
  82.         Complex() :Re(0), Im(0) {}
  83.         Complex(double Re, double Im) :Re(Re), Im(Im) {}
  84.         void setRe(double);
  85.         void setIm(double);
  86.         double getRe() const;
  87.         double getIm() const;
  88.     };
  89.     void Complex::setRe(double Re)
  90.     {
  91.         this->Re = Re;
  92.     }
  93.     void Complex::setIm(double Im)
  94.     {
  95.         this->Im = Im;
  96.     }
  97.     double Complex::getRe() const
  98.     {
  99.         return Re;
  100.     }
  101.     double Complex::getIm() const
  102.     {
  103.         return Im;
  104.     }
  105.     bool operator==(const Complex& ref1, const Complex& ref2)
  106.     {
  107.         return (ref1.getRe() == ref2.getRe())||(ref1.getIm() == ref2.getIm());
  108.     }
  109.     bool operator<(const Complex& ref1, const Complex& ref2)
  110.     {
  111.         if (ref1.getRe() == ref2.getRe())
  112.             return (ref1.getIm() < ref2.getIm());
  113.         else
  114.             return (ref1.getRe() < ref2.getRe());
  115.     }
  116.     bool operator!=(const Complex& ref1, const Complex& ref2)
  117.     {
  118.         return !(ref1 == ref2);
  119.     }
  120.     bool operator>(const Complex& ref1, const Complex& ref2)
  121.     {
  122.         return(ref2 < ref1);
  123.     }
  124.     bool operator<=(const Complex& ref1, const Complex& ref2)
  125.     {
  126.         return(ref1 < ref2 || ref1 == ref2);
  127.     }
  128.     bool operator>=(const Complex& ref1, const Complex& ref2)
  129.     {
  130.         return(ref1 > ref2 || ref1 == ref2);
  131.     }
  132. }
  133. void swap(Marko::Complex* x, Marko::Complex* y)
  134. {
  135.     Marko::Complex temp = *x;
  136.     *x = *y;
  137.     *y = temp;
  138. }
  139. void swap(Pavicic::Complex* x, Pavicic::Complex* y)
  140. {
  141.     Pavicic::Complex temp = *x;
  142.     *x = *y;
  143.     *y = temp;
  144. }
  145. void sort(Marko::Complex* polje, int n)
  146. {
  147.  
  148.     int i, j;
  149.     for (i = 0; i < n - 1; i++)
  150.         for (j = 0; j < n - i - 1; j++)
  151.             if (polje[j] > polje[j + 1])
  152.                 swap(&polje[j], &polje[j + 1]);
  153. }
  154. void sort(Pavicic::Complex* polje, int n)
  155. {
  156.     int i, j;
  157.     for (i = 0; i < n - 1; i++)
  158.         for (j = 0; j < n - i - 1; j++)
  159.             if (polje[j] > polje[j + 1])
  160.                 swap(&polje[j], &polje[j + 1]);
  161. }
  162.  
  163. int main()
  164. {
  165.     srand(time(NULL));
  166.     Marko::Complex polje[10];
  167.     Pavicic::Complex polje2[10];
  168.     for (int i = 0; i < 10; i++)
  169.     {
  170.         int x = rand();
  171.         int y = rand();
  172.         polje[i] = Marko::Complex(x, y);
  173.         polje2[i] = Pavicic::Complex(x, y);
  174.     }
  175.     sort(polje, 10);
  176.     sort(polje2, 10);
  177.     for (int i = 0; i < 10; i++)
  178.         std::cout << polje[i].getRe() << " + " << polje[i].getIm() <<"i"<< std::endl;
  179.     std::cout << std::endl;
  180.     for(int i=0;i<10;i++)
  181.         std::cout << polje2[i].getRe() << " + " << polje2[i].getIm() << "i" << std::endl;
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement