C++ 2) More Polynomial Methods Extend the Poly class to support the evaluation o
ID: 3685910 • Letter: C
Question
C++
2) More Polynomial Methods Extend the Poly class to support the evaluation of quadratic polynomials as follows: 1. Add a member function named eval that takes a single double argument representing the 'x' value and returns the result (type double) of evaluating the polynomial at x. 2. Add a void member function named roots that will take two reference arguments of type Complex (use your Complex class), then compute and provide both roots of a quadratic polynomial object. Recall that the roots are given by the quadratic equation: The expression under the radical is referred to as the discriminant. If the discriminant is negative, the roots must be expressed as imaginary values using Complex numbers as follows: Cl The roots method should first determine if the roots are real or imaginary based on the value of the discriminant. If the roots are real, return them as complex values with the imaginary component set to zero, if the roots are imaginary, return them in complex form using the absolute value of the discriminantExplanation / Answer
#include <iostream>
#include<math.h>
using namespace std;
class complex
{
double i=0,r=0;
};
class Poly
{
public:
double a,b,c,x1, x2, determinant, realPart, imaginaryPart;
complex c1,c2;
friend istream &operator>>( istream &input, Poly &inpoly )
{
input >> inpoly.a >> inpoly.b>>inpoly.c;
return input;
}
void eval(int i)
{
int eq;
eq=2*i*i-6*i+5;
cout<<eq<<endl;
}
void roots(complex c1,complex c2)
{
//cout<<"the roots of f(x) are"<<c1<<" "<<c2<<endl;
/*double determinant = inpoly.b*inpoly.b - 4*inpoly.a*inpoly.c;
double x1,x2,realPart,imaginaryPart;
if (determinant > 0) {
x1 = (-inpoly.b + sqrt(determinant)) / (2*inpoly.a);
x2 = (-inpoly.b - sqrt(determinant)) / (2*inpoly.a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (determinant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-inpoly.b + sqrt(determinant)) / (2*inpoly.a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -inpoly.b/(2*inpoly.a);
imaginaryPart =sqrt(-determinant)/(2*inpoly.a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}*/
}
// Height of a box
};
int main( )
{
Poly inpoly;
//inpoly.>>;
cin>>inpoly;
for(int i=0;i<=10;i++)
inpoly.eval(i);
complex c1,c2;
float determinant = inpoly.b*inpoly.b - 4*inpoly.a*inpoly.c;
double x1,x2,realPart,imaginaryPart;
inpoly.roots(c1,c2);
//cout<<"the roots of f(x) are"<<c1<<" "<<c2<<endl;
if (determinant > 0) {
x1 = (-inpoly.b + sqrt(determinant)) / (2*inpoly.a);
x2 = (-inpoly.b - sqrt(determinant)) / (2*inpoly.a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (determinant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-inpoly.b + sqrt(determinant)) / (2*inpoly.a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -inpoly.b/(2*inpoly.a);
imaginaryPart =sqrt(-determinant)/(2*inpoly.a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.