使用结构体(struct)将两个复数相加

简介: 使用结构体(struct)将两个复数相加。

使用结构体(struct)将两个复数相加。
我们把形如 a+bi(a,b均为实数)的数称为复数,其中 a 称为实部,b 称为虚部,i 称为虚数单位。
实例

include

typedef struct complex
{
float real;
float imag;
} complex;
complex add(complex n1,complex n2);

int main()
{
complex n1, n2, temp;

printf("第一个复数 \n");
printf("输入实部和虚部:\n");
scanf("%f %f", &n1.real, &n1.imag);

printf("\n第二个复数 \n");
printf("输入实部和虚部:\n");
scanf("%f %f", &n2.real, &n2.imag);

temp = add(n1, n2);
printf("Sum = %.1f + %.1fi", temp.real, temp.imag);

return 0;
AI 代码解读

}

complex add(complex n1, complex n2)
{
complex temp;

  temp.real = n1.real + n2.real;
  temp.imag = n1.imag + n2.imag;

  return(temp);
AI 代码解读

}
输出结果为:

第一个复数
输入实部和虚部:
2.3 4.5

第二个复数
输入实部和虚部:
3.4 5
Sum = 5.7 + 9.5i

目录
打赏
0
1
2
1
343
分享
相关文章
|
9月前
实现offsetof宏以及交换一个整数二进制奇偶位的宏
实现offsetof宏以及交换一个整数二进制奇偶位的宏
43 0
实现offsetof宏以及交换一个整数二进制奇偶位的宏
定义二维结构体常量数组
Implementation goes this way: type   TSampleEnumType = (seNone, seONE, seTWO, seTHREE, seFOUR);   TSampleRecord = record     SampEType: TSampleEnumTyp...
797 0
定义二维数组常量(结构体)
const   DayList:   array[0..6,1..4]   of   SmallInt                                       =((1,     5,11,1),                                          ...
720 0
详解sizeof、strlen、指针和数组等组合题
详解sizeof、strlen、指针和数组等组合题
133 0
【C语言】自定义类型:结构体深入解析(二)结构体内存对齐&&宏offsetof计算偏移量&&结构体传参
【C语言】自定义类型:结构体深入解析(二)结构体内存对齐&&宏offsetof计算偏移量&&结构体传参
115 6
关于c语言结构体偏移的一点思考(二)
关于c语言结构体偏移的一点思考(二)
92 0
关于c语言结构体偏移的一点思考(一)
关于c语言结构体偏移的一点思考(一)
153 0

热门文章

最新文章