设计一个复数数据结构,主要操作有复数的初始化、加、减和乘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);
    t1=add(x,y);
    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) {

}

共2行:每行2个实数,分别表示复数的实部和虚部
输出
见样例
样例输入 Copy
1 2
2 3
样例输出 Copy
2.00-8.00i

代码修改如下,供参考:

#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);
    t1=add(x,y);
    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;
}

你按照公式算不就得了。。。