Poly Poly::operator/(const Poly& orig)const //Division Overload { Poly quotient; //list to store the quotient of LHS/RHS Poly dividend = *this; //Copy LHS dividend to new list called dividend while(true) { Poly qTerm; //this operation returns an empty list if dividend is null //the dividend is decremented by an iteration of the quotient (qterm) * divisor //eventually the dividend will be less than the divisor upon which it will return quotient //further each iteration of qterm is accumulated in the quotient //also note when dividend is less than the divisor, this means dividend is now the remainder if((dividend.Head ==NULL)||(orig.Head->exponent > dividend.Head->exponent)) { bool loop = true; for(Node *cur = quotient.Head;(quotient.Head!=NULL) && ((cur!=quotient.Head) || (loop)); cur = cur->next) { loop = false; cur->coefficient = -cur->coefficient; } return quotient; } qTerm.insertPoly(-(dividend.Head->coefficient/orig.Head->coefficient), dividend.Head->exponent-orig.Head->exponent); quotient = quotient + qTerm; //quotient is accumulated dividend = (dividend + (qTerm * orig)); //dividend is decremented with every successfull division iteration } }
Poly Poly::operator%(const Poly& orig)const //Modulus Operator Overload { //this function is exactly the same as the division operation except it returns the decremented dividend instead //of the quotient Poly quotient; Poly dividend = *this; while(true) { Poly qTerm; if((dividend.Head ==NULL)||(orig.Head->exponent > dividend.Head->exponent)) { bool loop = true; for(Node *cur = quotient.Head;(quotient.Head!=NULL) && ((cur!=quotient.Head) || (loop)); cur = cur->next) { loop = false; cur->coefficient = -cur->coefficient; } return dividend; } qTerm.insertPoly(-(dividend.Head->coefficient/orig.Head->coefficient), dividend.Head->exponent-orig.Head->exponent); quotient = quotient + qTerm; dividend = (dividend + (qTerm * orig)); } }
Poly Poly::operator*(const Poly& orig)const //Multiplication Overload { Poly product; //list to store the product of LHS and RHS bool loop = true; bool loop2 = true; for(Node *cur = this->Head; (cur!=this->Head) || (loop); cur=cur->next) //Transverse LHS list { loop = false; for(Node *cur2 = orig.Head; (cur2!=orig.Head) || (loop2); cur2=cur2->next) //Transvers RHS list { loop2 = false; product.insertPoly(cur->coefficient*cur2->coefficient, cur->exponent + cur2->exponent); //Multiply each LHS node with entire RHS list and insert into product } loop2 = true; } return product; }