C语言实现两个复数相加

用C语言实现复数ADT的实例:

Code Blocks及Visual C++6.0均可运行

#include
#include
typedef struct
{
    float realpart;
    float imagpart;
}Complex;
Complex create(float x,float y);
Complex add(Complex z1,Complex z2);
Complex create(float x,float y)
{
    Complex z;
    z.realpart=x;
    z.imagpart=y;
    return(z);
}
Complex add(Complex z1,Complex z2)
{
    Complex sum;
    sum.realpart=z1.realpart+z2.realpart;
    sum.imagpart=z1.imagpart+z2.imagpart;
    return(sum);
}
main()
{
    float a,b;
    Complex z1,z2,z3;
    printf("Please input The first number's realpart and imagpart:\n");
    scanf("%f %f",&a,&b);
    z1=create(a,b);
    printf("Please input the second number's realpart and imagpart:\n");
    scanf("%f %f",&a,&b);
    z2=create(a,b);
    z3=add(z1,z2);
    printf("z1.realpart=%f,z1.imagpart=%f\n",z1.realpart,z1.imagpart);
    printf("z2.realpart=%f,z2.imagpart=%f\n",z2.realpart,z2.imagpart);
    printf("z3=z1+z2\n");
    printf("z3.realpart=%f,z3.imagpart=%f\n",z3.realpart,z3.imagpart);

}


 

你可能感兴趣的:(数据结构)