Unfortunately, your parser is sort of a mess. It starts by finding "X^" and then works forward and backward from there. I think will fail for something "21X^2" (a multi-digit coefficient).
You should be able to do this by reading it from left to right:
A line is NAME = terms
The terms are an optional coefficient (possibly signed), then an optional "*", then "X^" and then the exponent.
After you get the exponent and coefficient, you can add them to your Polynomial using SetCoefficient(). Or can you? SetCoefficient needs to check whether the exponent is larger than the current degree (grau) and expand the coefficient array if it is.
Some other comments:
operator- should not return cp. What would pol1 + pol2 +pol3 do?
Instead of
Pol &operator-()
it should be
Pol operator-().
(returning a value not a reference. Now here's the cool part: you can define operator-() using operator-=()
1 2 3 4 5 6
|
Pol Pol::operator-(const Pol &right)
{
Pol result(*this);
result -= right;
return result;
}
|
Use the same trick for operators +, -, *, and / the same way.
Define a destructor for Pol() to avoid memory leaks.
Pol::operator-=() isn't right. You're setting grau too early. move line 292 to line 300 Also you create the new coeff array at line line 291, but never assign it to coeff. This results in memory corruption as you assign values into the old coeff array and beyond the end of it.
operator+=() probably has the same problem.