Advertisement
merijb

Task 1

Feb 22nd, 2025
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct ComplexNumber
  4. {
  5.     double real = 0;
  6.     double imaginary = 0;
  7. };
  8.  
  9. ComplexNumber init(double re, double im)
  10. {
  11.     ComplexNumber number = { re, im };
  12.  
  13.     return number;
  14. }
  15.  
  16. void print(const ComplexNumber& complex)
  17. {
  18.     std::cout << "Complex number (" << complex.real << ", " << complex.imaginary << "):\n";
  19.     std::cout << "Real part: " << complex.real << std::endl;;
  20.     std::cout << "Imaginary part: " << complex.imaginary << std::endl;
  21. }
  22.  
  23. ComplexNumber conjugate(const ComplexNumber& complex)
  24. {
  25.     ComplexNumber conjugateComplex = { complex.real, -complex.imaginary };
  26.  
  27.     return conjugateComplex;
  28. }
  29.  
  30. ComplexNumber add(const ComplexNumber& lhs, const ComplexNumber& rhs)
  31. {
  32.     ComplexNumber complex = { lhs.real + rhs.real, lhs.imaginary + rhs.imaginary };
  33.  
  34.     return complex;
  35. }
  36.  
  37. ComplexNumber subratct(const ComplexNumber& lhs, const ComplexNumber& rhs)
  38. {
  39.     ComplexNumber complex = { lhs.real - rhs.real, lhs.imaginary - rhs.imaginary };
  40.  
  41.     return complex;
  42. }
  43.  
  44. ComplexNumber multiply(const ComplexNumber& lhs, const ComplexNumber& rhs)
  45. {
  46.     double real = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary;
  47.     double imaginary = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real;
  48.  
  49.     ComplexNumber complex = { real, imaginary };
  50.  
  51.     return complex;
  52. }
  53.  
  54. ComplexNumber divide(const ComplexNumber& lhs, const ComplexNumber& rhs)
  55. {
  56.     double denominator = (rhs.real * rhs.real + rhs.imaginary * rhs.imaginary);
  57.  
  58.     double real = (lhs.real * rhs.real + lhs.imaginary * rhs.imaginary) / denominator;
  59.  
  60.     double imaginary = (lhs.imaginary * rhs.real - lhs.real * rhs.imaginary) / denominator;
  61.  
  62.     ComplexNumber complex = { real, imaginary };
  63.  
  64.     return complex;
  65. }
  66.  
  67. int main()
  68. {
  69.     ComplexNumber complex = multiply({ 0, 1 }, { 0, 1 });
  70.  
  71.     print(complex);
  72.  
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement