amarek

OOP LV5 - Zadatak2

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