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

📄 complex.cpp

📁 一个用来测试我写的一个复数类的简单程序
💻 CPP
字号:
// Complex.cpp : Defines the entry point for the console application.
//

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

//构造函数
Complex::Complex()
{
	real = 0;
	imaginary = 0;
}

//重载构造函数
Complex::Complex(double a, double b)
{
	real = a;
	imaginary = b;
}

//析构函数
Complex::~Complex()
{

}

//取实部
double Complex::get_real()
{
	return real;
}

//取虚部
double Complex::get_imaginary()
{
	return imaginary;
}

//设置实部
void Complex::set_real(double r)
{
	real = r;
}

//设置虚部
void Complex::set_imaginary(double i)
{
	imaginary = i;
}

//通过设置实部和虚部重新设置一个复数
void Complex::set_complex(double r, double i)
{
	real = r;
	imaginary = i;
}


//求模
double Complex::get_modul()
{
	return sqrt(this->get_real() * this->get_real() + this->get_imaginary() * this->get_imaginary());
}

//求主幅角(实数0的幅角计算没有包括在内)
double Complex::get_argz()
{
	const double pai = 3.1415926535897932;

	double r, i;
	r = this->get_real();
	i = this->get_imaginary();

	//方法一:先考虑x正轴,y正轴,x负轴,y负轴4种情形,再考虑一、二、三、四这四个象限的情形
/*
	if (r > 0 && fabs(i - 0) < 0.000001)
	{
		return 0;
	}
	else if (r == 0 && i > 0)
	{
		return pai / 2;
	}
	else if (r < 0 && i == 0)
	{
		return pai;
	}
	else if (r == 0 && i < 0)
	{
		return 1.5 * pai;
	}
	else if (r > 0 && i >0)
	{
		return atan(i / r);
	}
	else if (r < 0)
	{
		return pai + atan(i / r);
	}
	else if (r > 0 && i < 0)
	{
		return 2 * pai + atan(i / r);
	}
	else
	{
	
	}
*/

	//方法二:利用函数atan2(y, x),先考虑r为0(即y轴)的情形,再考虑i为0(即x轴)情形,最后利用atan2(y, x)直接求出其它象限的情形
	if (fabs(r) < 0.000001)
	{
		if (i > 0)
		{
			return pai / 2;
		}
		else
		{
			return 1.5 * pai;
		}
	}
	else
	{
		if (fabs(i) < 0.000001)
		{
			if (r > 0)
			{
				return 0;
			}
			else 
			{
				return pai;
			}
		}
		else if (i > 0)
		{
			return atan2(i, r);
		}
		else if (i < 0)
		{
			return 2 * pai + atan2(i, r);
		}
	}

}

//求共轭复数
Complex Complex::get_conjugate_complex()
{
	Complex temp;

	temp.set_real(this->get_real());
	temp.set_imaginary(- this->get_imaginary());

	return temp;
}

//重载运算符“+”
Complex Complex::operator+(Complex c)
{
	Complex temp;
	double r, i;

	r = this->get_real() + c.get_real();
	i = this->get_imaginary() + c.get_imaginary();

	temp.set_real(r);
	temp.set_imaginary(i);
	
	return temp;
}

//重载运算符“-”
Complex Complex::operator-(Complex c)
{
	Complex temp;
	double r, i;

	r = this->get_real() - c.get_real();
	i = this->get_imaginary() - c.get_imaginary();

	temp.set_real(r);
	temp.set_imaginary(i);
	
	return temp;
}

//重载运算符“×”
Complex Complex::operator*(Complex c)
{
	Complex temp;
	double r, i;

	r = this->get_real() * c.get_real() - this->get_imaginary() * c.get_imaginary();
	i = this->get_real() * c.get_imaginary() + this->get_imaginary() * c.get_real();

	temp.set_real(r);
	temp.set_imaginary(i);

	return temp;
}

//重载运算符“/”
Complex Complex::operator/(Complex c)
{
	Complex temp;
	double r, i;

	r = (this->get_real() * c.get_real() + this->get_imaginary() * c.get_imaginary()) / (c.get_real() * c.get_real() + c.get_imaginary() * c.get_imaginary());
	i = (this->get_imaginary() * c.get_real() - this->get_real() * c.get_imaginary()) / (c.get_real() * c.get_real() + c.get_imaginary() * c.get_imaginary());

	temp.set_real(r);
	temp.set_imaginary(i);

	return temp;
}

//重载运算符“=”
Complex Complex::operator=(Complex c)
{
	//方法一:对实部和虚部分别赋值
//	this->set_real(c.get_real());
//	this->set_imaginary(c.get_imaginary());

	//方法二:一次性对实部和虚部进行赋值
	this->set_complex(c.get_real(), c.get_imaginary());

	return *this;
}

//重载运算符“<”
bool Complex::operator<(Complex c)
{
	if (this->get_modul() < c.get_modul())
	{
		return true;
	}
	else 
	{
		return false;
	}
}

//重载运算符“>”
bool Complex::operator>(Complex c)
{
	if (this->get_modul() > c.get_modul())
	{
		return true;
	}
	else 
	{
		return false;
	}
}

//重载运算符“==”
bool Complex::operator==(Complex c)
{
	if (fabs(this->get_real() - c.get_real()) < 0.0000001 && fabs(this->get_imaginary() - c.get_imaginary()) < 0.0000001)
	{
		return true;
	}
	else 
	{
		return false;
	}
}


//输出复数,考虑了很多输出形式
void Complex::output()
{
	if (this->get_real() == 0 && this->get_imaginary() == 0)
	{
		cout<<0<<endl;
	}
	else if (this->get_real() == 0 && this->get_imaginary() != 0)
	{
		cout<<this->get_imaginary()<<'i'<<endl;
	}
	else if (this->get_real() != 0 && this->get_imaginary() == 0)
	{
		cout<<this->get_real()<<endl;
	}
	else if (this->get_imaginary() > 0)
	{
		cout<<this->get_real()<<'+'<<this->get_imaginary()<<'i'<<endl;
	}
	else
	{
		cout<<this->get_real()<<this->get_imaginary()<<'i'<<endl;
	}
}

⌨️ 快捷键说明

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