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
|
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
void quad(double&,double&,double&,double&,double&);
int main()
{
double a;
double b;
double c;
double x1;
double x2;
char choice = 'y';
cout <<"This program will find the roots of a quadratic"<<endl<<endl;
do{
cout <<"Please input the values for the coefficients; a, b, c""(y=ax^2+bx+c)"<<endl<<endl;
cin>>a>>b>>c;
quad(a,b,c,x1,x2);
if(x1 < 0)
{
cout <<setw<<fixed<<setprecision<<x1<<" is an imaginary root"<<endl;
}
else
{
cout <<setw<<fixed<<setprecision<<x1<<" is a real root"<<endl;
}
if (x2 < 0)
{
cout <<setw<<fixed<<setpresision<<x2<<" is an imaginary root"<<endl<<endl;
}
else
{cout <<setw<<fixed<<setprecision<<x2<<" is a real root"<<endl<<endl;
}
system ("cls");
cout <<"Would you like to run this again? y or Y for yes, anything else for no"<<endl<<endl;
}while (choice =='y' || choice =='Y');
system ("Pause");
return 0;
}
void quad(double&a, double&b, double&c, double&x1, double&x2)
{
x1 = (-b+sqrt(b*b-4*a*c))/(2*a);
x2 = (-b-sqrt(b*b-4*a*c))/(2*a);
}
|