c++実現複素クラス
主に演算子を再ロードして複数の基本演算を実現する練習です.
複素加算(リロードされた演算子:+、+=、前置++、後置++);
複素減算(リロードされた演算子:-、-=、前置--、後置--);
複素乗算(リロードされた演算子:*、*=、);
複素除算(リロードされた演算子:/、/=、);
コードは次のとおりです.
各関数の実装:
複素加算(リロードされた演算子:+、+=、前置++、後置++);
複素減算(リロードされた演算子:-、-=、前置--、後置--);
複素乗算(リロードされた演算子:*、*=、);
複素除算(リロードされた演算子:/、/=、);
コードは次のとおりです.
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(float real = 1, float imag = 1):_real(real), _imag(imag){}//
Complex(const Complex& complex);//
~Complex(){};//
Complex& operator=(const Complex& complex);//
bool operator==(const Complex& complex);
bool operator!=(const Complex& complex);
Complex operator+(const Complex& complex);
Complex& operator++();// ++
Complex operator++(int);// ++
Complex& operator+=(const Complex& complex);
Complex operator-(const Complex& complex);
Complex& operator-=(const Complex& complex);
Complex& operator--();// --
Complex operator--(int);// --
Complex operator*(const Complex& complex);
Complex& operator*=(const Complex& complex);
Complex operator/(const Complex& complex);
Complex& operator/=(const Complex& complex);
friend ostream& operator<<(ostream& output, const Complex& complex);//
private:
float _real;
float _imag;
};
各関数の実装:
Complex::Complex(const Complex& complex)//
{
_real = complex._real;
_imag = complex._imag;
}
Complex& Complex::operator=(const Complex& complex)//
{
cout << " " << endl;
if (this != &complex)
{
_real = complex._real;
_imag = complex._imag;
}
return *this;
}
ostream& operator<<(ostream& output, const Complex& complex)//
{
output << "(" << complex._real;
if (complex._imag >= 0)
{
output << "+";
}
output << complex._imag << "i)";
return output;
}
bool Complex::operator==(const Complex& complex)
{
return (_real == complex._real) && (_imag == complex._imag);
}
bool Complex::operator!=(const Complex& complex)
{
return !(*this==complex);
}
Complex Complex::operator+(const Complex& complex)
{
Complex c(*this);
c._real +=complex._real;
c._imag +=complex._imag;
return c;
}
Complex& Complex::operator++()
{
++_real;
return *this;
}
Complex Complex::operator++(int)
{
Complex c;
c._real=_real++;
return c;
}
Complex& Complex::operator+=(const Complex& complex)
{
_real += complex._real;
_imag += complex._imag;
return *this;
}
Complex Complex::operator-(const Complex& complex)
{
return Complex(_real - complex._real, _imag - complex._imag);
}
Complex& Complex::operator-=(const Complex& complex)
{
_real -= complex._real;
_imag -= complex._imag;
return *this;
}
Complex& Complex::operator--()
{
--_real;
return *this;
}
Complex Complex::operator--(int)
{
Complex c;
c._real=_real--;
return c;
}
Complex Complex::operator*(const Complex& complex)
{
Complex c;
c._real = _real*complex._real - _imag*complex._imag;
c._imag = _real*complex._imag + _imag*complex._real;
return c;
}
Complex& Complex::operator*=(const Complex& complex)
{
float real = _real;
_real = _real*complex._real - _imag*complex._imag;
_imag = _imag*complex._real + real*complex._imag;
return *this;
}
Complex Complex::operator/(const Complex& complex)
{
return Complex((_real*complex._real + _imag*complex._imag) /
(complex._real*complex._real + complex._imag*complex._imag),
(_imag*complex._real - _real*complex._imag)/
(complex._real*complex._real + complex._imag*complex._imag));
/*Complex c;
c._real = (_real*complex._real + _imag*complex._imag)*1.0 /
(complex._real*complex._real + complex._imag*complex._imag);
c._imag = (_imag*complex._real - _real*complex._imag)*1.0 /
(complex._real*complex._real + complex._imag*complex._imag);
return c;*/
}
Complex& Complex::operator/=(const Complex& complex)
{
float real = _real;
_real = (_real*complex._real + _imag*complex._imag)/
(complex._real*complex._real + complex._imag*complex._imag);
_imag = (_imag*complex._real - real*complex._imag) /
(complex._real*complex._real + complex._imag*complex._imag);
return *this;
}