您的位置:首页 > 编程语言 > C语言/C++

博览网_C++_第一周_C++ complex类的实现(我的第一个c++程序)

2017-04-30 16:14 281 查看
如果喜欢可以关注我

本文是我真正意义上第一个c++程序,在此期间查阅了大量的资料,实现了一个complex类,实现了类模板,水平有限,如有错误请私信我。

本程序实现的功能:

1. +  -  *  /                              重载函数

2. +=  -=  *=  /=                      重载函数

3. 赋值重载函数

4.判断是否相等,是否不等的重载函数

5.取正,取负的重载函数

6.get_real ,get_imag  成员函数(成员函数互为友元)

7.友元函数,this指针的使用。

8.complex类的显示

因为程序比较简单,我都做了注释,就不详细展开,如有问题请私信我。

/* 本程序的注释代表仅代表个人理解,不一定完全正确,全是我亲自敲上去的,如有错误请联系我。 */

#ifndef __COMPLEX_B_H__
#define __COMPLEX_B_H__

template<typename T> //定义模板 typename is T

class complex /* 复数类 */
{
public:
complex(T r = 0, T i =0) : re(r), im(i)
{
;
}
complex& operator += (const complex&); //成员函数
complex& operator -= (const complex&);
complex& operator *= (const complex&);
complex& operator /= (const complex&);
complex& operator = (const complex&); //赋值运算符重载函数
T real() const { return re; }
T imag() const { return im; }

private:
T re, im;

friend complex& __doadd(complex *, const complex&);
friend complex& __dosub(complex *, const complex&);
friend complex& __domul(complex *, const complex&);
friend complex& __dodiv(complex *, const complex&);
friend complex& __doequ(complex*, const complex&); //赋值实际运算函数

};

/************ 全局函数声明区 ***************/

double real(const complex<double>& x); //取对象的实部(全局函数),不用在类外专门定义传参数
double imag(const complex<double>& x); //取对象的虚部(全局函数),不用在类外专门定义传参数
inline complex<double>& __doequ(complex<double>* ths, const complex<double>& r); //赋值实际运算函数

/**************************************************************************************************************************************************************/

/************ += -= *= /= ***************************************************************************************************************************/
/* 复数 += 的实际运算函数 */
inline complex<double>&
__doadd(complex<double>* ths, const complex<double>& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}

/* 函数名 ooerator+= 运算符重载 */
inline complex<double>&
complex<double>::operator +=(const complex<double>& r)
{
return __doadd(this, r);
}

/* 复数 -= 的实际运算函数 */
inline complex<double>&
__dosub(complex<double>* ths, const complex<double>& r)
{
ths->re -= r.re;
ths->im -= r.im;
return *ths;
}

/* 函数名 ooerator-= */
inline complex<double>&
complex<double>::operator -=(const complex<double>& r)
{
return __dosub(this, r);
}

/* 复数 *= 的实际运算函数 */
inline complex<double>&
__domul(complex<double>* ths, const complex<double>& r)
{
ths->re *= r.re;
ths->im *= r.im;
return *ths;
}

/* 函数名 ooerator*= */
inline complex<double>&
complex<double>::operator *=(const complex<double>& r)
{
return __domul(this, r);
}

/* 复数 /= 的实际运算函数 */
inline complex<double>&
__dodiv(complex<double>* ths, const complex<double>& r)
{
ths->re /= r.re;
ths->im /= r.im;
return *ths;
}

/* 函数名 ooerator/= */
inline complex<double>&
complex<double>::operator/=(const complex<double>& r)
{
return __dodiv(this, r);
}

/* 复数 赋值(=) 实际运算函数 */
inline complex<double>&
__doequ(complex<double>* ths, const complex<double>& r)
{
ths->re = r.re;
ths->im = r.im;
return *ths;
}

/* 函数名 ooerator= 复数赋值(=) */
inline complex<double>&
complex<double>::operator=(const complex<double>& r)
{
return __doequ(this, r);
}

/**************************************************************************************************************************************************************/

/* 函数名 ooerator+(复数,复数) */
inline complex<double>
operator+(const complex<double>& x, const complex<double>& y)
{
return complex<double> (real(x) + real(y), imag(x) + imag(y));
}

/* 函数名 ooerator+(复数,double) */
inline complex<double>
operator+(const complex<double> x, double y)
{
return complex<double>(real(x) + y, imag(x));
}

/* 函数名 ooerator+(double,复数) */
inline complex<double>
operator+(double x, const complex<double> y)
{
return complex<double>(x + real(y), imag(y));
}

/* 函数名 ooerator-(复数,复数) */
inline complex<double>
operator-(const complex<double> x, const complex<double> y)
{
return complex<double>(real(x) - real(y), imag(x) - imag(y));
}

/* 函数名 ooerator-(复数,double) */
inline complex<double>
operator-(const complex<double> x, double y)
{
return complex<double>(real(x) - y, imag(x));
}

/* 函数名 ooerator-(double,复数) */
inline complex<double>
operator-(double x, const complex<double> y)
{
return complex<double>(x - real(y), imag(y));
}

/* 函数名 ooerator*(复数,复数) */
inline complex<double>
operator*(const complex<double> x, const complex<double> y)
{
return complex<double>(real(x) * real(y), imag(x) * imag(y));
}

/* 函数名 ooerator*(复数,double) */
inline complex<double>
operator*(const complex<double> x, double y)
{
return complex<double>(real(x) * y, imag(x));
}

/* 函数名 ooerator*(double,复数) */
inline complex<double>
operator*(double x, const complex<double> y)
{
return complex<double>(x * real(y), imag(y));
}

