A polynomial class should keep track of the
coefficients, not the
mathematical variables. That is, given Ax²+Bx+C, your class should store A, B, and C, but not x.
(That said, I have not seen the class definition given you by your professor. It could be that he wants you to keep an x in there somewhere.)
Get rid of line 7. The point of a getter or setter is to manipulate values in a class object. (Not global variables. You don't need any global variables.)
1 2 3 4 5 6 7 8
|
class Polynomial
{
private:
double a, b, c;
public:
double getA() const { return a; }
void setA( double new_a ) { a = new_a; }
};
|
Make sense?
Line 11 doesn't do what you think it does. Set each value to zero individually.
Line 23 expects you to evaluate the
discriminant (the part under the radical). Remember, the quadratic formula finds the center (axis of reflection) of a curve, and then the offsets of the intersections with the X-axis. It is:
(center) ± (offset)
-1*b √d
----- ± -----
2*a 2*a
where
d (the discriminant) is:
(b*b - 4*a*c)
If √d is zero, then the offset is zero, meaning the curve only touches the X-axis at one point.
If √d is a real, nonzero number (because d > 0), then there are exactly two points where the curve crosses the X-axis.
If √d is a complex number (because d < 0) then there is no point where the X-axis intersects the curve.
So your
evaluate() function should compute and return the discriminant.
Line 14 should look like
bool findRoots(double x, double &r1, double &r2)
.
(Again, I could be wrong about how your professor wants you to keep that x.)
It should first find
d by calling the
evaluate() function with your x.
If d < 0, then there are no real solutions. Return false.
Otherwise d >= 0.
Set both r1 and r2 equal to (-1*b)/(2*a).
If d == 0, you're done. Return true.
Otherwise, you also need to calculate the offset part: k = √(d)/(2*a). And then add the offset to r2 and subtract it from r1.
Return true.
When you
print() your polynomial, just output the standard equation but with a,b,and c in the proper spots:
cout << a << "x^2 + " << b << "x + " << c << " = 0\n";
Hope this helps.