/
ageevava
/
complex-ageevava2222
Обзор
Документация
Войти
/
ageevava
/
complex-ageevava2222
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/library.cpp
65 строк
2 KB
ageevava
update src/library.cpp
09 апр 2025, 00:39
09 апр 2025, 00:39
ba5acde
Код
Авторство
О чём код?
#include "library.hpp" #include <stdexcept> #include <cmath> #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 denominator = other.m_real * other.m_real + other.m_imag * other.m_imag; if (denominator == 0) { throw std::runtime_error("Division by zero"); } double real = (m_real * other.m_real + m_imag * other.m_imag) / denominator; double imag = (m_imag * other.m_real - m_real * other.m_imag) / denominator; 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(m * other.m_real, m * other.m_imag); } // Операторы ввода/вывода std::ostream& operator<<(std::ostream& os, const Complex& c) { if (c.m_imag == 0) { os << c.m_real; } 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; } } // namespace template_library