예제 #1
0
polynomial::polynomial(Term t)
{
    //initializes polynomial to Term specifications
    head->setCoe(t.getCoe());
    head->exp = t.getExp();
    head->next = NULL;
}
예제 #2
0
polynomial polynomial::operator+ (polynomial &other)
{
    polynomial result;  //output vairable
    Term *polyIndex = head;  //lhs variable
    Term *otherIndex = other.head;  //rhs variable
    while (polyIndex != NULL && otherIndex != NULL) //while both polynomials exist
    {
        if (result.head->getExp() == 0 && result.head->getExp() == 0) //if the result is initialized and 'empty'
        {
            if (polyIndex->getExp() > otherIndex->getExp()) //lhs exponent > rhs exponent
            {
                result.head->setCoe(polyIndex->getCoe());
                result.head->setExp(polyIndex->getExp());
                polyIndex = polyIndex->next;
            }
            
            else if (polyIndex->getExp() == otherIndex->getExp()) //lhs exponent = rhs exponent
            {
                int buff = polyIndex->getCoe() + otherIndex->getCoe(); //add coefs
                int ffer = polyIndex->getExp();
                result.head->setCoe(buff);
                result.head->setExp(ffer);
                polyIndex = polyIndex->next;
                otherIndex = otherIndex->next;
            }
            
            else //rhs exponent > lhs exponent
            {
                result.head->setCoe(otherIndex->getCoe());
                result.head->setExp(otherIndex->getExp());
                otherIndex = otherIndex->next;
            }
        }
        
        //one polynomial is empty and the other is not:
        
        if ((polyIndex != NULL && otherIndex != NULL) &&(polyIndex->getExp() > otherIndex->getExp()))
        {
            result.push_back(*polyIndex);
            polyIndex = polyIndex->next;
        }
        
        else if ((polyIndex != NULL && otherIndex != NULL) && (polyIndex->getExp() == otherIndex->getExp()))
        {
            int buff = polyIndex->getCoe() + otherIndex->getCoe();
            int ffer = polyIndex->getExp();
            Term dummy = Term(buff, ffer);
            result.push_back(dummy);
            polyIndex = polyIndex->next;
            otherIndex = otherIndex->next;
        }
        
        else if (polyIndex != NULL && otherIndex != NULL)
        {
            result.push_back(*otherIndex);
            otherIndex = otherIndex->next;
        }
    }
    
    while (polyIndex == NULL && otherIndex != NULL)
    {
        result.push_back(*otherIndex);
        otherIndex = otherIndex -> next;
    }
    
    while (polyIndex != NULL && otherIndex == NULL)
    {
        result.push_back(*polyIndex);
        polyIndex = polyIndex -> next;
    }
    return result;
}