// p2_2l.cpp - karsittu versio 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 ) if ( b == 0 ) { if ( c == 0 ) return 0; else return 1; } else { x1 = x2 = -c/b; return 0; } else { D = b*b - 4*a*c; if ( D>=0 ) { SD = sqrt(D); x1 = (-b-SD)/(2*a); x2 = (-b+SD)/(2*a); return 0; } else return 1; } } 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; }