Code:
#include <stdio.h>
#include <math.h>
#define INFILE "c:\\input.txt"
#define OUTFILE "c:\\roots.txt"
int compute_roots(float x2,float x,float term,double *root1,double *root2)
{
float t1;
t1=x*x-4*x2*term;
if (t1<0) return 0;
else if (t1==0) *root1=*root2=-1*x/(2*x2);
else
{
*root1=(-1*x+sqrt(t1))/(2*x2);
*root2=(-1*x-sqrt(t1))/(2*x2);
}
return 1;
}
int main(int argc, char* argv[])
{
float a,b,c;
double r1,r2;
FILE *ifp,*ofp;
if ((ifp=fopen(INFILE,"r"))!=NULL)
{
ofp=fopen(OUTFILE,"w");
while (fscanf(ifp,"%f %f %f",&a,&b,&c) == 3)
{
if (compute_roots(a,b,c,&r1,&r2))
fprintf(ofp,"%5g %5g %5g root1=%5g root2=%5g\n",a,b,c,r1,r2);
else fprintf(ofp,"%5g %5g %5g The roots are complex\n",a,b,c);
}
fclose(ifp);
fclose(ofp);
}
else puts("ERROR:FILE NOT FOUND");
return 0;
}