【C++】复数类的实现

复数类的实现:

    这个是以前学习的补全,记录一下吧。

    

    复数类本身概念是具备一个实部_real和虚部_image,然后实现复数的加减乘除,自加自减还有等于符号的重载。算是一个基本的联系吧。

    废话不多说,看看代码,很简单。

    Complex_class.h

#include <iostream>
#include <math.h>

using namespace std;


class Complex
{
private:
	double _real;
	double _imag;
public:
	Complex(double real = 0.0,double imag = 0.0);
	Complex(Complex &cur);
	friend ostream& operator << (ostream& output,Complex& c);
	friend istream& operator >> (istream& input,Complex& c);

	friend Complex operator+(const Complex& c1,const Complex& c2);
	friend Complex operator-(const Complex& c1,const Complex& c2);
	friend Complex operator*(const Complex& c1,const Complex& c2);
	friend Complex operator/(const Complex& c1,const Complex& c2);
	Complex& operator ++();    // 前置 ++
	Complex operator ++(int);  // 后置++
	Complex& operator --();   // 前置 -
	Complex operator --(int); // 后置-
	Complex& operator -=(const Complex& c );
	Complex& operator +=(const Complex& c );

	bool operator <(const Complex& c);
	bool operator >(const Complex& c);
};

complex.cpp

#include "Complex_class.h"



Complex::Complex(double real,double imag)
	{
		_real = real;
		_imag = imag;
	}
//输出运算符的重载。
ostream& operator <<(ostream& output,Complex& c)
{
	output<<"("<<c._real;
	if(c._imag  >= 0)
	{
		output<<"+"<<c._imag<<"i)";
	}
	else
	{
		output<<c._imag<<"i)";
	}
	return output;
}

Complex::Complex(Complex &cur)
{
	_real = cur._real;
	_real = cur._imag;
}
//输入运算符的重载。
istream& operator >>(istream& input,Complex& c)
{
	int a,b;  
    char sign,i;  
    do  
    {   
		cout<<"input a complex number(a+bi或a-bi):";  
        input>>a>>sign>>b>>i;  
    }  
    while(!((sign == '+'||sign == '-')&&i == 'i'));  
    c._real=a;  
    c._imag=(sign=='+')?b:-b;  
    return input; 
}
//复数相加,(a+bi)+(c+di)=(a+c)+(b+d)i;
Complex operator+(const Complex& c1,const Complex& c2)
{
	Complex resultComplex;
	resultComplex._imag = c1._imag + c2._imag;
	resultComplex._real = c1._real + c2._real;
	return resultComplex;
}
//复数相减,a+bi)-(c+di)=(a-c)+(b-d)i
Complex operator-(const Complex& c1,const Complex& c2)
{
	Complex resultComplex;
	resultComplex._imag = c1._imag - c2._imag;
	resultComplex._real = c1._real - c2._real;
	return resultComplex;
}
//复数相乘:(a+bi)・(c+di)=(ac-bd)+(bc+ad)i
Complex operator*(const Complex& c1,const Complex& c2)
{
	Complex resultComplex;
	resultComplex._real = (c1._real * c2._real) - (c1._imag * c2._imag);
	resultComplex._imag = (c1._imag * c2._real) + (c1._real * c2._imag);
	return resultComplex;
}
////复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i  
Complex operator/(const Complex& c1,const Complex& c2)
{
	Complex resultComplex;
	resultComplex._real=(c1._real*c2._real+c1._imag*c2._imag)/(c2._real*c2._real+c2._imag*c2._imag);  
    resultComplex._imag=(c1._imag*c2._real-c1._real*c2._imag)/(c2._real*c2._real+c2._imag*c2._imag);  
	return resultComplex;
}

Complex& Complex::operator ++()    // 前置 ++
{
	this->_imag++;
	this->_real++;
	return *this;
}

Complex Complex::operator ++(int)  // 后置++
{
	Complex before(this->_real,this->_imag);
	++*this;
	return before;
}
Complex& Complex::operator --()   // 前置 -
{
	this->_imag--;
	this->_real--;
	return *this;
}
Complex Complex::operator --(int) // 后置-
{
	Complex before(this->_real,this->_imag);
	--*this;
	return before;
}
Complex& Complex::operator -=(const Complex& c )
{
	*this = *this - c;
	return *this;
}
Complex& Complex::operator +=(const Complex& c )
{
	*this = *this + c;
	return *this;
}


bool Complex::operator <(const Complex& c)
{
	return (pow(_real,2)+pow(_imag,2))<(pow(c._real,2)+pow(c._imag,2))? true:false;
}
bool Complex::operator >(const Complex& c)
{
	return (pow(_real,2)+pow(_imag,2))>(pow(c._real,2)+pow(c._imag,2))? true:false;

}

一个复数类的实现就完成了。是不是很简单。

本文出自 “剩蛋君” 博客,转载请与作者联系!

你可能感兴趣的:(include,private,Complex)