1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int function1(int a, int b, int c);
int function2(int a, int b, int c);
int main()
{
FILE *fp1;
fp1=fopen("sol.txt","r" "a");
double a,b,c, quad1, quad2, x1,x2;
printf("this program solves the quadratic equation\n");
printf("ax^2 + bx + c, enter the numbers with a space\n");
scanf("%lf %lf %lf", &a,&b,&c);
if (a==0)
{
printf("i'm sorry we have an error input \n");
}
else{
x1=function1(a,b,c);
x2=function2(a,b,c);
if(x1<0||x2<0)
{
printf("-1\n");
if(x1<0){
printf("only 1 root is found: %f\n",x2);
}
else if(x2<0)
{
printf("only 1 root is found: %f\n",x1);
}
}
else if(x1==x2)
{
printf("0\n");
printf("the x value is similar: %lf\n", x1);
}
else
{
printf("1");
printf("the first x value is: %lf\n", x1);
printf("the second x value is: %lf\n", x2);
fprintf(fp1, "%fl", x1);
fprintf(fp1, "%fl", x2);
}
}
fclose(fp1);
return 0;
}
int function1(int a, int b, int c)
{
double quad1;
quad1 = (-b + sqrt((b*b) - 4*a*c))/2*a;
if(quad1<0)
{
printf("there is a quadrant error\n");
exit(1);
}
else{
return quad1;}
}
int function2(int a, int b, int c)
{
double quad2;
quad2 = (-b - sqrt((b*b) - 4*a*c))/2*a;
if(quad2<0)
{
printf("there is a quadrant error\n");
exit(1);
}
else{
return quad2;}
}
|