编辑代码

#include <stdio.h>
//复数类
typedef struct complex
{
    int real,imag;
}comp;

void create(comp *x,int real,int imag);

void print(comp x);

void add(comp x,comp y,comp *z);//加
void subtract(comp x,comp y,comp *z);//减
void multiply(comp x,comp y,comp *z);//乘

int main()
{
    comp x,y,z;
    create(&x,1,3);
    create(&y,2,-2);
// x.real = 1; x.imag = 3;
// y.real = 2; y.imag = -2;
    print(x);
    print(y);
    add(x,y,&z);
    print(z);
    subtract(x,y,&z);
    print(z);
    multiply(x,y,&z);
    print(z);
    return 0;
}
//创建一个复数
void create(comp *x,int real,int imag)
{
    x -> real = real;
    x-> imag = imag;
}
//输出一个复数
void print(comp x)
{
    if(x.real != 0)
    {
        if(x.imag > 0)
        {
            if(x.imag != 1)
                printf("%d + %di",x.real,x.imag);
            else
                printf("%d + i",x.real);
        }
        else if(x,imag < 0)
        {
            if(x.imag != -1)
                printf("%d - %di",x.real,x.imag);
            else
                printf("%d - i",x.real);
        }
        else
            printf("%d",x.real);
    }
    else
    {
        if(x.imag > 0)
        {
            if(x.imag != 1)
                printf("%di",x.imag);
            else
                printf("i");
        }
        else if(x.imag < 0)
        {
            if(x.imag != -1)
                printf("%di",x.imag);
            else
                printf("-i");
        }
        else
            printf("0");
    }
    printf("\n");
}

void add(comp x,comp y,comp *z)
{
    z -> real = x.real + y.real;
    z -> imag = x.imag + y.imag;
}

void subtract(comp x,comp y,comp *z)
{
    z -> real = x.real - y.real;
    z -> imag = x.imag - y.imag;
}

void multiply(comp x,comp y,comp *z)
{
    z -> real = x.real * y.real - x.imag * y.imag;
    z -> imag = x.real * y.imag + x.imag * y.real;
}