复数的四则运算实现(运算符重载)
#include <iostream>
using namespace std;
class complex
{
public:
complex(double real, double image) : m_real(real), m_image(image) {}
complex()
{
m_real = 0.0;
m_image = 0.0;
}
void display();
complex operator+=(const complex &A);
complex operator-=(const complex &A);
complex operator*=(const complex &A);
complex operator/=(const complex &A);
complex &operator++();
complex operator++(int);
complex &operator--();
complex operator--(int);
operator double() { return m_real; }
operator int() { return m_real; }
friend complex operator+(const complex &A, const complex &B);
friend complex operator-(const complex &A, const complex &B);
friend complex operator*(const complex &A, const complex &B);
friend complex operator/(const complex &A, const complex &B);
friend istream &operator>>(istream &in, complex &a);
friend ostream &operator<<(ostream &in, const complex &a);
private:
double m_real;
double m_image;
};
complex complex::operator+=(const complex &A)
{
this->m_real = this->m_real + A.m_real;
this->m_image = this->m_image + A.m_image;
return *this;
}
complex complex::operator-=(const complex &A)
{
this->m_real = this->m_real - A.m_real;
this->m_image = this->m_image - A.m_image;
return *this;
}
complex complex::operator*=(const complex &A)
{
this->m_real = this->m_real * A.m_real - this->m_image * A.m_image;
this->m_image = this->m_image * A.m_real + this->m_real * A.m_image;
return *this;
}
complex complex::operator/=(const complex &A)
{
this->m_real = (this->m_real * A.m_real + this->m_image * A.m_image) / (A.m_real * A.m_real + A.m_image * A.m_image);
this->m_image = (this->m_image * A.m_real - this->m_real * A.m_image) / (A.m_real * A.m_real + A.m_image * A.m_image);
return *this;
}
complex operator+(const complex &A, const complex &B)
{
complex C;
C.m_real = A.m_real + B.m_real;
C.m_image = A.m_image + B.m_image;
return C;
}
complex operator-(const complex &A, const complex &B)
{
complex C;
C.m_real = A.m_real - B.m_real;
C.m_image = A.m_image - B.m_image;
return C;
}
complex operator*(const complex &A, const complex &B)
{
complex C;
C.m_real = A.m_real * B.m_real - A.m_image * B.m_image;
C.m_image = A.m_image * B.m_real + A.m_real * B.m_image;
return C;
}
complex operator/(const complex &A, const complex &B)
{
complex C;
C.m_real = (A.m_real * B.m_real + A.m_image * B.m_image) / (B.m_real * B.m_real + B.m_image * B.m_image);
C.m_image = (A.m_image * B.m_real - A.m_real * B.m_image) / (B.m_real * B.m_real + B.m_image * B.m_image);
return C;
}
complex &complex::operator++()
{
this->m_real++;
this->m_image++;
return *this;
}
complex complex::operator++(int)
{
complex temp = *this;
this->m_real++;
this->m_image++;
return temp;
}
complex &complex::operator--()
{
this->m_real--;
this->m_image--;
return *this;
}
complex complex::operator--(int)
{
complex temp = *this;
this->m_real--;
this->m_image--;
return temp;
}
istream &operator>>(istream &in, complex &a)
{
in >> a.m_real >> a.m_image;
return in;
}
ostream &operator<<(ostream &out, const complex &a)
{
out << a.m_real << "+" << a.m_image << "i";
return out;
}
void complex::display()
{
cout << m_real << "+" << m_image << "i" << endl;
}
int main()
{
complex A(1, 2);
cout << A << endl;
complex B(2.3, 3.4);
cout << B << endl;
complex d;
cin >> d;
cout << ++d << endl;
cout << d++ << endl;
cout << d << endl;
cout << --d << endl;
cout << d-- << endl;
cout << d << endl;
int a = A;
cout << a << endl;
return 0;
}