2016-03-22 16 views
-2

Ich kann nicht herausfinden, warum mein Programm nicht tut, was es tun soll. Die Aufgabe bestand darin, ein c-Programm zu erstellen, das quadratisch mit zwei c-Dateien lösen kann. Dies ist der Code genannt interface.c, dass wir haben, mit zu arbeiten, nichts auf diesem geändert werden muss:Warum Programmausgabe x1 = Nan, x2 = Nan?

#include <stdio.h> 

void abc (void); 

int a, b, c; 

extern double x1real, x1imag, x2real, x2imag; 

static void get_parameters (void) 
{ 
    scanf("%d", &a); 
    scanf("%d", &b); 
    scanf("%d", &c); 
} 

void print_solution(void) 
{ 
    printf("The roots of %dx^2 + %dx + %d are:\n",a,b,c); 

    if(x1imag == 0 && x2imag == 0) 
    { 
     if(x1real == x2real) 
     { 
      printf("x = %.4f\n", x1real); 
     } 
     else 
     { 
      printf("x1 = %.4f, x2 = %.4f\n", x1real, x2real); 
     } 
    } 
    else 
    { 
     printf("x1 = %.4f+%.4fi, x2 = %.4f-%.4fi\n", x1real, x1imag, x2real, x2imag); 
    } 
} 

int main (void) 
{ 
    int runs, run; 

    scanf("%d",&runs); 

    for(run=0; run < runs; run++) 
    { 
     get_parameters(); 
     abc(); 
     print_solution(); 
    } 

    return 0; 
} 

Als nächstes ist der Code, den ich gemacht, die nicht funktioniert, es scheint etwas schief zu gehen mit die ganzzahligen Typen, die ich denke. Mit jeder quadratischen Formel wird x1 = nan x2 = nan ausgegeben. Es ist etwas falsch mit dem Integer-Typ, aber kann nicht herausfinden, welche.

#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 

extern int a, b, c; 

double x1real, x1imag, x2real, x2imag; 

int discriminant(void) 
{ 
    int discriminant; 

    discriminant = (pow(b,2)-4*a*c); 

    return discriminant; 
} 

void abc (void) 
{ 
    if (discriminant() > 0) 
    { 
     x1real = (-b - sqrt(discriminant()))/(2*a); 
     x2real = (-b + sqrt(discriminant()))/(2*a); 
     x1imag = 0; 
     x2imag = 0; 
    } 
    else 
    { 
     x1real = x2real = (-b)/(2*a); 
     x1imag = (-b - sqrt(-discriminant()))/(2*a); 
     x2imag = (-b + sqrt(-discriminant()))/(2*a); 
    } 

    return; 
} 
input: 
4 
2 0 0 
1 3 2 
3 4 9 
1 0 1 



output: 
The roots of 2x^2 + 0x + 0 are: 
x = 0.0000 
The roots of 1x^2 + 3x + 2 are: 
x1 = -1.0000, x2 = -2.0000 
The roots of 3x^2 + 4x + 9 are: 
x1 = -0.6667+-2.2653i, x2 = -0.6667-0.9319i 
The roots of 1x^2 + 0x + 1 are: 
x1 = 0.0000+-1.0000i, x2 = 0.0000-1.0000i 

suspected output: 
The roots of 2x^2 + 0x + 0 are: 
x = 0.0000 
The roots of 1x^2 + 3x + 2 are: 
x1 = -1.0000, x2 = -2.0000 
The roots of 3x^2 + 4x + 9 are: 
x1 = -0.6667+1.5986i, x2 = -0.6667-1.5986i 
The roots of 1x^2 + 0x + 1 are: 
x1 = 0.0000+1.0000i, x2 = 0.0000-1.0000i 
+3

'Diskriminante> 0 '? Soll die Funktion mit diesem Namen aufgerufen werden? –

+0

In Ihrem Code kann es sich um einen Namensauflösungskonflikt handeln. – callyalater

+0

Ja, ich vermute, dass Sie discriminant() brauchen, um die Verwendung eines Funktionszeigers zu vermeiden. –

Antwort

0

Quadratische Formel?

bedingter Ausdruck von 「b^2-4ac ≧ 0」 wird nicht aufgerufen, oder?

int discriminant(void) 
{ 
    int discriminant; 

    discriminant = (pow(b,2)-4*a*c); 

    return discriminant; 
} 

void abc (void) 
{ 

// if (discriminant > 0) // it doesn't call [int discriminant(void)] 
    if (discriminant() >= 0) 
    { 
     x1real = (-b - sqrt(discriminant()))/(2*a); 
     x2real = (-b + sqrt(discriminant()))/(2*a); 
     x1imag = 0; 
     x2imag = 0; 
    } 
    else 
    { 
     x1real = x2real = (-b)/(2*a); 
     x1imag = (-b - sqrt(-discriminant()))/(2*a); 
     x2imag = (-b + sqrt(-discriminant()))/(2*a); 
    } 

    return; 
} 

(Ein Zusatz)

static void get_parameters (void) 
{ 
    do { 
     scanf("%d", &a); 
     scanf("%d", &b); 
     scanf("%d", &c); 
    } while(a == 0) 
} 
+0

Siehe [Kommentar] (http://stackoverflow.com/questions/36158645/why-does-program-output-x1-nan-x2-nan#comment59955663_36158645) – chux

+0

Vielen Dank für Ihre Hilfe ! – MrAlphaking

+0

Jetzt berechnen Sie die Diskriminante dreimal mit jedem Aufruf von abc. Und Sie berücksichtigen nicht die Möglichkeit, dass "a" gleich Null ist. Verwenden Sie nicht "pow (b, 2)", um a zu berechnen Quadrat - benutze einfach 'b * b 'und berechne zweimal die gleiche Quadratwurzel. – FredK