2012-03-25 117 views
-3

我需要幫助。 該程序使用gcc進行編譯。但是當我在終端上運行它時,我不知道爲什麼程序是給我等於2個真正的解決方案或2個複雜的解決方案,以0解決二次方程的C程序

與GCC編譯,你必須添加-lm

感謝你的幫助

#include<stdio.h> 
#include<stdlib.h> 
#include<math.h> 
main() 
{ 
int a,b,c; 
float x,x1,x2,x2c,x1c,xcb,xba,D; 
char i; 

x1==(-b+sqrt(D))/(2*a); 
x2==(-b-sqrt(D))/(2*a); 
x1c==(-b+i*sqrt(-D))/(2*a); 
x2c==(-b-i*sqrt(-D))/(2*a); 
xcb==-c/b; 
xba==-b/(2*a); 
D==b*b-4*a*c; 

printf("a="); 
scanf("%d",&a); 
printf("b="); 
scanf("%d",&b); 
printf("c="); 
scanf("%d",&c); 
a*x*x+b*x+c==0; 
if(a==0) 
{ 
if(b==0) 
    { 
    if(c==0) printf("x belongs to R\n"); 
    else printf("no solutions\n",x); 
    } 
else printf("x= %f \n",xcb); 
    } 

else 
{ 
if(D==0) printf("x= %f \n",xba); 
else 
    { 
    if(D>0) {printf("2 solutions Real: x1=%f\n x2=%f\n",x1,x2);} 
    else {printf("2 solutions Complex: x1=%f\n x2=%f\n",x1c,x2c);} 
    } 
} 
return 0; 
} 
+6

這不會使用GCC進行非常好的編譯。請使用(至少)'-Wall'標誌。 – Mat 2012-03-25 16:33:23

+4

爲什麼所有的==?你不想用=指定嗎? – duffymo 2012-03-25 16:34:10

+0

你的代碼可以更清晰。看到這個:http://cstartercodes.blogspot.in/2015/02/solution-of-quadratic-equation.html – 2015-02-12 00:59:28

回答

7

您使用的變量都沒有初始化。
您有未定義的行爲四處散落。

int a,b,c; 
float x,x1,x2,x2c,x1c,xcb,xba,D; 
char i; 
6

分配是一個0​​:

x1==(-b+sqrt(D))/(2*a); 
x2==(-b-sqrt(D))/(2*a); 
x1c==(-b+i*sqrt(-D))/(2*a); 
x2c==(-b-i*sqrt(-D))/(2*a); 
xcb==-c/b; 
xba==-b/(2*a); 
D==b*b-4*a*c; 

而且你有沒有變量初始化。

事實上,cl.exe時在Windows產量編譯:

test.c(10) : warning C4553: '==' : operator has no effect; did you intend '='? 
test.c(11) : warning C4553: '==' : operator has no effect; did you intend '='? 
test.c(12) : warning C4553: '==' : operator has no effect; did you intend '='? 
test.c(13) : warning C4553: '==' : operator has no effect; did you intend '='? 
test.c(14) : warning C4553: '==' : operator has no effect; did you intend '='? 
test.c(15) : warning C4553: '==' : operator has no effect; did you intend '='? 
test.c(16) : warning C4553: '==' : operator has no effect; did you intend '='? 
test.c(24) : warning C4553: '==' : operator has no effect; did you intend '='? 
d:\test.c(10) : warning C4700: uninitialized local variable 'D' used 
d:\test.c(30) : warning C4700: uninitialized local variable 'x' used 
d:\test.c(32) : warning C4700: uninitialized local variable 'xcb' used 
d:\test.c(37) : warning C4700: uninitialized local variable 'xba' used 
d:\test.c(40) : warning C4700: uninitialized local variable 'x2' used 
d:\test.c(40) : warning C4700: uninitialized local variable 'x1' used 
d:\test.c(41) : warning C4700: uninitialized local variable 'x2c' used 
d:\test.c(41) : warning C4700: uninitialized local variable 'x1c' used 
2

我不知道,他們已經被我添加複數到C.你乘,如果你是用Python編寫。

有太多不對的代碼來解決,但在這裏是一個不完整的企圖:

#include<stdio.h> 
#include<stdlib.h> 
#include<math.h> 
main() 
{ 
    int a,b,c; 
    float x,x1,x2,x2c,x1c,xcb,xba,D; 
    char i; 

    printf("a="); 
    scanf("%d",&a); 
    printf("b="); 
    scanf("%d",&b); 
    printf("c="); 
    scanf("%d",&c); 

    x1=(-b+sqrt(D))/(2*a); 
    x2=(-b-sqrt(D))/(2*a); 
    x1c=(-b+i*sqrt(-D))/(2*a); 
    x2c=(-b-i*sqrt(-D))/(2*a); 
    xcb=-c/b; 
    xba=-b/(2*a); 
    D=b*b-4*a*c; 

    if(a==0) 
    { 
    if(b==0) 
     { 
     if(c==0) printf("x appartient à R\n"); 
     else printf("Pas de solutions\n",x); 
     } 
    else printf("x= %f \n",xcb); 
     } 

    else 
    { 
    if(D==0) printf("x= %f \n",xba); 
    else 
     { 
     if(D>0) {printf("2 solutions Réelles: x1=%f\n x2=%f\n",x1,x2);} 
     else {printf("2 solutions Complexes: x1=%f\n x2=%f\n",x1c,x2c);} 
     } 
    } 
    return 0; 
} 
+0

C99增加了'complex.h'和'_Complex'類型。見例如。 http://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c – 2012-03-25 16:39:32

+0

謝謝你,亞歷山大;自從我寫C以來已經太久了。這段代碼是否正確地執行了它? – duffymo 2012-03-25 16:42:15

+3

你應該使用'float _Complex'作爲數據類型,'_Complex_I'代替'i'(並且包括'')。另外,如果其中一個係數很大,那麼該方法本身並不真實。請參閱[我的這個答案](http://stackoverflow.com/questions/4503849/quadratic-equation-in-ada/4504415#4504415),以避免災難性的取消,當三角洲接近b。另外'D'被使用初始化。 – 2012-03-25 16:50:17

0

存在着這些問題:

1.Undefined變量(你應該在使用前將它們分配)

2.'=='在這裏不適用。

3.當我們做

int a; 
    int b; 
    printf("%f",a/b); 

的編譯器會給你一個警告,如果B> A,將在屏幕上的結果。