C++学习记录-复数类

发布于 2020-07-30  100 次阅读


实现复数类

complex_test.cpp

#include <iostream>
#include "complex.h"
#include<iomanip>

using namespace std;

ostream&
operator << (ostream& os, const complex& x)
{
  return os << '(' << real (x) << ',' << imag (x) << ')';
}

int main()
{
  complex c1(2, 1);
  complex c2(4, 0);

  cout <<setw(20)<<"c1 : "<< c1 << endl;
  cout <<setw(20)<<"c2 : "<< c2 << endl;
  
  cout <<setw(20)<<"c1 + c2 : "<< c1+c2 << endl;
  cout <<setw(20)<<"c1 - c2 : "<< c1-c2 << endl;
  cout <<setw(20)<<"c1 * c2 : "<< c1*c2 << endl;
  cout <<setw(20)<<"c1 / 2 : "<< c1 / 2 << endl;
  
  cout <<setw(20)<<"conj(c1) : "<< conj(c1) << endl;
  cout <<setw(20)<<"norm(c1) : "<< norm(c1) << endl;
  cout <<setw(20)<<"polar(10,4) : "<< polar(10,4) << endl;
  
  cout <<setw(20)<<"(c1 += c2) : "<< (c1 += c2) << endl;
  
  cout <<setw(20)<<"(c1 == c2) : "<< (c1 == c2) << endl;
  cout <<setw(20)<<"(c1 != c2) : "<< (c1 != c2) << endl;
  cout <<setw(20)<<"+c2 : "<< +c2 << endl;
  cout <<setw(20)<<"-c2 : "<< -c2 << endl;
  
  cout <<setw(20)<<"(c2 - 2) : "<< (c2 - 2) << endl;
  cout <<setw(20)<<"(5 + c2) : "<< (5 + c2) << endl;
  
  return 0;
}

complex.h

#ifndef __MYCOMPLEX__
#define __MYCOMPLEX__

class complex;
complex&
    _doapl(complex* ths, const complex& r);
complex&
    _doami(complex* ths, const complex& r);
complex&
    _doaml(complex* ths, const complex& r);


class complex{
public:
    complex(double r = 0, double i = 0) : re(r), im(i){}
    complex& operator += (const complex&);
    complex& operator -= (const complex&);
    complex& operator *= (const complex&);
    complex& operator /= (const complex&);
    double real() const  { return re; };
    double imag() const  { return im; }

private:
    double re, im;

    friend complex& __doapl(complex * , const complex& r);
    friend complex& __doami(complex * , const complex& r);
    friend complex& __doaml(complex * , const complex& r);
};// 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);
}

// 减
inline complex&
__doami(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);
}

// 乘
inline complex&
__doaml(complex* ths, const complex& r){
    double f = ths->re * r.re - ths->im * r.im;
    ths->im = ths->re * r.im + ths->im * r.re;
    ths->re = f;
    return *ths;
}

inline complex&
complex::operator *= (const complex& r){
    return __doapl(this, r);
}

inline double 
imag (const complex& x){
    return x.imag();
}

inline double 
real (const complex& x){
    return x.real();
}

// 加
inline complex
operator + (const complex& x, const complex& y){
    return complex(real(x) + real(y), imag(x) + imag(y));
}
inline complex
operator + (const complex& x, double y){
    return complex(real(x) + y, imag(x) + y);
}
inline complex
operator + (double x, const complex& y){
    return complex(x + real(y), x + imag(y));
}
// 减
inline complex
operator - (const complex& x, const complex& y){
    return complex(real(x) - real(y), imag(x) - imag(y));
}
inline complex
operator - (const complex& x, double y){
    return complex(real(x) - y, imag(x) - y);
}
inline complex
operator - (double x, const complex& y){
    return complex(x - real(y), x - imag(y));
}
// 乘
inline complex
operator * (const complex& x, const complex& y){
    return complex(real(x) * real(y), imag(x) * imag(y));
}
inline complex
operator * (const complex& x, double y){
    return complex(real(x) * y, imag(x) * y);
}
inline complex
operator * (double x, const complex& y){
    return complex(x * real(y), x * imag(y));
}
// 除
complex
operator / (const complex& x, double y){
    return complex(real(x) / y, imag(x) / y);
}

// 正
inline complex
operator + (const complex& x){
    return x;
}

// 负
inline complex
operator - (const complex& x){
    return complex(-real(x), -imag(x));
}

inline bool
operator == (const complex& x, const complex& y){
    return real(x) == real(y) && imag(x) == imag(y);
}

inline bool
operator == (const complex& x, double y){
    return real(x) == y && imag(x) == 0;
}

inline bool
operator == (double x, const complex& y){
    return x == real(y) && 0 == imag(y);
}

inline bool
operator != (const complex& x, const complex& y)
{
  return real (x) != real (y) || imag (x) != imag (y);
}

inline bool
operator != (const complex& x, double y)
{
  return real (x) != y || imag (x) != 0;
}

inline bool
operator != (double x, const complex& y)
{
  return x != real (y) || imag (y) != 0;
}

#include<cmath>

inline complex
polar(double r, double t){
    return complex(r * cos(t), r * sin(t));
}

inline complex
conj(const complex& x){
    return complex(real(x), -imag(x));
}

inline complex
norm(const complex& x){
    return real(x) * real(x) + imag(x) * imag(x);
}

#endif /* __MYCOMPLEX__ */