1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
// This program is a fully functional root calculator.
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <ctime>
using namespace std;
//function prototypes
double quadraticpositive(double a,double b,double c);
double quadraticnegative(double a,double b,double c);
double quadraticzero(double a, double b);
double discriminant(double a,double b,double c);
bool Runagain (void);
//main function
int main()
{
cout << "This program can calculate the roots" << endl;
cout << "of an equation using the quadratic forumla." << endl;
cout << endl;
cout << endl;
do {
double a, b, c;
cout << "Enter coefficient a:"<< endl;
cin >> a;
cout << "Enter coefficient b:" << endl;
cin >> b;
cout << "Enter coefficient c:" << endl;
cin >> c;
cout << endl;
cout << endl;
if (discriminant (a, b, c) > 0 )
{
cout << "There are two real solutions." << endl;
cout << endl;
cout << "Equation: " << a << "x^2 + " << b << "x - " << c << endl;
cout << endl;
cout << "Has Roots: x = " << quadraticpositive(a,b,c) << " and x = " << quadraticnegative(a,b,c) << endl;
cout << endl;
}
else if (discriminant (a, b, c) == 0 )
{
cout << "There is one real solution." << endl;
cout << endl;
cout << "Equation: " << a << "x^2 + " << b << "x - " << c << endl;
cout << endl;
cout << "Has Root: x = " << quadraticzero(a,b) << endl;
cout << endl;
}
else (discriminant (a, b, c) < 0 );
{
cout << "There are two complex soultions." << endl;
cout << endl;
cout << "Equation: " << a << "x^2 + " << b << "x - " << c << endl;
cout << endl;
cout << "Has Roots: x = " << b << " + " << discriminant (a,b,c) << "i and x = " << b << discriminant (a,b,c) << "i" << endl;
cout << endl;
}
}
while (Runagain());
return(0);
}
//function implementation
bool Runagain(void)
{
char userResponse;
cout << "\nWould you like to run again (y or n): ";
cin >> userResponse;
if(userResponse == 'y')
return(true);
return(false);
}
double discriminant (double a, double b, double c)
{
double d;
d = (sqrt((b*b)+(-4*a*c)));
return (d);
}
double quadraticpositive(double a,double b,double c)
{
double qp;
qp = ((-(b) + (discriminant (a,b,c)))/(2*a));
return (qp);
}
double quadraticnegative(double a,double b,double c)
{
double qn;
qn = ((-(b) - ((discriminant (a,b,c))))/(2*a));
return (qn);
}
double quadraticzero(double a, double b)
{
double qz;
qz = (-(b)/(2*a));
return (qz);
}
|