C++面向对象:实现complex类

//complex.h
#ifndef __MYCOMPLEX_H__
#define __MYCOMPLEX_H__

class complex;
complex& __doapl(complex*, const complex&); //友元可以在类外声明
complex& __doami(complex*, const complex&);
complex& __doaml(complex*, const complex&);


class complex{
public:
	complex(double r = 0, double i = 0) :re(r), im(i) {}
	complex(const complex& x) { re = x.real(); im = x.imag(); }

	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&);//TODO plus
	friend complex& __doami(complex*, const complex&);//TODO minus
	friend complex& __doaml(complex*, const complex&);//TODO multiply
};

//友元函数不是类的成员函数, 没有class
inline complex& __doapl(complex* ths, const complex& r){
	ths->re += r.re;
	ths->im += r.im;
	return *ths;
}

inline complex& __doami(complex* ths, const complex& r){
	ths->re -= r.re;
	ths->im -= r.im;
	return *ths;
}

//(a+bi)(c+di) = (ac-bd)+(ad+bc)i
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 complex& complex::operator += (const complex& r){
	return __doapl(this, r);
}

inline complex& complex::operator -= (const complex& r){
	return __doami(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){ //区分+=, 不用写在public里, 要考虑多种情况
	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));
}

//(a+bi)(c+di) = (ac-bd)+(ad+bc)i
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));
}

inline 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_H__

  

//complex.cpp
#include<iostream>
#include"complex.h"

using namespace std;

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

int main(){
	complex c1(3, 2);
	complex c2(5, 0);
	cout << c1 << endl;
	cout << c2 << endl;

	complex c3;
	complex c4(2);
	complex c31(c3);
	complex c41(c4);
	cout << c3 << " " << c4 << endl;
	cout << c31 << " " << c41 << endl;

	
	complex c5(7, 3);
	cout << c1 + c3 << endl;

	complex c6 = c1 * c5;
	cout << c5 << " " << c6 << " " << -c6 << endl;
	
	cout << c6 - 2 << endl;
	cout << c6 - c1 << endl;
	cout << c6 * 9 << endl;
	cout << c6 / 2 << endl;
	
	cout << conj(c6) << endl;
	cout << norm(c6) << endl;
	cout << polar(10, 4) << endl;

	cout << (c1 += c2) << endl;

	cout << (c1 == c2) << endl;
	cout << (c1 != c2) << endl;
	cout << +c2 << endl;
	cout << -c2 << endl;

	cout << (c2 - 2) << endl;
	cout << (5 + c2) << endl;

	return 0;
}

  执行结果:

(3, 2i)
(5, 0i)
(0, 0i) (2, 0i)
(0, 0i) (2, 0i)
(3, 2i)
(7, 3i) (15, 23i) (-15, -23i)
(13, 23i)
(12, 21i)
(135, 207i)
(7.5, 11.5i)
(15, -23i)
754
(-6.53644, -7.56802i)
(8, 2i)
0
1
(5, 0i)
(-5, -0i)
(3, 0i)
(10, 0i)

  

posted @ 2023-02-21 11:17  karinto  阅读(50)  评论(0)    收藏  举报