/****************************************************************************** Lab Exercise #5 -- Implementation of support module ******************************************************************************/ using namespace std; #include #include #include "lab05.support.h" extern int InputStatus; int InputStatus; /*----------------------------------------------------------------------------- Name: coefficients Purpose: Accept the three coefficients of an equation from the user. Input: The coefficients (A, B and C) of the equation Return: The coefficients (as reference parameters) True if "end-of-file"; false otherwise (as the return expression) -----------------------------------------------------------------------------*/ void coefficients( double & A, double & B, double & C ) { cout << "Please enter the coefficients for a quadratic equation" << endl; cout << "(enter A, B and C, or enter Ctrl-d to halt):" << endl; cin >> A >> B >> C; if (cin.eof()) { InputStatus = 0; } else { InputStatus = 1; } } /*----------------------------------------------------------------------------- Name: roots Purpose: Compute the roots, given the coefficients of a quadratic equation. Receive: The coefficients (A, B and C) of the equation Return: The roots (as reference parameters) An error flag (as the return expression) -----------------------------------------------------------------------------*/ int roots( double A, double B, double C, double & Root1, double & Root2 ) { int Flag; double Discriminant; if (A == 0.0) { Flag = 3; // Not a quadratic equation } else { Discriminant = B*B-4.0*A*C; if (Discriminant < 0.0) { Flag = 2; // Equation has complex roots } else if (Discriminant == 0.0) { Flag = 1; // Equation has one real root Root1 = (-B)/(2.0*A); } else { Flag = 0; // Equation has two real roots Root1 = (-B + sqrt( Discriminant ))/(2.0*A); Root2 = (-B - sqrt( Discriminant ))/(2.0*A); } } return Flag; }