Menghapus komentar di depan #include ‹math.h› akan menghancurkan kode saya

Program ini akan meminta pengguna untuk memasukkan bagian real dan imajiner dari dua bilangan kompleks. Program harus menampilkan jumlah, selisih, hasil kali, hasil bagi, dan nilai absolut dari bilangan kompleks yang diformat menjadi dua tempat desimal.

Semuanya telah berfungsi, kecuali bagian nilai abs dari kode, setiap kali saya menjalankannya, saya mendapatkan angka yang sangat aneh di mana nilai abs seharusnya berada, misalnya -1.644534634 atau -1.4363465.

Jadi saat saya mencari, saya perhatikan saya lupa menambahkan #include <math.h> ke atas, namun, ketika saya melakukan ini, semuanya menjadi kacau dan saya mendapatkan 61 kesalahan tentang bagaimana .real dan .img bukan bagian dari kompleks.

Jika memungkinkan, bisakah Anda melihat metode nilai abs saya dan melihat apakah saya menulisnya dengan benar, terima kasih.

#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 sumber


Jawaban (1)


Bagian 1:

Mungkin saja math.h menyertakan complex.h yang akan membuat makro ini:

#define complex _Complex

Saya sarankan mengganti nama tipe kompleks Anda, atau gunakan tipe bawaan yang dijelaskan di sini.

Anda juga bisa melakukan #undef complex setelah #include <main.h>, tetapi untuk program besar hal itu mungkin tidak berkelanjutan.

Bagian 2:

Anda menggunakan penentu format yang salah saat mencetak nilai absolut. Berikut peringatan dari clang yang menjelaskan masalah secara jelas dan memberikan solusi:

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
Ini memecahkan masalah kesalahan 61 saya! Terima kasih banyak. Bisakah Anda membantu saya dengan fungsi nilai abs? Saya masih mendapatkan jawaban yang sangat besar. Sepertinya fungsi sqrt tidak melakukan tugasnya. - person Sean; 30.09.2014
comment
Masalah dengan math.h mendefinisikan complex tampaknya merupakan masalah Microsoft C. Header math.h seharusnya tidak melakukan itu (ada komentar di math.h MSVC yang mengatakan, Nama non-ANSI untuk kompatibilitas yang menentukannya). - person Michael Burr; 30.09.2014
comment
Ohhh, saya lupa %d adalah cara lain untuk memasukkan int, bukan ganda. Terima kasih untuk itu! - person Sean; 30.09.2014