复数运算

复数,高考中的一道5分题,现实中作用貌似不大,在C中的表达还挺有个性,只是这代码好笨重,不知还能不能简化。
#include <stdio.h>
#include <complex.h>/*包含复数关键字complex和I*/

int main(void)
{
    long a=0L,b=0L,c=0L,d=0L;

    printf("Type a number for the real part of z1:");
    scanf("%ld",&a);
    printf("Type a number for the imaginary part of z1:");
    scanf("%ld",&b);
    printf("\nType a number for the real part of z2:");
    scanf("%ld",&c);
    printf("Type a number for the imaginary part of z2:");
    scanf("%ld",&d);

    double complex z1 = a + b*I;/*定义第一个复数*/
    double complex z2 = c + d*_Complex_I;/*定义第二个复数*/

    printf("\nStarting values: \n\t\tz1 = %.0f%+.0fi\n\t\tz2 = %.0f%+.0fi",
            creal(z1),cimag(z1),creal(z2),cimag(z2));

    double complex sum = z1 + z2;/*求和*/
    if (creal(sum)!=0&&cimag(sum)!=0)/*实部、虚部都不为零*/
        printf("\n\nThe sum z1 + z1 = %.0f%+.0fi",creal(sum),cimag(sum));
    if (creal(sum)!=0&&cimag(sum)==0)/*虚部为零,实部不为零*/
        printf("\n\nThe sum z1 + z1 = %.0f",creal(sum));
    if (creal(sum)==0&&cimag(sum)!=0)/*实部为零,虚部不为零*/
        printf("\n\nThe sum z1 + z1 = %.0fi",cimag(sum));
    if (creal(sum)==0&&cimag(sum)==0)/*实部、虚部都为零*/
        printf("\n\nThe sum z1 + z1 = 0");

    double complex difference = z1 - z2;/*求差*/
    if (creal(difference)!=0&&cimag(difference)!=0)
        printf("\n\nThe diference z1 - z2 = %.0f%+.0fi",
            creal(difference),cimag(difference));
    if (creal(difference)!=0&&cimag(difference)==0)
        printf("\n\nThe diference z1 - z2 = %.0f",creal(difference));
    if (creal(difference)==0&&cimag(difference)!=0)
        printf("\n\nThe diference z1 - z2 = %.0fi",cimag(difference));
    if (creal(difference)==0&&cimag(difference)==0)
        printf("\n\nThe diference z1 - z2 = 0");

    double complex product = z1*z2;/*求积*/
    if (creal(product)!=0&&cimag(product)!=0)
        printf("\n\nThe product z1*z2 = %.0f%+.0fi",
            creal(product),cimag(product));
    if (creal(product)!=0&&cimag(product)==0)
        printf("\n\nThe product z1*z2 = %.0f",creal(product));
    if (creal(product)==0&&cimag(product)!=0)
        printf("\n\nThe product z1*z2 = %.0fi",cimag(product));
    if (creal(product)==0&&cimag(product)==0)
        printf("\n\nThe product z1*z2 = 0");


    double complex quotient = z1/z2;/*求商*/
    if (creal(quotient)!=0&&cimag(quotient)!=0)
        printf("\n\nThe quotient z1/z2 = %.0f%+.0fi",
            creal(quotient),cimag(quotient));
    if (creal(quotient)!=0&&cimag(quotient)==0)
        printf("\n\nThe quotient z1/z2 = %.0f",creal(quotient));
    if (creal(quotient)==0&&cimag(quotient)!=0)
        printf("\n\nThe quotient z1/z2 = %.0fi",cimag(quotient));
    if (creal(quotient)==0&&cimag(quotient)==0)
        printf("\n\nThe quotient z1/z2 = 0");



    double _Complex z1_conjugate = conj(z1),z2_conjugate = conj(z2);/*共轭*/
    if (creal(z1_conjugate)!=0&&cimag(z1_conjugate)!=0)
        printf("\n\nThe conjugate of z1 = %.0f%+.0fi",
            creal(z1_conjugate),cimag(z1_conjugate));
    if (creal(z1_conjugate)!=0&&cimag(z1_conjugate)==0)
        printf("\n\nThe conjugate of z1 = %.0f",creal(z1_conjugate));
    if (creal(z1_conjugate)==0&&cimag(z1_conjugate)!=0)
        printf("\n\nThe conjugate of z1 = %.0fi",cimag(z1_conjugate));
    if (creal(z1_conjugate)==0&&cimag(z1_conjugate)==0)
        printf("\n\nThe conjugate of z1 = 0");
    if (creal(z2_conjugate)!=0&&cimag(z2_conjugate)!=0)
        printf("\nThe conjugate of z2 = %.0f%+.0fi\n",
            creal(z2_conjugate),cimag(z2_conjugate));
    if (creal(z2_conjugate)!=0&&cimag(z2_conjugate)==0)
        printf("\n\nThe conjugate of z2 = %.0f\n",creal(z2_conjugate));
    if (creal(z2_conjugate)==0&&cimag(z2_conjugate)!=0)
        printf("\n\nThe conjugate of z2 = %.0fi\n",cimag(z2_conjugate));
    if (creal(z2_conjugate)==0&&cimag(z2_conjugate)==0)
        printf("\n\nThe conjugate of z2 = 0\n");

    return 0;
}

评论

此博客中的热门博文

清明雨凉

简记