Risonna

programm languages lab2 zad1(complex)

Oct 25th, 2020
900
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <math.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Complex {
  6.     friend ostream& operator<<(ostream& out, const Complex& s);
  7.     friend double mod(Complex& mod);
  8. private:
  9.     double x;
  10.     double y;
  11.     double m;
  12. public:
  13.     Complex() {
  14.         x = 0;
  15.         y = 0;
  16.         m = 0;
  17.     }
  18.     Complex(int a, int b) {
  19.         x = a;
  20.         y = b;
  21.     }
  22.  
  23.     Complex operator +(const Complex& other) {
  24.         Complex temp;
  25.         temp.x = this->x + other.x;
  26.         temp.y = this->y + other.y;
  27.         return temp;
  28.     }
  29.     Complex operator -(const Complex& other) {
  30.         Complex temp;
  31.         temp.x = this->x - other.x;
  32.         temp.y = this->y - other.y;
  33.         return temp;
  34.     }
  35.     Complex operator *(const Complex& other) {
  36.         Complex temp;
  37.         temp.x = (this->x * other.x) - (this->y * other.y);
  38.         temp.y = (this->x * other.y) + (this->y * other.x);
  39.         return temp;
  40.     }
  41.     bool operator ==(const Complex& other) {
  42.         return this->x == other.x && this->y == other.y;
  43.     }
  44.     bool operator !=(const Complex& other) {
  45.         return this->x == other.x && this->y == other.y;
  46.     }
  47.     bool operator >(const Complex& other) {
  48.         return this->x > other.x && this->y > other.y;
  49.     }
  50.     bool operator <(const Complex& other) {
  51.         return this->x < other.x&& this->y < other.y;
  52.     }
  53. };
  54. ostream& operator<<(ostream& out, const Complex& s) {
  55.     out << s.x << "+" << s.y << "i";
  56.     return out;
  57. }
  58. double mod(Complex& mod) {
  59.     mod.m = sqrt(pow(mod.x, 2) + pow(mod.y, 2));
  60.     return mod.m;
  61. }
  62.  
  63.  
  64. int main()
  65. {
  66.     Complex z(3, 5);
  67.     cout << z;
  68.     cout << endl;
  69.     Complex z1(3, 5);
  70.     cout << z1;
  71.     cout << endl;
  72.     Complex sum;
  73.     sum = z + z1;
  74.     cout << sum;
  75.     cout << endl;
  76.     Complex mult;
  77.     mult = z * z1;
  78.     cout << mult;
  79.     cout << endl;
  80.     mod(z);
  81.     mod(z1);
  82.     bool result1 = mod(z) == mod(z1);
  83.     cout << "z==z1 " << result1;
  84.     cout << endl;
  85.     bool result2 = mod(z) != mod(z1);
  86.     cout << "z!=z1 " << result2;
  87.     cout << endl;
  88.     bool result3 = mod(z) > mod(z1);
  89.     cout << "z>z1 " << result3;
  90.     cout << endl;
  91.     bool result4 = mod(z) < mod(z1);
  92.     cout << "z<z1 " << result4;
  93.     cout << endl;
  94.     return 0;
  95. }
  96.  
  97.  
RAW Paste Data