该程序采用两个复数作为结构,并使用函数将它们相加。
功能原型:
struct complex add(struct complex n1, struct complex n2);
n1 和 n2 是复数。
测试代码 :
#include <stdio.h>
struct complex
{
float real;
float imag;
};
struct complex add(struct complex n1, struct complex n2);
int main()
{
complex n1, n2, temp;
scanf("%f %f", &n1.real, &n1.imag);
scanf("%f %f", &n2.real, &n2.imag);
temp = add(n1, n2);
printf("Sum = %.1f + %.1fi", temp.real, temp.imag);
return 0;
}
/* Your code will be inserted here */
输入示例:
2.3 4.5
3.4 5
输出样例:
Sum = 5.7 + 9.5i
代码如下:
struct complex add(struct complex n1, struct complex n2)
{
struct complex sum;
sum.real=n1.real+n2.real;
sum.imag=n1.imag+n2.imag;
return sum;
}