用c++写一个复数类
我是c++初学者,之前在看侯捷的c++视频,里面有几个练习我觉得拿来练一练会很有用。第一个就是写一个复数类,以下是我的代码,我首先会呈现我的代码,在后面会呈现标准库的代码:
我的实现
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class Complex {
public:
Complex(double r = 0, double i = 0) : re(r), im(i) { }
Complex(const Complex &c) : re(c.re), im(c.im) { }
Complex& operator=(const Complex &);
~Complex() { }
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;
};
Complex& Complex::operator=(const Complex &c)
{
re = c.re;
im = c.im;
return *this;
}
double real(const Complex& c)
{
return c.real();
}
double imag(const Complex& c)
{
return c.imag();
}
inline
Complex& Complex::operator+=(const Complex& rhs)
{
re += rhs.re;
im += rhs.im;
return *this;
}
inline
Complex operator+(const Complex& lhs, const Complex& rhs)
{
return Complex(real(lhs) + real(rhs), imag(lhs) + imag(rhs));
}
inline
Complex operator+(const Complex& lhs, double rhs)
{
return Complex(real(rhs) + rhs, imag(rhs));
}
inline
Complex operator+(double lhs, const Complex& rhs)
{
return Complex(real(rhs) + lhs, imag(rhs));
}
inline
Complex& Complex::operator-=(const Complex& rhs)
{
re -= rhs.re;
im -= rhs.im;
return *this;
}
inline
Complex operator-(const Complex& lhs, const Complex& rhs)
{
return Complex(real(lhs) - real(rhs), imag(lhs) - imag(rhs));
}
inline
Complex operator-(const Complex& lhs, double rhs)
{
return Complex(real(lhs) - rhs, imag(lhs));
}
inline
Complex operator-=(double lhs, const Complex& rhs)
{
return Complex(lhs - real(rhs), imag(rhs));
}
inline
Complex& Complex::operator*= (const Complex& rhs)
{
double r = re * rhs.re - im * rhs.im;
double i = re * rhs.im + im * rhs.re;
re = r;
im = i;
return *this;
}
inline
Complex operator* (const Complex& lhs, const Complex& rhs)
{
double r = real(lhs) * real(rhs) - imag(lhs) * imag(rhs);
double i = real(lhs) * imag(rhs) + imag(lhs) * real(rhs);
return Complex(r, i);
}
inline
Complex operator* (const Complex& lhs, double rhs)
{
double r = real(lhs) * rhs;
double i = imag(lhs) * rhs;
return Complex(r, i);
}
inline
Complex operator* (double lhs, const Complex& rhs)
{
double r = lhs * real(rhs);
double i = lhs * imag(rhs);
return Complex(r, i);
}
inline
Complex& Complex::operator/= (const Complex& rhs)
{
double r = (re * rhs.re + im * rhs.im) / (rhs.re * rhs.re + rhs.im * rhs.im);
double i = (im * rhs.re - re * rhs.im) / (rhs.re * rhs.re + rhs.im * rhs.im);
re = r;
im = i;
return *this;
}
inline
Complex operator/ (const Complex& lhs, const Complex& rhs)
{
double r = (real(lhs) * real(rhs) + imag(lhs) * imag(rhs)) / (real(rhs) * real(rhs) + imag(rhs) * imag(rhs));
double i = (imag(lhs) * real(rhs) - real(lhs) * imag(rhs)) / (real(rhs) * real(rhs) + imag(rhs) * imag(rhs));
return Complex(r, i);
}
inline
Complex operator/ (const Complex& lhs, double rhs)
{
double r = real(lhs) / rhs;
double i = imag(lhs) / rhs;
return Complex(r, i);
}
inline
Complex operator+(const Complex& c)
{
return c;
}
inline
Complex operator-(const Complex& c)
{
return Complex(-real(c), -imag(c));
}
inline
bool operator == (const Complex& lhs, const Complex& rhs)
{
return real(lhs) == real(rhs) && imag(lhs) == imag(rhs);
}
inline
bool operator == (const Complex& lhs, double rhs)
{
return real(lhs) == rhs && imag(lhs) == 0;
}
inline
bool operator == (double lhs, Complex& rhs)
{
return lhs == real(rhs) && imag(rhs) == 0;
}
inline
bool operator != (const Complex& lhs, const Complex& rhs)
{
return real(lhs) != real(rhs) || imag(lhs) != imag(rhs);
}
inline
bool operator != (const Complex& lhs, double rhs)
{
return real(lhs) != rhs || imag(lhs) != 0;
}
inline
bool operator !=(double lhs, const Complex& rhs)
{
return lhs != real(rhs) || imag(rhs) != 0;
}
inline
Complex conj(const Complex& c)
{
return Complex(real(c), -imag(c));
}
#endif
标准库实现
#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&);
friend complex& __doami (complex *, const complex&);
friend complex& __doaml (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);
}
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 __doami (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 __doaml (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));
}
inline complex
operator + (double x, const complex& y)
{
return complex (x + real (y), 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));
}
inline complex
operator - (double x, const complex& y)
{
return complex (x - real (y), - imag (y));
}
inline complex
operator * (const complex& x, const complex& y)
{
return complex (real (x) * real (y) - imag (x) * imag (y),
real (x) * imag (y) + imag (x) * real (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) && imag (y) == 0;
}
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 double
norm (const complex& x)
{
return real (x) * real (x) + imag (x) * imag (x);
}
#endif //__MYCOMPLEX__
总结
- 类对象的初始化尽量在构造函数初始化列表中完成。
- 如果成员函数不会改变数据成员的值,则成员函数要修饰成const。
- 以pass by reference to const替代pass by value。