// p2_2.cpp - esimerkki 2. asteen yht„l”n ratkaisemisesta #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 { cout << "Anna 2. asteen yht„l”n a b c >"; cin >> a >> b >> c; if ( ratkaise_2_asteen_yhtalo(x1,x2,a,b,c) ) { cout << "Yht„l”ll„ ei ole reaalisia juuria!" << endl; } else { cout << "1. ratkaisu on " << x1 << ". Arvoksi tulee t„ll”in " << P2(x1,a,b,c) << endl; cout << "2. ratkaisu on " << x2 << ". Arvoksi tulee t„ll”in " << P2(x2,a,b,c) << endl; } } while (a>0); return 0; }