/
maryryryp
/
complex-maryryryp
Обзор
Документация
Войти
/
maryryryp
/
complex-maryryryp
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/library.cpp
64 строки
2 KB
maryryryp
src/library.cpp
29 мар 2025, 20:26
29 мар 2025, 20:26
21ed3e4
Код
Авторство
О чём код?
#include "library.hpp" #include <stdexcept> #include <sstream> 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 { double real = m_real * other.m_real - m_imag * other.m_imag; double imag = m_real * other.m_imag + m_imag * other.m_real; return Complex(real, imag); } Complex Complex::operator/(const Complex& other) const { double denom = other.m_real * other.m_real + other.m_imag * other.m_imag; if (denom == 0.0) { throw std::runtime_error("Division by zero"); } double real = (m_real * other.m_real + m_imag * other.m_imag) / denom; double imag = (m_imag * other.m_real - m_real * other.m_imag) / denom; return Complex(real, imag); } 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(other.m_real * m, other.m_imag * m); } std::ostream& operator<<(std::ostream& os, const Complex& c) { if (c.m_real == 0.0 && c.m_imag == 0.0) os << "0.0"; else os << c.m_real << (c.m_imag >= 0 ? " + " : " - ") << std::abs(c.m_imag) << "i"; return os; } std::istream& operator>>(std::istream& is, Complex& c) { is >> c.m_real >> c.m_imag; return is; } }