Advertisement
plarmi

workcpp_10_1

Jun 23rd, 2023
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Complex {
  4. private:
  5.     double real;
  6.     double imaginary;
  7.  
  8. public:
  9.     // Конструкторы
  10.     Complex() : real(0.0), imaginary(0.0) {}
  11.     Complex(double r, double i) : real(r), imaginary(i) {}
  12.  
  13.     // Перегрузка оператора ввода >>
  14.     friend std::istream& operator>>(std::istream& is, Complex& complex) {
  15.         is >> complex.real >> complex.imaginary;
  16.         return is;
  17.     }
  18.  
  19.     // Перегрузка оператора вывода <<
  20.     friend std::ostream& operator<<(std::ostream& os, const Complex& complex) {
  21.         os << "(" << complex.real << " + " << complex.imaginary << "i)";
  22.         return os;
  23.     }
  24.  
  25.     // Перегрузка оператора сложения +
  26.     Complex operator+(const Complex& other) const {
  27.         double sumReal = real + other.real;
  28.         double sumImaginary = imaginary + other.imaginary;
  29.         return Complex(sumReal, sumImaginary);
  30.     }
  31.  
  32.     // Перегрузка оператора вычитания -
  33.     Complex operator-(const Complex& other) const {
  34.         double diffReal = real - other.real;
  35.         double diffImaginary = imaginary - other.imaginary;
  36.         return Complex(diffReal, diffImaginary);
  37.     }
  38.  
  39.     // Перегрузка оператора неравенства !=
  40.     bool operator!=(const Complex& other) const {
  41.         return real != other.real || imaginary != other.imaginary;
  42.     }
  43.  
  44.     // Перегрузка оператора равенства ==
  45.     bool operator==(const Complex& other) const {
  46.         return real == other.real && imaginary == other.imaginary;
  47.     }
  48. };
  49.  
  50. int main() {
  51.     Complex c1(2.0, 3.0);
  52.     Complex c2(1.0, 4.0);
  53.  
  54.     // Ввод комплексных чисел с помощью перегруженного оператора >>
  55.     std::cout << "Enter a complex number (format: real imaginary): ";
  56.     std::cin >> c1;
  57.  
  58.     std::cout << "Enter another complex number (format: real imaginary): ";
  59.     std::cin >> c2;
  60.  
  61.     // Вывод комплексных чисел с помощью перегруженного оператора <<
  62.     std::cout << "Complex number 1: " << c1 << std::endl;
  63.     std::cout << "Complex number 2: " << c2 << std::endl;
  64.  
  65.     // Сложение двух комплексных чисел с помощью перегруженного оператора +
  66.     Complex sum = c1 + c2;
  67.     std::cout << "Sum: " << sum << std::endl;
  68.  
  69.     // Вычитание двух комплексных чисел с помощью перегруженного оператора -
  70.     Complex diff = c1 - c2;
  71.     std::cout << "Difference: " << diff << std::endl;
  72.  
  73.     // Проверка на неравенство двух комплексных чисел с помощью перегруженного оператора !=
  74.     if (c1 != c2) {
  75.         std::cout << "The complex numbers are not equal." << std::endl;
  76.     } else {
  77.         std::cout << "The complex numbers are equal." << std::endl;
  78.     }
  79.  
  80.     // Проверка на равенство двух комплексных чисел с помощью перегруженного оператора ==
  81.     if (c1 == c2) {
  82.         std::cout << "The complex numbers are equal." << std::endl;
  83.     } else {
  84.         std::cout << "The complex numbers are not equal." << std::endl;
  85.     }
  86.  
  87.     return 0;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement