二次関数の解についてのプログラム
Posted: 2010年5月01日(土) 23:47
学校の課題の二次関数の解を求めるプログラムでわからないところがあったので質問させて頂きます。
自分の作ったプログラムは
#include<stdio.h>
#include<math.h>
int main(void)
{
int a,b,c,d;
double e,u,m,r;
printf("Input coefficient A:");
scanf_s("%d",&a);
printf("Input coefficient B:");
scanf_s("%d",&b);
printf("Input coefficient C:");
scanf_s("%d",&c);
d=(b*b)-(4*a*c);
u=-b/(2.0*a);
r=-c/b;
if(d==0 && a!=0 && b!=0 && c!=0){
printf("x1=x2=%f",u);}
if(d>0 && a!=0 && c!=0){
e=sqrt(d)/(2*a);
printf("x1=%f\n",u+e);
printf("x2=%f\n",u-e);}
if(d<0 && a!=0 && c!=0){
m=sqrt(-d)/(2*a);
printf("x1=%f+%fi\n",u,m);
printf("x2=%f+%fi\n",u,m);}
if(a==0 && b!=0){
printf("x1=x2=%f",r);}
if(a==0 && b==0 && c==0){
printf("No solution");}
return 0;
}
です。
コンパイラは通るのですが、”NO solution”の結果が出るはずの時に強制終了してしまいます。
なぜなのか考えてみたのですがわかりません。
C言語は学校で初めて半年ほどです。
環境はWindows XPで、コンパイラはVC++2008EEです。
提出期限は5月6日です。よろしくお願いします。
自分の作ったプログラムは
#include<stdio.h>
#include<math.h>
int main(void)
{
int a,b,c,d;
double e,u,m,r;
printf("Input coefficient A:");
scanf_s("%d",&a);
printf("Input coefficient B:");
scanf_s("%d",&b);
printf("Input coefficient C:");
scanf_s("%d",&c);
d=(b*b)-(4*a*c);
u=-b/(2.0*a);
r=-c/b;
if(d==0 && a!=0 && b!=0 && c!=0){
printf("x1=x2=%f",u);}
if(d>0 && a!=0 && c!=0){
e=sqrt(d)/(2*a);
printf("x1=%f\n",u+e);
printf("x2=%f\n",u-e);}
if(d<0 && a!=0 && c!=0){
m=sqrt(-d)/(2*a);
printf("x1=%f+%fi\n",u,m);
printf("x2=%f+%fi\n",u,m);}
if(a==0 && b!=0){
printf("x1=x2=%f",r);}
if(a==0 && b==0 && c==0){
printf("No solution");}
return 0;
}
です。
コンパイラは通るのですが、”NO solution”の結果が出るはずの時に強制終了してしまいます。
なぜなのか考えてみたのですがわかりません。
C言語は学校で初めて半年ほどです。
環境はWindows XPで、コンパイラはVC++2008EEです。
提出期限は5月6日です。よろしくお願いします。