一、什么是复数
我们都知道实数,例如1,-7,3.25,其实复数就是比实数范围更广的数。因为表达式x * x + 1= 0无法解出x的值,所以引入i来表示,定义i的平方等于-1;所以i * i = -1,x * x = i * i。
复数形如z = a+bi;(a和b为实数,i是固定不变的字母,只有i的平方有数值为-1)
b等于0时z = a为实数;b不等于0时,z = a + bi为虚数;虚数中a 等于0时z = bi为纯虚数。
二、复数的求和
例如:z1 = -5 + 2i;z2 = 10.5 + 0.15i; 相加结果为z = 5.50 + 2.15i
简单代码
typedef struct
{
float x;
float y;
}cpl;
void add(cpl num1,cpl num2)
{
float sum1, sum2;
sum1 = num1.x + num2.x;
sum2 = num1.y + num2.y;
printf("两个复数相加为z=%.2f+%.2fi\r\n",sum1,sum2);
}
int main()
{
cpl num1,num2;
printf("请输入第一个复数:\r\n");
scanf("%f%f",&num1.x,&num1.y);
printf("请输入第二个复数:\r\n");
scanf("%f%f",&num2.x,&num2.y);
add(num1,num2);
}
输出结果为
请输入第一个复数:
-5 2
请输入第二个复数:
10.5 0.15
两个复数相加为z=5.50+2.15i