/分别输入两个复数的实部与虚部,用函数实现计算两个复数之和与之积/
全局变量:为了解决多个函数间的变量共用
当局部变量与全局变量同名时,该函数中全局变量不起作用
#include<stdio.h>
float result_real,result_imag;//运算结果有两个数值,无法通过return返回,所以使用全局变量使其成为主函数与自定义函数均能使用的变量
int main(void)
{
float imag1,imag2,real1,real2;
void product(float real1,float real2,float imag1,float imag2);
void sum(float real1,float real2,float imag1,float iamg2);
printf("Enter 1st complex number:");
scanf("%f%f",&real1,&imag1);
printf("Enter 2st complex number:");
scanf("%f%f",&real2,&imag2);
product(real1,real2,imag1,imag2);
printf("product of complex if %f+%fi\n",result_real,result_imag);
sum(real1,real2,imag1,imag2);
printf("addition of complex is %f+%fi\n",result_real,result_imag);
return 0;
}
//复数之和
void sum(float real1,float real2,float imag1,float imag2)
{
result_real=real1+real2;
result_imag=imag1+imag2;
}
//复数之积
void product(float real1,float real2,float imag1,float imag2)
{
result_real=real1*real2-imag1*imag2;
result_imag=real1*imag2+real2*imag1;
}