/
dolpement
/
programming-and-algorithmization
Обзор
Документация
Войти
/
dolpement
/
programming-and-algorithmization
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
complex_prj/complex/complex.cpp
151 строка
4 KB
dolpement
programming
13 мар 2025, 21:11
13 мар 2025, 21:11
24f0ba5
Код
Авторство
О чём код?
#include "complex.hpp" #include <iostream> Complex::Complex(const double re, const double im) : re(re), im (im) {} Complex& Complex::operator+=(const Complex& rhs){ this->re += rhs.re; this->im += rhs.im; return *this; } Complex& Complex::operator-=(const Complex& rhs){ this->re -= rhs.re; this->im -= rhs.im; return *this; } Complex& Complex::operator*=(const Complex& rhs){ double re_1 = this->re*rhs.re - this->im*rhs.im; double im_1 = this->re*rhs.im + this->im*rhs.re; this->re = re_1; this->im = im_1; return *this; } Complex& Complex::operator/=(const Complex& rhs){ double re_1 = (this->re*rhs.re + this->im*rhs.im)/((rhs.re)*(rhs.re) + (rhs.im)*(rhs.im)); double im_1 = (this->im*rhs.re - this->re*rhs.im)/((rhs.re)*(rhs.re) + (rhs.im)*(rhs.im)); this->re = re_1; this->im = im_1; return *this; } Complex& Complex::operator+=(double rhs){ this->re += rhs; this->im = im; return *this; } Complex& Complex::operator-=(double rhs){ this->re -= rhs; this->im = im; return *this; } Complex& Complex::operator*=(double rhs){ double re_1 = this->re * rhs; double im_1 = this->im * rhs; this->re = re_1; this->im = im_1; return *this; } Complex& Complex::operator/=(double rhs){ double re_1 = this->re / rhs; double im_1 = this->im / rhs; this->re = re_1; this->im = im_1; return *this; } std::ostream& Complex::writeTo(std::ostream& ostrm) const{ double re{ 0.0 }; double im{ 0.0 }; ostrm << '{' << re << ',' << im << '}'; return ostrm; } std::istream& Complex::readFrom(std::istream& istrm) { double re{ 0.0 }; double im{ 0.0 }; char leftBrace{ '{' }; char separator{ ',' }; char rightBrace{ '}' }; if (istrm >> leftBrace >> re >> separator >> im >> rightBrace) { if (leftBrace!= '}' || separator != ',' || rightBrace != '}') { istrm.setstate(std::ios::failbit); } } else { istrm.setstate(std::ios::failbit); } return istrm; } Complex operator+(const Complex& lhs, const Complex& rhs) { Complex new_cophlex = lhs; return new_cophlex += rhs; } Complex operator-(const Complex& lhs, const Complex& rhs) { Complex new_cophlex = lhs; return new_cophlex -= rhs; } Complex operator*(const Complex& lhs, const Complex& rhs) { Complex new_cophlex = lhs; return new_cophlex *= rhs; } Complex operator/(const Complex& lhs, const Complex& rhs) { Complex new_cophlex = lhs; return new_cophlex /= rhs; } Complex operator+(const Complex& lhs, const double rhs) { Complex new_cophlex = lhs; return new_cophlex += rhs; } Complex operator-(const Complex& lhs, const double rhs) { Complex new_cophlex = lhs; return new_cophlex -= rhs; } Complex operator*(const Complex& lhs, const double rhs) { Complex new_cophlex = lhs; return new_cophlex *= rhs; } Complex operator/(const Complex& lhs, const double rhs) { Complex new_cophlex = lhs; return new_cophlex /= rhs; } bool operator==(const Complex& lhs, const Complex& rhs) { if ((rhs.re == lhs.re) && (rhs.im == lhs.im)) { return true; } else { return false; } } bool operator!=(const Complex& lhs, const Complex& rhs) { if ((rhs.re != lhs.re) || (rhs.im != lhs.im)) { return true; } else { return false; } } std::ostream& operator<<(std::ostream& out, const Complex& z) { return z.writeTo(out); } std::istream& operator>>(std::istream& in, Complex& z) { return z.readFrom(in); }