Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C++ (https://www.askmehelpdesk.com/forumdisplay.php?f=439)
-   -   Write a program to Overload the addition operator (+) to add two Polynomials. (https://www.askmehelpdesk.com/showthread.php?t=528112)

  • Nov 22, 2010, 05:29 AM
    fiza_murtaza
    write a program to Overload the addition operator (+) to add two Polynomials.
    Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term 2x4
    has a coefficient of 2 and an exponent of 4. Develop a full class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities:
    a) Overload the addition operator (+) to add two Polynomials.
    b) Overload the subtraction operator (-) to subtract two Polynomials.
    c) Overload the assignment operator (=) to assign one Polynomial to another.
    d) Overload the multiplication operator (*) to multiply two Polynomials.

    Sample output:


    Enter number of polynomial terms: 2
    Enter coefficient: 2
    Enter exponent: 2
    Enter coefficient: 3
    Enter exponent: 3
    Enter number of polynomial terms: 3
    Enter coefficient: 1
    Enter exponent: 1
    Enter coefficient: 2
    Enter exponent: 2
    Enter coefficient: 3
    Enter exponent: 3
    First polynomial is:
    2x^2+3x^3
    Second polynomial is:
    1x+2x^2+3x^3
    Adding the polynomials yields:
    1x+4x^2+6x^3
    Subtracting the polynomials yields:
    -1x
    Multiplying the polynomials yields:
    2x^3+7x^4+12x^5+9x^6 class Polynomial
  • Mar 21, 2011, 03:40 AM
    vazkardutta
    //Add and subtract two polynomials (Using Linked List)

    #include<iostream.h>
    #include<conio.h>
    #include<process.h>

    // Creating a NODE Structure
    struct node
    {
    int coe,exp; // data
    struct node *next; // link to next node and previous node
    };

    // Creating a class Polynomial
    class polynomial
    {
    struct node *start,*ptrn,*ptrp;
    public:
    void get_poly(); // to get a polynomial
    void show(); // show

    void add(polynomial p1,polynomial p2); // Add two polynomials
    void subtract(polynomial p1,polynomial p2); //Subtract2
    //polynomials
    };

    void polynomial::get_poly() // Get Polynomial
    {
    char c='y';
    ptrn=ptrp=start=NULL;
    while(c=='y' || c=='Y')
    {
    ptrn=new node;
    ptrp->next=ptrn;
    if(start==NULL)
    start=ptrn;
    ptrp=ptrn;
    cout<<"\nEnter the coefficient: ";
    cin>>ptrn->coe;
    cout<<"\nEnter the exponent: ";
    cin>>ptrn->exp;
    ptrn->next=NULL;
    cout<<"\nEnter y to add more nodes: ";
    cin>>c;
    }
    return;
    }


    void polynomial::show() // Show Polynomial
    {
    struct node *ptr;
    ptr=start;
    while(ptr!=NULL)
    {
    cout<<ptr->coe<<"X^"<<ptr->exp<<" + ";
    ptr=ptr->next;
    }
    cout<<" ";
    }

    void polynomial::add(polynomial p1,polynomial p2) // Add Polynomials
    {
    struct node *p1ptr,*p2ptr;
    int coe,exp;
    ptrn=ptrp=start=NULL;
    p1ptr=p1.start;
    p2ptr=p2.start;
    while(p1ptr!=NULL && p2ptr!=NULL)
    {
    if(p1ptr->exp==p2ptr->exp) // If exponents are equal
    {
    coe=p1ptr->coe+p2ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    p2ptr=p2ptr->next;
    }
    else if(p1ptr->exp>p2ptr->exp)
    {
    coe=p1ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    }
    else if(p1ptr->exp<p2ptr->exp)
    {
    coe=p2ptr->coe;
    exp=p2ptr->exp;
    p2ptr=p2ptr->next;
    }
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;ptrn->exp=exp;
    ptrn->next=NULL;ptrp->next=ptrn; ptrp=ptrn;
    } // End of While
    /* if(p1ptr==NULL)
    {
    while(p2ptr!=NULL)
    {
    coe=p2ptr->coe;
    exp=p2ptr->exp;
    p2ptr=p2ptr->next;
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
    }
    }
    else if(p2ptr==NULL)
    {
    while(p1ptr!=NULL)
    {
    coe=p1ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
    }
    } */
    } // End of addition

    // Subtract two polynomials
    void polynomial::subtract(polynomial p1,polynomial p2) // Subtract
    {
    struct node *p1ptr,*p2ptr;
    int coe,exp;
    ptrn=ptrp=start=NULL;
    p1ptr=p1.start;
    p2ptr=p2.start;
    while(p1ptr!=NULL && p2ptr!=NULL)
    {
    if(p1ptr->exp==p2ptr->exp) // If coefficients are equal
    {
    coe=p1ptr->coe-p2ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    p2ptr=p2ptr->next;
    }
    else if(p1ptr->exp>p2ptr->exp)
    {
    coe=p1ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    }
    else if(p1ptr->exp<p2ptr->exp)
    {
    coe=0-p2ptr->coe;
    exp=p2ptr->exp;
    p2ptr=p2ptr->next;
    }
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
    } // End of While
    if(p1ptr==NULL)
    {
    while(p2ptr!=NULL)
    {
    coe=0-p2ptr->coe;
    exp=p2ptr->exp;
    p2ptr=p2ptr->next;
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
    }
    }
    else if(p2ptr==NULL)
    {
    while(p1ptr!=NULL)
    {
    coe=p1ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
    }
    }
    } // End of subtraction


    int main()
    {
    clrscr();
    polynomial p1,p2,p3,sum,diff;
    cout<<"\nFirst Polynomial.";
    p1.get_poly();
    cout<< "\nSecond polynomial.";
    p2.get_poly();
    clrscr();
    cout<<"\nThe First polynomial is: ";
    p1.show();
    cout<<"\nThe second polynomial is: ";
    p2.show();
    cout<<"\nThe sum of two polynomials is: ";
    sum.add(p1,p2);
    sum.show();
    cout<<"\nThe difference of two polynomials is: ";
    diff.subtract(p1,p2);
    diff.show();
    getch();
    return 0;
    }

  • All times are GMT -7. The time now is 02:39 PM.