Quadratic formula loss of significance and simplification

Professor asked us to write codes that could solve a quadratic formula, and my codes are like this:

#include <bjarne/std_lib_facilities.h>

int main()
{
cout << "Enter the coefficients of a quadratic polynomial a*x**2 + b*x +c:\n";
cout << " a? ";
double a;
cin >> a;
cout << " b? ";
double b;
cin >> b;
cout << " c? ";
double c;
cin >> c;
double d = b*b-4*a*c;
double sqrtd = sqrt (d);
double xsingle = -b/(2*a);
double xlinear = -c/b;
double x1 = (-b+sqrtd)/(2*a);
double x2 = (-b-sqrtd)/(2*a);
if (a==0 && b==0 && c==0){
cout << "Trying to solve linear equation " << b << "*x + " << c << " == 0\n";
cout << "This is the trivial identity 0 == 0.\n";}
else if (a==0 && b==0 && c!=0){
cout << "Trying to solve linear equation " << b << "*x + " << c << " == 0\n";
cout << "This is the contradictory statement " << c << " == 0.\n";}
else if (a==0 && b!=0){
cout << "Trying to solve linear equation " << b << "*x + " << c << " == 0\n";
cout << "One root, x = " << xlinear << "\n";}
else if (a!=0 && d<0){
cout << "Trying to solve the quadratic equation " << a << "*x*x + " << b << "*x + " << c << " == 0\n";
cout << "No real roots.\n";}
else if (a!=0 && d==0 && x1*x2==c/a){
cout << "Trying to solve the quadratic equation " << a << "*x*x + " << b << "*x + " << c << " == 0\n";
cout << "Double root, x=" << xsingle << "\n";}
else if (a!=0 && d>0 && x1*x2==c/a){
cout << "Trying to solve the quadratic equation " << a << "*x*x + " << b << "*x + " << c << " == 0\n";
cout << "Two roots, x =" << x1 << " and x =" << x2 << "\n";}
}

Which runs perfectly, but I have 2 questions:
1. How to simplify these code?
On the assignment sheet the professor wrote about using
void solve_linear(double b, double c);
and
void solve_quadratic(double a, double b, double c);
which I currently dont understand how these works.
He asked us to write a well-encapsulated (as short as possible) program.

2. These are for extra points:
the precision problem of floating numbers: professor asked us to find a way to get the precise answer of it, like this:
Enter the coefficients of a quadratic polynomial a*x**2 + b*x +c:
a? 1
b? -20000
c? 1.5e-9
Trying to solve the quadratic equation 1*x*x + -20000*x + 1.5e-09 == 0
Using classical formula: Two roots, x = 20000 and x = 0
Using stable formula: Two roots, x = 20000 and x = 7.5e-14

My guess is that
A. Using code like if(x1*x2=a/c) to check if numbers were approximated.
B. Somehow determine the larger one in x1 and x2.
C. Somehow use that larger one to do something (LOL I really have no idea)
Is the smaller root = a/(c*the larger root) ?
I figured question B out by myself

else if (a!=0 && d>0 && x1*x2!=c/a){
if (x1==0){
xstable1 = c/(a*x2);
cout << "Trying to solve the quadratic equation " << a << "*x*x + " << b << "*x + " << c << " == 0\n";
cout << "Using classical formula: Two roots, x = " << x1 << " and x =" << x2 << "\n";
cout << "Using stable formula: Two roots, x = " << xstable1 << " and x =" << x2 << "\n";}
else{
xstable2 = c/(a*x1);
cout << "Trying to solve the quadratic equation " << a << "*x*x + " << b << "*x + " << c << " == 0\n";
cout << "Using classical formula: Two roots, x = " << x1 << " and x =" << x2 << "\n";
cout << "Using stable formula: Two roots, x = " << xstable2 << " and x =" << x1 << "\n";}
}
Topic archived. No new replies allowed.