c语言 - 删除"#include <math.h>"前面的注释会破坏我的代码



程序将要求用户输入两个复数的实部和虚部。程序应输出复数的和、差、积、商和绝对值,这些复数的格式为小数点后两位。

除了代码中的abs值部分,一切都正常,每当我运行它时,我都会得到abs值应该在的奇怪数字,例如-1.644534634或-1.4363465。

所以当我看的时候,我注意到我忘了把#include <math.h>添加到顶部,然而,当我这样做的时候,一切都变得一团糟,我得到了61个关于.real和.img如何不是复杂的一部分的错误。

如果可能的话,你能看看我的abs值方法,看看我写的是否正确,谢谢。

#include <stdio.h>
//#include <math.h>
struct complex
{
    float real;
    float img;
};
struct complex add_complex(struct complex c1, struct complex c2);
struct complex subtract_complex(struct complex c1, struct complex c2);
struct complex multiply_complex(struct complex c1, struct complex c2);
struct complex divide_complex(struct complex c1, struct complex c2);
double abs_complex(struct complex c);
void main()
{
    struct complex c, c1, c2, tempa, temps, tempm, tempd;
    double abs1, abs2;
    printf("Enter the real part of the 1st complex number:n");
    scanf("%f", &c1.real);
    printf("Enter the imaginary part of the 1st complex number:n");
    scanf("%f", &c1.img);
    printf("Enter the real part of the 2nd complex number:n");
    scanf("%f", &c2.real);
    printf("Enter the imaginary part of the 2nd complex number:n");
    scanf("%f", &c2.img);
    tempa = add_complex(c1, c2);
    temps = subtract_complex(c1, c2);
    tempm = multiply_complex(c1, c2);
    tempd = divide_complex(c1, c2);
    abs1 = abs_complex(c1);
    abs2 = abs_complex(c2);
    printf("n");
    if (tempa.real == 0)
        printf("(%.2f + %.2fi) + (%.2f + %.2fi) = (%.2fi)n", c1.real, c1.img, c2.real, c2.img, tempa.img);
    else if (tempa.img == 0)
        printf("(%.2f + %.2fi) + (%.2f + %.2fi) = (%.2f)n", c1.real, c1.img, c2.real, c2.img, tempa.real);
    else
        printf("(%.2f + %.2fi) + (%.2f + %.2fi) = (%.2f + %.2fi)n", c1.real, c1.img, c2.real, c2.img, tempa.real, tempa.img);
    if (temps.real == 0)
        printf("(%.2f + %.2fi) - (%.2f + %.2fi) = (%.2fi)n", c1.real, c1.img, c2.real, c2.img, temps.img);
    else if (temps.img == 0)
        printf("(%.2f + %.2fi) - (%.2f + %.2fi) = (%.2f)n", c1.real, c1.img, c2.real, c2.img, temps.real);
    else
        printf("(%.2f + %.2fi) - (%.2f + %.2fi) = (%.2f + %.2fi)n", c1.real, c1.img, c2.real, c2.img, temps.real, temps.img);
    if (tempm.real == 0)
        printf("(%.2f + %.2fi) * (%.2f + %.2fi) = (%.2fi)n", c1.real, c1.img, c2.real, c2.img, tempm.img);
    else if (tempm.img == 0)
        printf("(%.2f + %.2fi) * (%.2f + %.2fi) = (%.2f)n", c1.real, c1.img, c2.real, c2.img, tempm.real);
    else
        printf("(%.2f + %.2fi) * (%.2f + %.2fi) = (%.2f + %.2fi)n", c1.real, c1.img, c2.real, c2.img, tempm.real, tempm.img);
    if (tempd.real == 0)
        printf("(%.2f + %.2fi) / (%.2f + %.2fi) = (%.2fi)n", c1.real, c1.img, c2.real, c2.img, tempd.img);
    else if (tempd.img == 0)
        printf("(%.2f + %.2fi) / (%.2f + %.2fi) = (%.2f)n", c1.real, c1.img, c2.real, c2.img, tempd.real);
    else
        printf("(%.2f + %.2fi) / (%.2f + %.2fi) = (%.2f + %.2fi)n", c1.real, c1.img, c2.real, c2.img, tempd.real, tempd.img);
    printf("|(%.2f + %.2fi)| = (%.2d)n", c1.real, c1.img, abs1);
    printf("|(%.2f + %.2fi)| = (%.2d)n", c2.real, c2.img, abs2);
    printf("n");
}
struct complex add_complex(struct complex c1, struct complex c2)
{
    struct complex tempa;
    tempa.real = c1.real + c2.real;
    tempa.img = c1.img + c2.img;
    return(tempa);
}
struct complex subtract_complex(struct complex c1, struct complex c2)
{
    struct complex temps;
    temps.real = c1.real - c2.real;
    temps.img = c1.img - c2.img;
    return(temps);
}
struct complex multiply_complex(struct complex c1, struct complex c2)
{
    struct complex tempm;
    tempm.real = c1.real*c2.real - c1.img*c2.img;
    tempm.img = c1.real*c2.img + c1.img*c2.real;
    return(tempm);
}
struct complex divide_complex(struct complex c1, struct complex c2)
{
    struct complex tempd, temp1, temp2;
    temp1.real = c1.real*c2.real + c1.img*c2.img;
    temp2.real = c2.real*c2.real + c2.img*c2.img;
    temp1.img = c1.img*c2.real - c1.real*c2.img;
    temp2.img = c2.real*c2.real + c2.img*c2.img;
    tempd.real = temp1.real / temp2.real;
    tempd.img = temp1.img / temp2.img;
    return(tempd);
}
double abs_complex(struct complex c)
{
    double temp1, temp2;
    double abs;
    temp1 = c.real*c.real;
    temp2 = c.img*c.img;
    abs = sqrt(temp1 + temp2);
    return(abs);
}

第1部分:

math.h可能包括complex.h,后者将创建此宏:

#define complex _Complex

我建议重命名你的复杂类型,或者使用这里描述的内置类型。

你也可以在#include <main.h>之后再做#undef complex,但对于大型项目来说,这可能是不可持续的。

第2部分:

打印绝对值时使用了错误的格式说明符。以下是clang的警告,这些警告清楚地解释了问题并提供了解决方案:

foo.c:68:60: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
    printf("|(%.2f + %.2fi)| = (%.2d)n", c1.real, c1.img, abs1);
                                ~~~~                       ^~~~
                                %.2f
foo.c:69:60: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
    printf("|(%.2f + %.2fi)| = (%.2d)n", c2.real, c2.img, abs2);
                                ~~~~                       ^~~~
                                %.2f

最新更新