/* 函数名 ooerator/(复数,复数) */
inline complex<double>
operator/(const complex<double> x, const complex<double> y)
{
return complex<double>(real(x) / real(y), imag(x) / imag(y));
}

/* 函数名 ooerator/(复数,double) */
inline complex<double>
operator/(const complex<double> x, double y)
{
return complex<double>(real(x) / y, imag(x));
}

/* 函数名 ooerator/(double,复数) */
inline complex<double>
operator/(double x, const complex<double> y)
{
return complex<double>(x / real(y), imag(y));
}

/* 函数名 ooerator==(复数,复数) 判断两个复数是否相等 重载 */
inline bool
operator==(const complex<double>& x, const complex<double>& y)
{
return real(x) == real(y) && imag(x) == imag(y);
}

/* 函数名 ooerator==(复数,double) 判断两个复数是否相等 重载 */
inline bool
operator==(const complex<double>& x, double y)
{
return real(x) == y && imag(x) == 0;
}

/* 函数名 ooerator==(double,复数) 判断两个复数是否相等 重载 */
inline bool
operator==(double x
4000
, const complex<double>& y)
{
return x == real(y) && imag(y) == 0;
}

/* 函数名 ooerator!=(复数,复数) 判断两个复数是否不相等 重载 */
inline bool
operator != (const complex<double>& x, const complex<double>& y)
{
return real(x) != real(y) || imag(x) != imag(y);
}

/* 函数名 ooerator!=(复数,double) 判断两个复数是否不相等 重载 */
inline bool
operator != (const complex<double>& x, double y)
{
return real(x) != y || imag(x) != 0;
}

/* 函数名 ooerator!=(double,复数) 判断两个复数是否相等 重载 */
inline bool operator != (double x, const complex<double>& y)
{
return x != real(y) || imag(y) != 0;
}

/* 函数名 ooerator+(复数) +正数 */
inline complex<double>
operator+ (const complex<double>& x)
{
return x;
}

/* 函数名 ooerator-(复数) -复数 */
inline complex<double>
operator- (const complex<double>& x)
{
return complex<double>(-real(x), -imag(x));
}

inline double real(const complex<double>& x) //取对象的实部(全局函数),不用在类外专门定义传参数
{
return x.real();
}

inline double imag(const complex<double>& x) //取对象的虚部(全局函数),不用在类外专门定义传参数
{
return x.imag();
}

#endif
/*	本程序的注释代表仅代表个人理解,不一定完全正确,全是我亲自敲上去的,如有错误请联系我。  	*/

#include<iostream>
#include"complex_B.h"
#include<windows.h>

using namespace std;

/*	对 cout<< complex 类型的重载    ostream 是 cout的类	*/
ostream& operator<<(ostream& os, const complex<double>& x)
{

return os << '(' << real(x) << ',' << imag(x) << ')';
//return os << '(' << x.real() << ',' << x.imag() << ')';	//直接调用成员函数 real() 和 imag()  不必在
}

int main(void)
{
complex<double> c1(2, 1);
complex<double> c2(4, 0);
complex<double> c3(-1, 1);
complex<double> c4;		//c4未初始化(构造函数会赋初值0)

cout << "c1=" << c1 << endl;
cout << "c2=" << c2 << endl;
cout << "c3=" << c3 << endl;
cout << "c4=" << c4 << endl << endl;

cout << "c1+c2" << c1 + c2 << endl;
cout << "c1+4" << c1 + 4 << endl;
cout << "2+c2" << 2 + c2 << endl << endl;

cout << "c1-c2" << c1 - c2 << endl;
cout << "c1-4" << c1 - 4 << endl;
cout << "2-c2" << 2 - c2 << endl << endl;

cout << "c1*c2" << c1 * c2 << endl;
cout << "c1*4" << c1 * 4 << endl;
cout << "2*c2" << 2 * c2 << endl << endl;

cout << "c1/c2" << c1 / c2 << endl;
cout << "c1/4" << c1 / 4 << endl;
cout << "2/c2" << 2 / c2 << endl << endl;

cout << "+c3" << +c3 << endl;	//测试运算符  +
cout << "-c3" << -c3 << endl << endl;	//测试运算符  -

c3 = complex<double>(1, -1);	// 运算符 =   (赋值)
cout << "c3=(1,-1)" << endl;
cout << "c3=" << c3 << endl;	// 测试运算符 = (赋值)		复数变量=临时复数
c3 = c4;
cout << "c3=c4" << endl;
cout << "c3=" << c3 << endl << endl;	// 测试运算符 = (赋值)		复数变量1=复数变量2

//cout << "(c1 += c2)="  << (c1 += c2) << endl;
/*
c1 += c2
相当于c1.operator+=(c2)
也就是c1调用的+=,所以this指向c1
*/
//cout << "(c1 -= c2)="   << (c1 -= c2) << endl;
//cout << "(c1 *= c2)=" << (c1 *= c2) << endl;
cout << "(c1 /= c2)=" << (c1 /= c2) << endl << endl;

cout << "c1==c2  =" << (c1 == c2) << endl;	// 测试运算符 ==  (两个复数是否相等判断)  复数变量1==复数变量2
cout << "c1==2  =" << (c1 == 2) << endl;	// 测试运算符 ==  (两个复数是否相等判断)	复数变量==实数
cout << "2==c2  =" << (4 == c2) << endl;	// 测试运算符 ==  (两个复数是否相等判断)	复数变量==实数

system("pause");

return 0;
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息