
please wait
|
|
|
|
|
|
ne555 (1349) Jun 16, 2011 at 4:44am Just implement two operators, divide and modulus. However you obtain the other when calculate so a method that returns both will be better (return a pair or polynomials). Like with integer division and module. @rechim: at least you could give it a shot. Think in how do you do the division by hand. (division of numbers) |
void Polynomial::divide(const Polynomial &Divisor, Polynomial &q, Polynomial &r)
|
|
|
|
r = *this;
|
|
fun2code (535) Jun 16, 2011 at 6:29am Yes, it's a bit tricky. After those 4 steps comes the loop in which the division is carried out. I will give that code, see if you can figure out how to adapt it for your problem: for(int i = q.Nterms-1; i >= 0; i--)// for each term in quotient (from highest to lowest, like on paper) { q.pCoeffs[i] = r.pCoeffs[r.Nterms-1]/DleadCoeff;// quotient term found trial = Divisor; trial *= q.pCoeffs[i];// trial multiplied by the quotient term just found above. r.Nterms--;// remainder shrinks by 1 term for(int j=0; j< trial.Nterms-1; j++)// find next remainder r.pCoeffs[i+j] -= trial.pCoeffs[j];// only the leading terms of remainder are affected } This loop leaves r as the final remainder and all terms of q have been found so that's the end of the function! |
q.Nterms = length ? |
DleadCoeff = divisor coeff for higher power? |
C(2) = R1(4)/I(3) Aux2 = C(2)*I R2 = R1-Aux2 [....] and so on |