c program

2007-11-25 8:56 pm
write a program to solve the quartratic equation in the form a^2+b*x+c.the coefficients a,b,c are input from the keyboard. your program should consider of the following case:
1.when both a and b are zero, output"it is not an equation"
2.when a =0and b not equal to 0,x=-c/b
3when a not equal to 0and b*b-4ac<0,output"it is not solution"
4when a not equal to 0 and b^2-4ac=0,x=-b/2a
5when a not equal 0 and b*b-4ac>0,x=-b+-sqrt(b*b-4ac)/2a

回答 (1)

2007-11-27 8:25 pm
✔ 最佳答案
/* quadratic equation using Borland C-compiler, the include statement
should have been enclosed in angle brackets, shown in quotes because
yahooHK traps angle brackets \<>*/
#include "stdio"
#include "stdlib.h"
#include "math.h"
int main(int argc, char* argv[])
{
double array[3],a,b,c,x,x1,x2,D;
double eps=0.000000001; // tolerance
int i;
char li[200];
char ch[3]="abc";
printf("Solution of equation ax^2+bx+c=0\n");
for(i=0;i<3;i++)
{
printf("Enter %c:",ch[i]);
gets(li);
array[i]=atof(li);
}
a=array[0]; b=array[1]; c=array[2];
if(a==0&&b==0){
printf("The input parameters do not make an equation\n");
} else if(a==0){
x=-c/b;
printf("problem reduced to linear equation, x=%lf\n",x);
} else if(b*b<4*a*c){
printf("roots are complex, no real solution\n");
} else if(fabs(b*b-4*a*c less than eps){
printf("Two identical solutions are x=%f\n",-b/2/a);
} else {
D=sqrt(b*b-4*a*c);
x1=(-b+D)/2/a;
x2=(-b-D)/2/a;
printf("The solutions are x=%f and x=%f\n",x1,x2);
}
return 0;
}


收錄日期: 2021-04-13 14:34:12
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20071125000051KK01796

檢視 Wayback Machine 備份