การลบความคิดเห็นที่อยู่หน้า #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);

}

person Sean    schedule 30.09.2014    source แหล่งที่มา


คำตอบ (1)


ส่วนที่ 1:

อาจเป็นไปได้ว่า math.h รวม complex.h ซึ่งจะสร้างมาโครนี้:

#define complex _Complex

ฉันขอแนะนำให้เปลี่ยนชื่อประเภทที่ซับซ้อนของคุณ หรือใช้บิวด์อินที่อธิบายไว้ที่นี่

คุณสามารถหลีกเลี่ยงการทำ #undef complex หลังจาก #include <main.h> ได้ แต่สำหรับโปรแกรมขนาดใหญ่นั้นอาจไม่ยั่งยืน

ส่วนที่ 2:

คุณกำลังใช้ตัวระบุรูปแบบที่ไม่ถูกต้องเมื่อคุณพิมพ์ค่าสัมบูรณ์ ต่อไปนี้เป็นคำเตือนจากเสียงดังกราวที่อธิบายปัญหาและให้แนวทางแก้ไขอย่างชัดเจน:

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
person Bill Lynch    schedule 30.09.2014
comment
วิธีนี้แก้ไขปัญหาข้อผิดพลาด 61 ของฉันได้! ขอบคุณมาก. ตอนนี้คุณช่วยฉันเกี่ยวกับฟังก์ชันค่า abs ได้ไหม ฉันยังคงได้รับคำตอบจำนวนมาก ดูเหมือนว่าฟังก์ชัน sqrt จะไม่ทำงาน - person Sean; 30.09.2014
comment
ปัญหาเกี่ยวกับ math.h การกำหนด complex ดูเหมือนจะเป็นปัญหาของ Microsoft C ส่วนหัว math.h ไม่ควรทำเช่นนั้น (มีความคิดเห็นใน math.h ของ MSVC ที่ระบุว่า ชื่อที่ไม่ใช่ ANSI สำหรับความเข้ากันได้โดยที่มันกำหนด) - person Michael Burr; 30.09.2014
comment
โอ้ ฉันลืมไปว่า %d เป็นอีกวิธีในการรวม int ไม่ใช่ double ขอบคุณสำหรับสิ่งนั้น! - person Sean; 30.09.2014