用c语言设计一个可进行复数运算的演示程序。要求实现下列6 种基本运算 ()由输人的实部和虚部生成一个复数 (2)两个复数求和 (3)两个复数求差 (4)两个复数求积 5)从已知复数中分离出实部 (6)从已知复数中分离出虚部 结果以相应的复数或实数的表示形式显示
时间: 2024-06-05 19:06:07 浏览: 12
#include <stdio.h>
typedef struct
{
double real; // 实部
double imag; // 虚部
} Complex;
// 函数声明
Complex add(Complex c1, Complex c2); // 复数加法
Complex sub(Complex c1, Complex c2); // 复数减法
Complex mul(Complex c1, Complex c2); // 复数乘法
double real(Complex c); // 分离实部
double imag(Complex c); // 分离虚部
// 主函数
int main()
{
Complex c1, c2, c3;
// 输入第一个复数
printf("请输入第一个复数的实部和虚部:");
scanf("%lf %lf", &c1.real, &c1.imag);
// 输入第二个复数
printf("请输入第二个复数的实部和虚部:");
scanf("%lf %lf", &c2.real, &c2.imag);
// 复数加法
c3 = add(c1, c2);
printf("复数加法:%.2f + %.2fi\n", c3.real, c3.imag);
// 复数减法
c3 = sub(c1, c2);
printf("复数减法:%.2f + %.2fi\n", c3.real, c3.imag);
// 复数乘法
c3 = mul(c1, c2);
printf("复数乘法:%.2f + %.2fi\n", c3.real, c3.imag);
// 分离实部
printf("第一个复数的实部为:%.2f\n", real(c1));
// 分离虚部
printf("第一个复数的虚部为:%.2f\n", imag(c1));
return 0;
}
// 函数定义
Complex add(Complex c1, Complex c2)
{
Complex c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return c;
}
Complex sub(Complex c1, Complex c2)
{
Complex c;
c.real = c1.real - c2.real;
c.imag = c1.imag - c2.imag;
return c;
}
Complex mul(Complex c1, Complex c2)
{
Complex c;
c.real = c1.real * c2.real - c1.imag * c2.imag;
c.imag = c1.real * c2.imag + c1.imag * c2.real;
return c;
}
double real(Complex c)
{
return c.real;
}
double imag(Complex c)
{
return c.imag;
}
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)