/* p2_2.c */ #include #include int ratkaise_2_asteen_yhtalo(double *x1, double *x2, double a, double b, double c) { double D,SD; *x1 = *x2 = 0; if ( a==0 ) { /* bx + c = 0 */ if ( b==0 ) { /* c = 0 */ if ( c==0 ) { /* 0 = 0 */ return 0; /* id. tosi */ } /* c==0 */ else { /* c!=0 */ /* 0 != c = 0 */ return 1; /* Aina ep„t. */ } /* c!=0 */ } /* b==0 */ else { /* b!=0 */ /* bx + c = 0 */ *x1 = *x2 = -c/b; return 0; } /* b!=0 */ } /* a==0 */ else { /* a!=0 */ /* axx + bx + c = 0 */ D = b*b - 4*a*c; if ( D>=0 ) { /* Reaaliset juuret */ SD = sqrt(D); *x1 = (-b-SD)/(2*a); *x2 = (-b+SD)/(2*a); return 0; } /* D>=0 */ else { /* Imag. juuret */ return 1; } /* D<0 */ } /* a!=0 */ } double P2(double x, double a, double b, double c) { return (a*x*x + b*x + c); } int main(void) { double a,b,c,x1,x2; do { printf("Anna 2. asteen yht„l”n a b c >"); scanf("%lf %lf %lf",&a,&b,&c); if ( ratkaise_2_asteen_yhtalo(&x1,&x2,a,b,c) ) { printf("Yht„l”ll„ ei ole reaalisia juuria!\n"); } else { printf("1. ratkaisu on %lf. " "Arvoksi tulee t„ll”in %lf.\n",x1,P2(x1,a,b,c)); printf("2. ratkaisu on %lf. " "Arvoksi tulee t„ll”in %lf.\n",x2,P2(x2,a,b,c)); } } while (a>0); return 0; } &&