C 语言实例 - 复数相加
C 语言教程
· 2019-02-21 10:41:40
使用结构体(struct)将两个复数相加。
我们把形如 a+bi(a,b均为实数)的数称为复数,其中 a 称为实部,b 称为虚部,i 称为虚数单位。
实例
#include <stdio.h>
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;
}
complex add(complex n1, complex n2)
{
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return(temp);
}
输出结果为:
第一个复数 输入实部和虚部: 2.3 4.5 第二个复数 输入实部和虚部: 3.4 5 Sum = 5.7 + 9.5i
点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html
高效程序员的45个习惯
Venkat Subramaniam、Andy Hunt / 钱安川、郑柯 / 人民邮电出版社 / 2010-01 / 35.00元
“书中‘切身感受’的内容非常有价值——通过它我们可以做到学有所思,思有所悟,悟有所行。” ——Nathaniel T. Schutta,《Ajax基础教程》作者 “此书通过常理和经验,阐述了为什么你应该在项目中使用敏捷方法。最难得的是,这些行之有效的实战经验,竟然从一本书中得到了。” ——Matthew Johnson,软件工程师 十年来,软件行业发生了翻天覆地的变化。敏捷......一起来看看 《高效程序员的45个习惯》 这本书的介绍吧!