Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <complex/complex.hpp>
- #include <cmath>
- Complex::Complex(const double real) : Complex(real, 0.0) {}
- Complex::Complex(const double real, const double imaginary) : re(real), im(imaginary) {}
- std::istream& operator>>(std::istream& is, Complex& rhs) {
- return rhs.read(is);
- }
- std::ostream& operator<<(std::ostream& os, const Complex& rhs) {
- return rhs.write(os);
- }
- std::istream& Complex::read(std::istream& is) {
- char lbrace;
- double real;
- char c;
- double imaginary;
- char rbrace;
- is >> lbrace >> re >> c >> im >> rbrace;
- if (is.good()) {
- if (Complex::lbrace == lbrace && Complex::separator == c && Complex::rbrace == rbrace) {
- re = real;
- im = imaginary;
- correct = true;
- } else {
- is.setstate(std::ios_base::failbit);
- }
- }
- return is;
- }
- std::ostream& Complex::write(std::ostream& os) const {
- os << '{' << re << ", " << im << '}';
- return os;
- }
- bool Complex::operator==(const Complex& rhs) const {
- return abs(re - rhs.re) < 2 * std::numeric_limits <double>::epsilon() && abs(im - rhs.im) < 2 * std::numeric_limits <double>::epsilon();
- }
- bool Complex::operator!=(const Complex& rhs) const {
- return !operator==(rhs);
- }
- Complex Complex::operator-() const noexcept {
- return Complex(-re, -im);
- }
- Complex& Complex::operator+=(const Complex& rhs) {
- re += rhs.re;
- im += rhs.im;
- return *this;
- }
- Complex& Complex::operator+=(const double rhs) {
- return operator+=(Complex(rhs));
- }
- Complex& Complex::operator-=(const Complex& rhs) {
- re -= rhs.re;
- im -= rhs.im;
- return *this;
- }
- Complex& Complex::operator-=(const double rhs) {
- return operator-=(Complex(rhs));
- }
- Complex& Complex::operator*=(const Complex& rhs) {
- double t = re;
- re = (re * rhs.re - im * rhs.im);
- im = (t * rhs.im + im * rhs.re);
- return *this;
- }
- Complex& Complex::operator*=(const double rhs) {
- return operator*=(Complex(rhs));
- }
- Complex& Complex::operator/=(const Complex& rhs) {
- if (pow(rhs.re, 2) + pow(rhs.im, 2) != 0) {
- double t = re;
- re = (re * rhs.re + im * rhs.im) / (pow(rhs.re, 2) + pow(rhs.im, 2));
- im = (im * rhs.re - t * rhs.im) / (pow(rhs.re, 2) + pow(rhs.im, 2));
- } else {
- std::cerr << "Error: dividing by 0. ";
- re = NAN;
- im = NAN;
- }
- return *this;
- }
- Complex& Complex::operator/=(const double rhs) {
- return operator/=(Complex(rhs));
- }
- // сложение
- Complex operator+(const Complex& lhs, const Complex& rhs) {
- return Complex(lhs) += rhs;
- }
- Complex operator+(const Complex& lhs, const double rhs) {
- return Complex(lhs) += rhs;
- }
- Complex operator+(const double lhs, const Complex& rhs) {
- return Complex(lhs) += rhs;
- }
- // вычитание
- Complex operator-(const Complex& lhs, const Complex& rhs) {
- return Complex(lhs) -= rhs;
- }
- Complex operator-(const Complex& lhs, const double rhs) {
- return Complex(lhs) -= rhs;
- }
- Complex operator-(const double lhs, const Complex& rhs) {
- return Complex(lhs) -= rhs;
- }
- // умножение
- Complex operator*(const Complex& lhs, const Complex& rhs) {
- return Complex(lhs) *= rhs;
- }
- Complex operator*(const Complex& lhs, const double rhs) {
- return Complex(lhs) *= rhs;
- }
- Complex operator*(const double lhs, const Complex& rhs) {
- return Complex(lhs) *= rhs;
- }
- // деление
- Complex operator/(const Complex& lhs, const Complex& rhs) {
- return Complex(lhs) /= rhs;
- }
- Complex operator/(const Complex& lhs, const double rhs) {
- return Complex(lhs) /= rhs;
- }
- Complex operator/(const double lhs, const Complex& rhs) {
- return Complex(lhs) /= rhs;
- }
Advertisement
Add Comment
Please, Sign In to add comment