复数算术c语言,C语言的complex.h库复数运算

C支持复数的数学计算,复数Z可以在笛卡尔坐标表示为:Z=x+y*I;其中x和y是实数,I是虚数单位。数x被称为实部,数y为虚部。在c语言中,一个复数是有浮点类型表示的实部和虚部。两部分都具有相同的类型,无论是float,double或者long double。

float _complex:实虚都为float

double _complex:实虚都为double

long double _complex:实虚都为long double

如果在c 源文件中包含了头文件 complex.h ,complex.h定义了complex 和 I宏。宏定义complex和一个关键字_complex 同义。我们可以用complex代替_complex.

此外还有一点值得注意的是:

cos(), exp() 和 sqrt()同样也会有对应得复数方法,例如:ccos(),cexp(),csqrt()

/*

* Title : Complex Numbers

* Description: Work with complex numbers in c

* Author:Eric.Lee

*

*/

#include#include#define Get_Array_Length(tempArray) sizeof(tempArray)/sizeof(tempArray[0])

void GetResult(char operate,double complex x,double complex y)

{

double complex result = 0+0*I;

switch(operate)

{

case '+':

result = x+y;

break;

case '-':

result = x-y;

break;

case '*':

result = x*y;

break;

case '/':

result =x/y;

break;

default:

break;

}

printf("double complex x %c double complex y=%.2f+%.2fi\n",operate,creal(result),cimag(result));

}

int main()

{

double complex x = 10.0+15.0*I;

double complex y = 20.0-5.0*I;

printf("working with complex number:\n");

//creal(x):得到复数的实部(对于 double),如果对于float,使用crealf(x),如果对于long double ,请使用 creall(x)

//cimag(x):得到复数的虚部(对于double),如果对于float,使用crealf(x),如果对于long double ,请使用 creall(x)

printf("Starting values:x=%.2f+%.2fi\ty=%.2f +%.2fi\n",creal(x),cimag(x),creal(y),cimag(y));

char operates[] = {'+','-','*','/'};

char * op = operates;

int i = 0;

int operateLength = Get_Array_Length(operates);

for(i=0;i<=operateLength-1;i++)

{

GetResult(*(op++),x,y);

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值