ke_timofeeva7

Untitled

Dec 11th, 2024
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <complex/complex.hpp>
  2. #include <cmath>
  3.  
  4. Complex::Complex(const double real) : Complex(real, 0.0) {}
  5. Complex::Complex(const double real, const double imaginary) : re(real), im(imaginary) {}
  6.  
  7. std::istream& operator>>(std::istream& is, Complex& rhs) {
  8.     return rhs.read(is);
  9. }
  10.  
  11. std::ostream& operator<<(std::ostream& os, const Complex& rhs) {
  12.     return rhs.write(os);
  13. }
  14.  
  15. std::istream& Complex::read(std::istream& is) {
  16.     char lbrace;
  17.     double real;
  18.     char c;
  19.     double imaginary;
  20.     char rbrace;
  21.     is >> lbrace >> re >> c >> im >> rbrace;
  22.     if (is.good()) {
  23.         if (Complex::lbrace == lbrace && Complex::separator == c && Complex::rbrace == rbrace) {
  24.             re = real;
  25.             im = imaginary;
  26.             correct = true;
  27.         } else {
  28.             is.setstate(std::ios_base::failbit);
  29.         }
  30.     }
  31.     return is;
  32. }
  33.  
  34. std::ostream& Complex::write(std::ostream& os) const {
  35.     os << '{' << re << ", " << im << '}';
  36.     return os;
  37. }
  38.  
  39. bool Complex::operator==(const Complex& rhs) const {
  40.     return abs(re - rhs.re) < 2 * std::numeric_limits <double>::epsilon() && abs(im - rhs.im) < 2 * std::numeric_limits <double>::epsilon();
  41. }
  42.  
  43. bool Complex::operator!=(const Complex& rhs) const {
  44.     return !operator==(rhs);
  45. }
  46.  
  47. Complex Complex::operator-() const noexcept {
  48.     return Complex(-re, -im);
  49. }
  50.  
  51. Complex& Complex::operator+=(const Complex& rhs) {
  52.     re += rhs.re;
  53.     im += rhs.im;
  54.     return *this;
  55. }
  56.  
  57. Complex& Complex::operator+=(const double rhs) {
  58.     return operator+=(Complex(rhs));
  59. }
  60.  
  61. Complex& Complex::operator-=(const Complex& rhs) {
  62.     re -= rhs.re;
  63.     im -= rhs.im;
  64.     return *this;
  65. }
  66.  
  67. Complex& Complex::operator-=(const double rhs) {
  68.     return operator-=(Complex(rhs));
  69. }
  70.  
  71. Complex& Complex::operator*=(const Complex& rhs) {
  72.     double t = re;
  73.     re = (re * rhs.re - im * rhs.im);
  74.     im = (t * rhs.im + im * rhs.re);
  75.     return *this;
  76. }
  77.  
  78. Complex& Complex::operator*=(const double rhs) {
  79.     return operator*=(Complex(rhs));
  80. }
  81.  
  82. Complex& Complex::operator/=(const Complex& rhs) {
  83.     if (pow(rhs.re, 2) + pow(rhs.im, 2) != 0) {
  84.         double t = re;
  85.         re = (re * rhs.re + im * rhs.im) / (pow(rhs.re, 2) + pow(rhs.im, 2));
  86.         im = (im * rhs.re - t * rhs.im) / (pow(rhs.re, 2) + pow(rhs.im, 2));
  87.     } else {
  88.         std::cerr << "Error: dividing by 0. ";
  89.         re = NAN;
  90.         im = NAN;
  91.     }
  92.     return *this;
  93. }
  94.  
  95. Complex& Complex::operator/=(const double rhs) {
  96.     return operator/=(Complex(rhs));
  97. }
  98.  
  99. // сложение
  100. Complex operator+(const Complex& lhs, const Complex& rhs) {
  101.     return Complex(lhs) += rhs;
  102. }
  103.  
  104. Complex operator+(const Complex& lhs, const double rhs) {
  105.     return Complex(lhs) += rhs;
  106. }
  107.  
  108. Complex operator+(const double lhs, const Complex& rhs) {
  109.     return Complex(lhs) += rhs;
  110. }
  111.  
  112. // вычитание
  113. Complex operator-(const Complex& lhs, const Complex& rhs) {
  114.     return Complex(lhs) -= rhs;
  115. }
  116.  
  117. Complex operator-(const Complex& lhs, const double rhs) {
  118.     return Complex(lhs) -= rhs;
  119. }
  120.  
  121. Complex operator-(const double lhs, const Complex& rhs) {
  122.     return Complex(lhs) -= rhs;
  123. }
  124.  
  125. // умножение
  126. Complex operator*(const Complex& lhs, const Complex& rhs) {
  127.     return Complex(lhs) *= rhs;
  128. }
  129.  
  130. Complex operator*(const Complex& lhs, const double rhs) {
  131.     return Complex(lhs) *= rhs;
  132. }
  133.  
  134. Complex operator*(const double lhs, const Complex& rhs) {
  135.     return Complex(lhs) *= rhs;
  136. }
  137.  
  138. // деление
  139. Complex operator/(const Complex& lhs, const Complex& rhs) {
  140.     return Complex(lhs) /= rhs;
  141. }
  142.  
  143. Complex operator/(const Complex& lhs, const double rhs) {
  144.     return Complex(lhs) /= rhs;
  145. }
  146.  
  147. Complex operator/(const double lhs, const Complex& rhs) {
  148.     return Complex(lhs) /= rhs;
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment