#ifndef __COMPLEX__
#define __COMPLEX__
using std::ostream;
class Complex;
Complex&
__doapl(Complex*, const Complex&);
class Complex
{
public:
Complex(double r = 0, double i = 0) : re(r), im(i) {}
double real() const {return re;}
double imag() const {return im;}
Complex& operator += (const Complex&);
private:
double re, im;
friend Complex& __doapl(Complex*, const Complex&);
};
inline Complex&
__doapl(Complex* ths, const Complex& r) {
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline Complex&
Complex::operator += (const Complex& r) {
return __doapl(this, r);
}
ostream&
operator << (ostream& os, const Complex& c) {
return os<<"("<<c.real()<<","<<c.imag()<<")";
}
inline Complex
operator + (const Complex& c1, const Complex& c2) {
return Complex(c1.real() + c2.real(),
c1.imag() + c2.imag());
}
inline Complex
operator + (const Complex& c1, double d) {
return Complex(c1.real() + d, c1.imag());
}
inline Complex
operator + (double d, const Complex& c1) {
return Complex(c1.real() + d, c1.imag());
}
inline Complex
operator + (const Complex& c) {
return c;
}
inline Complex
operator - (const Complex& c) {
return Complex(-c.real(), -c.imag());
}
inline bool
operator == (const Complex& l, const Complex& r) {
return (l.real() == r.real()) && (l.imag() == r.imag());
}
inline bool
operator != (const Complex& l, const Complex& r) {
return (l.real() != r.real()) || (l.imag() != r.imag());
}
inline Complex
conj (const Complex& c) {
return Complex(c.real(), -c.imag());
}
#endif
#include <iostream>
#include "complex.h"
using namespace std;
int main() {
Complex c1(2,1);
Complex c2;
cout<<c1<<endl;
cout<<c2<<endl;
c2 = c1 + 5;
c2 = 7 + c1;
c2 = c1 + c2;
c2 += c1;
c2 += 3;
cout<<c2<<endl;
c2 = -c1;
cout<<(c1 == c2)<<endl;
cout<<(c1 != c2)<<endl;
cout<<conj(c2)<<endl;
return 0;
}