Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- // ---------------------------------------
- namespace Enio {
- class Complex {
- friend bool operator==(Complex&, Complex&);
- friend bool operator!=(Complex&, Complex&);
- friend bool operator<(Complex&, Complex&);
- friend bool operator>(Complex&, Complex&);
- friend bool operator<=(Complex&, Complex&);
- friend bool operator>=(Complex&, Complex&);
- private:
- double mRe, mIm;
- public:
- Complex();
- Complex(double, double);
- void setRe(double);
- void setIm(double);
- double getRe();
- double getIm();
- };
- Complex::Complex() :mRe(0), mIm(0) {}
- Complex::Complex(double Re, double Im) : mRe(Re), mIm(Im) {}
- void Complex::setRe(double Re) { mRe = Re; }
- void Complex::setIm(double Im) { mIm = Im; }
- double Complex::getRe() { return mRe; }
- double Complex::getIm() { return mIm; }
- bool operator==(Complex &ref1, Complex &ref2) {
- return sqrt(pow(ref1.mRe, 2) + pow(ref1.mIm, 2)) == sqrt(pow(ref2.mRe, 2) + pow(ref2.mIm, 2));
- }
- bool operator!=(Complex &ref1, Complex &ref2) {
- return !(ref1 == ref2);
- }
- bool operator<(Complex &ref1, Complex &ref2) {
- return sqrt(pow(ref1.mRe, 2) + pow(ref1.mIm, 2)) < sqrt(pow(ref2.mRe, 2) + pow(ref2.mIm, 2));
- }
- bool operator>=(Complex &ref1, Complex &ref2) {
- return !(ref1 < ref2);
- }
- bool operator>(Complex &ref1, Complex &ref2) {
- return sqrt(pow(ref1.mRe, 2) + pow(ref1.mIm, 2)) > sqrt(pow(ref2.mRe, 2) + pow(ref2.mIm, 2));
- }
- bool operator<=(Complex &ref1, Complex &ref2) {
- return !(ref1 > ref2);
- }
- }
- // ---------------------------------------
- namespace Dordan {
- class Complex {
- friend bool operator==(Complex&, Complex&);
- friend bool operator!=(Complex&, Complex&);
- friend bool operator<(Complex&, Complex&);
- friend bool operator>(Complex&, Complex&);
- friend bool operator<=(Complex&, Complex&);
- friend bool operator>=(Complex&, Complex&);
- private:
- double mRe, mIm;
- public:
- Complex();
- Complex(double, double);
- void setRe(double);
- void setIm(double);
- double getRe();
- double getIm();
- };
- Complex::Complex() :mRe(0), mIm(0) {}
- Complex::Complex(double Re, double Im) : mRe(Re), mIm(Im) {}
- void Complex::setRe(double Re) { mRe = Re; }
- void Complex::setIm(double Im) { mIm = Im; }
- double Complex::getRe() { return mRe; }
- double Complex::getIm() { return mIm; }
- bool operator==(Complex &ref1, Complex &ref2) {
- return ref1.mRe == ref2.mRe && ref1.mIm == ref2.mIm;
- }
- bool operator!=(Complex &ref1, Complex &ref2) {
- return !(ref1 == ref2);
- }
- bool operator<(Complex &ref1, Complex &ref2) {
- return ref1.mRe < ref2.mRe && ref1.mIm < ref2.mIm;
- }
- bool operator>=(Complex &ref1, Complex &ref2) {
- return !(ref1 < ref2);
- }
- bool operator>(Complex &ref1, Complex &ref2) {
- return ref1.mRe > ref2.mRe && ref1.mIm > ref2.mIm;
- }
- bool operator<=(Complex &ref1, Complex &ref2) {
- return !(ref1 > ref2);
- }
- }
- // ---------------------------------------
- template <class Type>
- void swap(Type *xp, Type *yp)
- {
- Type temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- template<class Type>
- void sort(Type arr[], int n)
- {
- int i, j;
- for (i = 0; i < n - 1; i++) {
- for (j = 0; j < n - i - 1; j++) {
- if (arr[j] > arr[j + 1]) {
- swap(&arr[j], &arr[j + 1]);
- }
- }
- }
- }
- // ---------------------------------------
- int main() {
- srand((unsigned)time(NULL));
- int n = 5;
- Enio::Complex* First = new Enio::Complex[n];
- Dordan::Complex* Second = new Dordan::Complex[n];
- for (int i = 0; i < n; i++) {
- First[i].setRe(rand() % 20); First[i].setIm(rand() % 20);
- Second[i].setRe(rand() % 20); Second[i].setIm(rand() % 20);
- }
- sort(First, n);
- sort(Second, n);
- cout << "First: " << endl;
- for (int i = 0; i < n; i++) {
- cout << First[i].getRe() << " + " << First[i].getIm() << "i" << endl;
- }
- cout << "Second: " << endl;
- for (int i = 0; i < n; i++) {
- cout << Second[i].getRe() << " + " << Second[i].getIm() << "i" << endl;
- }
- delete[] First;
- delete[] Second;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment