C++complex复数类

  复数是形如a+bi的数,复数由实部和虚部构成,在C++的模板库中由complex类,可以直接调用,包含在complex头文件中,再使用时应该添加#include。下面介绍一些基本操作:
生成复数对象
  complex类型的构造函数接受两个参数,第一个参数是复数实部的值,第二个参数是虚部的值。要想生成一个复数对象,并且对其值进行修改,参考以下代码:

#include
#include

using namespace std;

int main()
{
	complex<double>z(2, 3);
	complex<double>z1;
	complex<double>z2(z);

	cout << z << endl;
	cout << z1 << endl;
	cout << z2 << endl;
	z.real(22);
	z.imag(33);//修改z的实部和虚部的值
	cout << z << endl;

	complex<double>a, b, c;
	cout << "请输入三个复数:";
	cin >> a >> b >> c;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	cout << "c=" << c << endl;
	system("pause");
	return 0;
}

结果如下,程序实现了复数的生成,实部和虚部的修改,或者是按照用户需要生成复数
C++complex复数类_第1张图片
复数的运算
  复数和实数一样都有加减乘除四则运算,这些运算符号在complex模板中已经被重载过,能够直接使用,下面代码示例展示了其功能:

#include
#include

using namespace std;

int main()
{
complex<double> z(2 , 3);
	complex<double> z1 = z + 5.0;
	cout << z1 << endl;
	complex<double> z2 = z - 5.0;
	cout << z2 << endl;
	complex<double> z3 = z*2.0;
	cout << z3 << endl;
	complex<double>z4 = z / complex<double>(1,1);
	cout << z4 << endl;
	system("pause");
	return 0;
}

C++complex复数类_第2张图片
复数的比较
  复数的比较需要同时比较实部和虚部是否都相等,有其一不等两数就 不等,下面是示例代码:

#include
#include

using namespace std;

int main()
{
complex<double> z1(2, 3);
	complex<double> z2(3, 4);
	complex<double> z3(2, 3);
	cout << boolalpha << (z1 == z3) << endl;
	cout << boolalpha << (z1 == z2) << endl;
	system("pause");
	return 0;
}

在这里插入图片描述
以上是C++提供的基本功能,我看完后想着用自己的代码来实现一下复数的基本运算,实现代码如下:

#include
#include
using namespace std;

typedef struct 
{
    double real;//实部
	double imag;//虚部
}Complex;

//创建一个复数
Complex CreatComplex(double re, double im)
{
	Complex c;
	c.real = re;
	c.imag = im;
	return c;
}

//打印复数
void PrintComplex(Complex c)
{
	cout << c.real << " " << "+" << " " << "i" << c.imag << endl;
}

//复数的加法
Complex addition(Complex c1, Complex c2)
{
	Complex c;
	c.real = c1.real + c2.real;
	c.imag = c1.imag + c2.imag;
	return c;
}

//复数减法
Complex Subtraction(Complex c1, Complex c2)
{
	Complex c;
	c.real = c1.real - c2.real;
	c.imag = c1.imag - c2.imag;
	return c;
}

//复数乘法
Complex multiplication(Complex c1, Complex c2)
{
	Complex c;
	c.real = (c1.real * c2.real)- (c1.imag * c2.imag);
	c.imag = (c1.imag * c2.real)+ (c1.real * c2.imag);
	return c;
}

//复数除法
Complex Division(Complex c1, Complex c2)
{
	Complex c;
	c.real = (c1.real*c2.real + c1.imag*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag);
	c.imag=(c1.imag*c2.real-c1.real*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag);
	return c;
}

int main()
{	
	Complex c=CreatComplex(2.0,1.0);
	PrintComplex(c);
	Complex d = CreatComplex(1.0,5.0);
	PrintComplex(d);
	Complex add = addition(c, d);
	cout << "加法结果为:";
	PrintComplex(add);

	Complex sub = Subtraction(c, d);
	cout << "减法结果为:";
	PrintComplex(sub);

	Complex mul = multiplication(c, d);
	cout << "乘法结果为:";
	PrintComplex(mul);

	Complex div = Division(c, d);
	cout << "除法结果为:";
	PrintComplex(div);

	system("pause");
	return 0;
	
}

C++complex复数类_第3张图片
从结果可以看到,基本功能都能很好的实现。作者也是因为学到傅里叶变换接触到复数地运算才有此想法,等后面把傅里叶变换有了自己地理解再写一篇总结记录一下这根难啃地骨头。

你可能感兴趣的:(C++)