From cc2c753993efd3f8cffa23a105e2a7a09efccf3a Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Sun, 21 Jun 2026 19:49:27 +0000 Subject: [PATCH] [Sync Iteration] cpp/complex-numbers/1 --- .../cpp/complex-numbers/1/complex_numbers.cpp | 49 +++++++++++++++++++ .../cpp/complex-numbers/1/complex_numbers.h | 30 ++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 solutions/cpp/complex-numbers/1/complex_numbers.cpp create mode 100644 solutions/cpp/complex-numbers/1/complex_numbers.h diff --git a/solutions/cpp/complex-numbers/1/complex_numbers.cpp b/solutions/cpp/complex-numbers/1/complex_numbers.cpp new file mode 100644 index 0000000..2d39e40 --- /dev/null +++ b/solutions/cpp/complex-numbers/1/complex_numbers.cpp @@ -0,0 +1,49 @@ +#include "complex_numbers.h" +#include + +using namespace std; + +namespace complex_numbers { + +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; } + +double Complex::abs() const { + return sqrt((m_real * m_real) + (m_imag * m_imag)); +} + +Complex Complex::conj() const { + return Complex(m_real, -m_imag); +} + +Complex Complex::exp() const { + double exp_real = std::exp(m_real); + return Complex(exp_real * cos(m_imag), exp_real * sin(m_imag)); +} + +Complex operator+(const Complex& lhs, const Complex& rhs) { + return Complex(lhs.m_real + rhs.m_real, lhs.m_imag + rhs.m_imag); +} + +Complex operator-(const Complex& lhs, const Complex& rhs) { + return Complex(lhs.m_real - rhs.m_real, lhs.m_imag - rhs.m_imag); +} + +Complex operator*(const Complex& lhs, const Complex& rhs) { + double r = (lhs.m_real * rhs.m_real) - (lhs.m_imag * rhs.m_imag); + double i = (lhs.m_imag * rhs.m_real) + (lhs.m_real * rhs.m_imag); + return Complex(r, i); +} + +Complex operator/(const Complex& lhs, const Complex& rhs) { + double denominator = (rhs.m_real * rhs.m_real) + (rhs.m_imag * rhs.m_imag); + + double r = ((lhs.m_real * rhs.m_real) + (lhs.m_imag * rhs.m_imag)) / denominator; + double i = ((lhs.m_imag * rhs.m_real) - (lhs.m_real * rhs.m_imag)) / denominator; + + return Complex(r, i); +} + +} diff --git a/solutions/cpp/complex-numbers/1/complex_numbers.h b/solutions/cpp/complex-numbers/1/complex_numbers.h new file mode 100644 index 0000000..ba63e71 --- /dev/null +++ b/solutions/cpp/complex-numbers/1/complex_numbers.h @@ -0,0 +1,30 @@ +#pragma once + +namespace complex_numbers { + +class Complex { +private: + double m_real; + double m_imag; + +public: + // Constructor + Complex(double real = 0.0, double imag = 0.0); + + // Getters + double real() const; + double imag() const; + + // Métodos unarios + double abs() const; + Complex conj() const; + Complex exp() const; + + // Operadores Simétricos como funciones libres "friend" + friend Complex operator+(const Complex& lhs, const Complex& rhs); + friend Complex operator-(const Complex& lhs, const Complex& rhs); + friend Complex operator*(const Complex& lhs, const Complex& rhs); + friend Complex operator/(const Complex& lhs, const Complex& rhs); +}; + +}