Code in C++, modify this code below. #include using namespace std; class Poly {
ID: 3817166 • Letter: C
Question
Code in C++, modify this code below.
#include
using namespace std;
class Poly
{
private:
double a, b, c;
public:
Poly()
{
a = b = c = 1;
}
Poly(double x, double y, double z)
{
a = x;
b = y;
c = z;
}
Poly operator+(Poly p)
{
Poly sum;
sum.a = a + p.a;
sum.b = b + p.b;
sum.c = c + p.c;
return sum;
}
friend std::ostream &operator<<(std::ostream &out, Poly p);
};
ostream &operator<<(std::ostream &out, Poly p)
{
if(p.a != 0)
out<
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:
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 discriminant.
3. Overload the >> (input) operator to input a Poly object in the following format: (a,b,c) Where a, b and c are the data values of the poly object. The input will include the parentheses and commas as shown.
Now, write a main program that will evaluate the quadratic polynomial 2x 2 - 6x + 5 at the integer values 0 through 10. Include the following C++ code:
The complex class:
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
double real;
double imag;
public:
Complex()
{
real = 0.0;
imag = 0.0;
}
Complex(double r,double i)
{
real = r;
imag = i;
}
double getReal()
{
return real;
}
double getImag()
{
return imag;
}
void setReal(double r)
{
real = r;
}
void setImag(double i)
{
imag = i;
}
friend ostream &operator<<(ostream &output,const Complex &D )
{
if(D.imag>0&&D.real>0)
output<<D.real<<" + "<<D.imag<<"i";
else if(D.imag==0)
output<<D.real;
else
output<<D.real<<D.imag<<"i";
return output;
}
friend istream &operator>>( istream &input, Complex &D )
{
input>>D.real>>D.imag;
return input;
}
Complex operator +(Complex obj)
{
Complex temp;
temp.real=real+obj.real;
temp.imag=imag+obj.imag;
return temp;
}
void operator -()
{
real=-real;
imag=-imag;
}
};
int main()
{
Complex c1,c2,c3;
cout <<" Enter two complex values: ";
cin >> c1 >> c2;
c3 = c1+c2;
cout <<" The sum is: " << c3 << endl;
-c3;
cout <<" and negating the sum yields: " <<c3<< endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex()
{
real = 0.0;
imag = 0.0;
}
Complex(double r,double i)
{
real = r;
imag = i;
}
double getReal()
{
return real;
}
double getImag()
{
return imag;
}
void setReal(double r)
{
real = r;
}
void setImag(double i)
{
imag = i;
}
friend ostream &operator<<(ostream &output,const Complex &D )
{
if(D.imag>0&&D.real>0)
output<<D.real<<" + "<<D.imag<<"i";
else if(D.imag==0)
output<<D.real;
else
output<<D.real<<D.imag<<"i";
return output;
}
friend istream &operator>>( istream &input, Complex &D )
{
input>>D.real>>D.imag;
return input;
}
Complex operator +(Complex obj)
{
Complex temp;
temp.real=real+obj.real;
temp.imag=imag+obj.imag;
return temp;
}
void operator -()
{
real=-real;
imag=-imag;
}
};
class Poly
{
private:
double a, b, c;
public:
Poly()
{
a = b = c = 1;
}
Poly(double x, double y, double z)
{
a = x;
b = y;
c = z;
}
Poly operator+(Poly p)
{
Poly sum;
sum.a = a + p.a;
sum.b = b + p.b;
sum.c = c + p.c;
return sum;
}
void setCoefficient(double x, double y, double z)
{
a = x;
b = y;
c = z;
}
double eval(double x)
{
return a*(x*x) + (b*x) + c;
}
void roots(Complex &root1, Complex &root2)
{
double discriminant = pow(b,2) - (4*a*c);
double imag_1 = 0, real_1 = 0;
double imag_2 =0, real_2 = 0;
if (discriminant < 0)
{
real_1 = real_2 = -b/(2*a);
imag_1 = sqrt(abs(discriminant)) / (2*a);
imag_2 = -imag_1;
}
else {
real_1 = (-b + sqrt(discriminant)) / (2*a);
real_2 = (-b - sqrt(discriminant)) / (2*a);
}
root1.setReal(real_1);
root1.setImag(imag_1);
root2.setReal(real_2);
root2.setImag(imag_2);
}
friend std::ostream &operator<<(std::ostream &out, Poly &p)
{
cout << "polynomial is ";
if(p.a != 0)
out << p.a <<"(x^2)";
if(p.b != 0)
out << " + "<< p.b << "x";
if(p.c != 0)
out << " + " << p.c << endl;
return out;
}
friend std::istream &operator>>(std::istream &input, Poly &p)
{
char open_br, close_br, comma_1, comma_2;
double a, b, c;
cout << "Enter the ploynomial (a,b,c) : ";
input>> open_br >> a>> comma_1 >>b >> comma_2 >> c >> close_br;
if (open_br == '(' && close_br == ')' && comma_1==',' && comma_2==',')
{
p.setCoefficient(a,b,c);
return input;
}
cout << "Invalid input"<<endl;
std::exit(EXIT_FAILURE);
}
};
int main()
{
/* [2(X^2) - 6X + 5] */
Poly p1(2, -6, 5);
cout << "Given polynomial is "<< p1;
for (int i=0; i<=10; i++) {
cout << "solution at x = "<< i <<" is "<< p1.eval(i) <<endl;
}
cout <<endl<<endl;
/* Finding out roots */
Complex c1, c2;
p1.roots(c1, c2);
cout << "roots are " << c1 << " and " << c2 <<endl<<endl;
/* Testing cin >> overloaded method */
Poly p2;
cin >> p2;
cout << p2 <<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.