Dallas884 2022-02-27 11:12 采纳率: 58.3%

# 设计一个复数数据结构，主要操作有复数的初始化、加、减和乘4种，要求利用这个数据结构，计算两个复数之和与两个复数之差的乘积，并打印此结果。

``````#include <stdio.h>

struct complex {
double real;
double image;
};

struct complex init(double real, double image);
struct complex add(struct complex a, struct complex b);
struct complex sub(struct complex a, struct complex b);
struct complex mul(struct complex a, struct complex b);

int main(void) {
double real,image;
struct complex x,y,z,t1,t2;
scanf("%lf%lf",&real,&image);
x=init(real,image);
scanf("%lf%lf",&real,&image);
y=init(real,image);
t2=sub(x,y);
z=mul(t1,t2);

if(z.image>=0)printf("%.2lf+%.2fi\n",z.real,z.image);
else printf("%.2lf%.2fi\n",z.real,z.image);

return 0;
}

/*提交以下代码*/
struct complex init(double real, double image) {

}

struct complex add(struct complex a, struct complex b) {

}

struct complex sub(struct complex a, struct complex b) {

}

struct complex mul(struct complex a, struct complex b) {

}

``````

1 2
2 3

2.00-8.00i

• 写回答

#### 2条回答默认 最新

• qzjhjxj 2022-02-27 11:53
关注

代码修改如下，供参考：

``````#include <stdio.h>
struct complex {
double real;
double image;
};

struct complex init(double real, double image);
struct complex add(struct complex a, struct complex b);
struct complex sub(struct complex a, struct complex b);
struct complex mul(struct complex a, struct complex b);

int main(void) {
double real,image;
struct complex x,y,z,t1,t2;
scanf("%lf%lf",&real,&image);
x=init(real,image);
scanf("%lf%lf",&real,&image);
y=init(real,image);
t2=sub(x,y);
z=mul(t1,t2);

if(z.image>=0)
printf("%.2lf+%.2fi\n",z.real,z.image);
else
printf("%.2lf%.2fi\n",z.real,z.image);

return 0;
}

/*提交以下代码*/
struct complex init(double real, double image) {
struct complex c;
c.real = real;
c.image= image;
return c;
}

struct complex add(struct complex a, struct complex b) {
struct complex sum;
sum.real = a.real + b.real;
sum.image= a.image+ b.image;
return sum;
}

struct complex sub(struct complex a, struct complex b) {
struct complex dif;
dif.real = a.real - b.real;
dif.image= a.image- b.image;
return dif;
}

struct complex mul(struct complex a, struct complex b) {
struct complex product;
product.real = a.real * b.real - a.image * b.image;
product.image= a.real * b.image+ a.image * b.real;
return product;
}

``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

• 系统已结题 3月7日
• 已采纳回答 2月27日
• 创建了问题 2月27日

#### 悬赏问题

• ¥100 ICEM3-4度尖角位置能不能将质量提升到0.2甚至更高
• ¥15 在网页上点击view PDF或者open PDF之类的选项加载很慢甚至加载不出来，换网或浏览器都不行
• ¥115 不能成功安装R语言pathview包
• ¥15 LEfSe在线分析Galaxy报错，如何解决？
• ¥15 GTEx数据库eqtl数据整理求教学
• ¥15 初学者，自己写的js俄罗斯代码，谁能帮我调试并找出问题所在！！ 自己调试了好多遍！快乱了，崩溃了！
• ¥15 qt 编译webengine 支持 webgl