/
oop_nust_misis
/
complex-forthang
Обзор
Документация
Войти
/
oop_nust_misis
/
complex-forthang
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
patch
src/library.cpp
74 строки
2 KB
forthang
update src/library.cpp
12 апр 2025, 23:26
12 апр 2025, 23:26
0da9b35
Код
Авторство
О чём код?
#include "library.hpp" #include <stdexcept> #include <istream> #include <ostream> #include <limits> namespace template_library { Complex::Complex() : m_real(0.0), m_imag(0.0) {} Complex::Complex(double real, double imag) : m_real(real), m_imag(imag) {} double Complex::real() const { return m_real; } double Complex::imag() const { return m_imag; } Complex Complex::operator+(const Complex& other) const { return Complex(m_real + other.m_real, m_imag + other.m_imag); } Complex Complex::operator-(const Complex& other) const { return Complex(m_real - other.m_real, m_imag - other.m_imag); } Complex Complex::operator*(const Complex& other) const { return Complex( m_real * other.m_real - m_imag * other.m_imag, m_real * other.m_imag + m_imag * other.m_real ); } Complex Complex::operator/(const Complex& other) const { double denominator = other.m_real * other.m_real + other.m_imag * other.m_imag; if (denominator == 0) { throw std::invalid_argument("Complex division by zero"); } return Complex( (m_real * other.m_real + m_imag * other.m_imag) / denominator, (m_imag * other.m_real - m_real * other.m_imag) / denominator ); } Complex operator*(double m, const Complex& other) { return Complex(m * other.m_real, m * other.m_imag); } Complex operator*(const Complex& other, double m) { return Complex(m * other.m_real, m * other.m_imag); } std::ostream& operator<<(std::ostream& os, const Complex& c) { os << '(' << c.m_real << ", " << c.m_imag << ')'; return os; } std::istream& operator>>(std::istream& is, Complex& c) { char ch; is >> ch; if (ch != '(') { is.setstate(std::ios::failbit); return is; } is >> c.m_real >> ch; if (ch != ',') { is.setstate(std::ios::failbit); return is; } is >> c.m_imag >> ch; if (ch != ')') { is.setstate(std::ios::failbit); } return is; } }