⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complex.cpp

📁 复数计算,实现复数的加减乘除,求模,求主幅角,求共轭等功能
💻 CPP
字号:
//Complex.cpp

#include "stdafx.h"
#include "stdio.h"
#include "Complex.h"
#include "math.h"
#include "iostream.h"

#define PI 3.1415926

Complex::Complex()						//构造函数
{
}
Complex::Complex(double x,double y)		
{
	r=x;
	i=y;
}
Complex::~Complex()						//析构函数
{
}

void Complex::setComplex(double x,double y)		//修改复数成员变量
{
	r=x;
	i=y;
}
double Complex::getReal(Complex)				//取实部
{
	return this->r;
}
double Complex::getImaginary(Complex)			//取虚部
{
	return this->i;
}
double Complex::getLeth()						//求模
{
	return sqrt(r*r+i*i);
}
void Complex::Conjutate()						//求共轭复数
{
	i=-i;
}
double Complex::Angle()							//求幅角
{
	if(r>0			&&	i>=0)		return		atan(	i/r	);
	else if(r==0	&&	i>0)		return	PI/2;
	else if(r<0)					return (PI+	atan(	i/r	)	);
	else if(r==0	&&	i<0)		return PI*3/2;
	else if(r>0		&&	i<0)	return (PI*2+	atan(	i/r	)	);
	else							return 0;
}

Complex Complex::operator+(Complex Other)		//重载+运算符
{
	Complex temp;
	temp.r=this->r+Other.r;
	temp.i=this->i+Other.i;
	return temp;
}
Complex Complex::operator-(Complex Other)
{
	Complex temp;
	temp.r=this->r-Other.r;
	temp.i=this->i-Other.i;
	return temp;
}
Complex Complex::operator*(Complex Other)
{
	Complex temp;
	temp.r	=	this->r	*	Other.r	-	this->i	*	Other.i;
	temp.i	=	this->r	*	Other.i	+	this->i	*	Other.r;
	return temp;
}
Complex Complex::operator/(Complex Other)
{
	Complex temp;
	double ta;
	ta		=	Other.r	*	Other.r	+	Other.i	*	Other.i;
	if(	ta	!=0	)
	{
		temp.r	=(	this->r	*	Other.r	+	this->i	*	Other.i	)/	ta;
		temp.i	=(	this->i	*	Other.r	-	this->r	*	Other.i	)/	ta;
		return temp;
	}
	else
		printf("ERROR!");
}
/*Complex Complex::operator=(Complex Other)
{
	Complex temp;
	temp.setComplex(Other.getReal(Other), Other.getImaginary(Other) );
	return temp;
}*/
bool Complex::operator ==(Complex Other)		//重载==运算符,比较两复数模是否相等
{
	double t1,t2;
	t1=this->getLeth();
	t2=Other.getLeth();
	if(t1==t2)
		return 1;
	else return 0;
}
bool Complex::operator >(Complex Other)
{
	double t1,t2;
	t1=this->getLeth();
	t2=Other.getLeth();
	if(t1>t2)
		return 1;
	else return 0;
}
bool Complex::operator <(Complex Other)
{
	double t1,t2;
	t1=this->getLeth();
	t2=Other.getLeth();
	if(t1<t2)
		return 1;
	else return 0;
}

void Complex::Display()							//显示复数
{
	if(i>=0)
		cout<<r<<"+"<<i<<"i\n";
	else
		cout<<r<<i<<"i\n";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -