资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

数据结构(C语言描述)——复数-创新互联

我们先创建一个有利于后续操作的头文件,把预处理命令和一些常用的的宏(之后会用到)封装在头文件"StdFile.h"里面。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名与空间、虚拟主机、营销软件、网站建设、丘北网站维护、网站推广。

以下为"StdFile.h"里面的信息:

#include#include#include#include#define _USE_MATH_DEFINES
#include#includeusing namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef long long ElemType;
typedef int Index;
typedef int Num;
typedef int Step;

之后对复数的基本定义:

typedef struct
{
 Real real;
 Imag imag;
}Complex;

再创建一个头文件"Complex.h",里面存放关于复数的一些基本方法。

以下为"Complex.h"里面的信息:

#include "StdFile.h"
typedef long double Real, Imag;
typedef long double RealNum;

typedef struct
{
	Real real;
	Imag imag;
}Complex;

//创建复数
Complex CreateComplex(Real x = 0, Imag y = 0)
{
	Complex z = { 0,0 };
	z.real = x;
	z.imag = y;
	return z;
}

//打印复数
void PrintComplex(Complex z)
{
	if (z.real == 0)
		printf("j%.2llf\n", z.imag);
	else
		printf("%.2llf+j%.2llf\n", z.real, z.imag);
}

//获取实部
RealNum RealComplex(Complex z)
{
	return z.real;
}

//获取虚部
RealNum ImagComplex(Complex z)
{
	return z.imag;
}

//共轭复数
Complex ConjugateComplex(Complex z)
{
	Complex _z;
	_z.real = z.real;
	_z.imag = -z.imag;
	return _z;
}

//复数的模
RealNum AbsComplex(Complex z)
{
	RealNum r = 0;
	r = sqrt(z.real * z.real + z.imag * z.imag);
	return r;
}

//复数加法
Complex AddComplex(Complex z1, Complex z2)
{
	Complex z3 = { 0,0 };
	z3.real = z1.real + z2.real;
	z3.imag = z1.imag + z2.imag;
	return z3;
}

//复数减法
Complex SubComplex(Complex z1, Complex z2)
{
	Complex z3 = { 0,0 };
	z3.real = z1.real - z2.real;
	z3.imag = z1.imag - z1.imag;
	return z3;
}

//复数乘法
Complex MulComplex(Complex z1, Complex z2)
{
	Complex z3 = { 0,0 };
	z3.real = z1.real * z2.real - z1.imag * z2.imag;
	z3.imag = z1.real * z2.imag + z1.imag * z2.real;
	return z3;
}

//复数除法
Complex DivComplex(Complex z1, Complex z2)
{
	Complex z3 = { 0,0 };
	z3.real = (z1.real * z2.real + z1.imag * z2.imag) / (z2.real * z2.real + z2.imag * z2.imag);
	z3.imag = (z1.imag * z2.real - z1.real * z2.imag) / (z2.real * z2.real + z2.imag * z2.imag);
	return z3;
}

//复数的辐角
RealNum ArgComplex(Complex z)
{
	RealNum r;
	if (z.real >= 0 && z.imag == 0)
		r = 0;
	else if (z.real< 0 && z.imag == 0)
		r = M_PI;
	else if (z.real >= 0)
		r = atan(z.imag / z.real);
	else if (z.real< 0 && z.imag>0)
		r = atan(z.imag / z.real) + M_PI;
	else
		r = atan(z.imag / z.real) - M_PI;
	return r;
}

此方法不全,读者可添加更多方法(比如复数的三角式,复数的指数式,复数的幂,复数的对数,指数……)

以下为效果展示:

#include "Complex.h"
int main()
{
	Complex Z1, Z2, Z;
	Z = { 6,8 };
	Z1 = { 1,0 };
	Z2 = CreateComplex(2, 4);
	PrintComplex(Z1);
	PrintComplex(Z2);
	PrintComplex(AddComplex(Z1, Z2));
	cout<< "Z1+Z2的模 "<< AbsComplex(AddComplex(Z1, Z2))<< endl;
	cout<< "Z的实部 "<< RealComplex(Z)<< ' '<< "Z的虚部"<< ImagComplex(Z)<< endl;
	cout<< "打印Z/(Z1+Z2) ";
	PrintComplex(DivComplex(Z, AddComplex(Z1, Z2)));
	cout<< "Z的辐角主值 "<< ArgComplex(Z)<< endl;
}

显示结果:

辐角主值以弧度制显示,可能很难看出角度,优化后可以把弧度转化为角度:

//复数的辐角
RealNum ArgComplex(Complex z)
{
	RealNum r;
	if (z.real >= 0 && z.imag == 0)
		r = 0;
	else if (z.real< 0 && z.imag == 0)
		r = M_PI;
	else if (z.real >= 0)
		r = atan(z.imag / z.real);
	else if (z.real< 0 && z.imag>0)
		r = atan(z.imag / z.real) + M_PI;
	else
		r = atan(z.imag / z.real) - M_PI;
	return r/M_PI*180;
}

只需修改返回值即可。

修改后主函数的运行结果:

arctan(8/6)刚好就是53.1°,辐角显示无误。

以下是资源文件里面的文本文件"Help.Complex.txt"的内容,为方法的使用大纲:

typedef struct
{
	Real real;
	Imag imag;
}Complex;

//创建复数
Complex CreateComplex(Real x = 0, Imag y = 0)

//打印复数
void PrintComplex(Complex z)

//获取实部
RealNum RealComplex(Complex z)

//获取虚部
RealNum ImagComplex(Complex z)

//共轭复数
Complex ConjugateComplex(Complex z)

//复数的模
RealNum AbsComplex(Complex z)

//复数加法
Complex AddComplex(Complex z1, Complex z2)

//复数减法
Complex SubComplex(Complex z1, Complex z2)

//复数乘法
Complex MulComplex(Complex z1, Complex z2)

//复数除法
Complex DivComplex(Complex z1, Complex z2)

//复数的辐角
RealNum ArgComplex(Complex z)

之后继续更新关于顺序表的相关方法。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


本文题目:数据结构(C语言描述)——复数-创新互联
浏览路径:http://cdkjz.cn/article/ddedis.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220