Thursday, August 7, 2008

Program to Add two polynomials

Program to Add two polynomials(Source Code)
// Program Ch02pr05
// Program to Add two polynomials

#include <iostream>
#include <conio.h>
using namespace std;

const int MAX = 10 ;



class poly

{

    private :



        struct term

        {

            int coeff ;

            int exp ;

        } t[MAX] ;

        int noofterms ;



    public :



        poly( ) ;

        void polyappend ( int c, int e ) ;

        void polyadd ( poly &amp;p1, poly &amp;p2 ) ;

        void display( ) ;

} ;



// initializes data members of class poly

poly :: poly( )

{

    noofterms = 0 ;

    for ( int i = 0 ; i &lt; MAX ; i++ )

    {

        t[i].coeff = 0 ;

        t[i].exp = 0 ;

    }

}



// adds the term of polynomial to the array t

void poly :: polyappend ( int c, int e )

{

    t[noofterms].coeff = c ;

    t[noofterms].exp =  e ;

    noofterms++ ;

}



// displays the polynomial equation

void poly :: display( )

{

    int flag = 0 ;

    for ( int i = 0 ; i &lt; noofterms ; i++ )

    {

        if ( t[i].exp != 0 )

            cout &lt;&lt; t[i].coeff &lt;&lt; "x^" &lt;&lt; t[i].exp  &lt;&lt; " + " ;

        else

        {

            cout &lt;&lt; t[i].coeff ;

            flag = 1 ;

        }

    }

    if ( !flag )

        cout &lt;&lt; "\b\b  " ;

}



// adds two polynomials p1 and p2

void poly :: polyadd ( poly&amp; p1, poly&amp; p2 )

{

    int c = p1.noofterms &gt; p2.noofterms ? p1.noofterms : p2.noofterms ;



    for ( int i = 0, j = 0 ; i &lt;= c ; noofterms++ )

    {

        if ( p1.t[i].coeff == 0 &amp;&amp; p2.t[j].coeff == 0 )

            break ;

        if ( p1.t[i].exp &gt;= p2.t[j].exp )

        {

            if ( p1.t[i].exp == p2.t[j].exp )

            {

                t[noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;

                t[noofterms].exp = p1.t[i].exp ;

                i++ ;

                j++ ;

            }

            else

            {

                t[noofterms].coeff = p1.t[i].coeff ;

                t[noofterms].exp = p1.t[i].exp ;

                i++ ;

            }

        }

        else

        {

            t[noofterms].coeff = p2.t[j].coeff ;

            t[noofterms].exp = p2.t[j].exp ;

            j++ ;

        }

    }

}



int main( )

{

    poly p1 ;



    p1.polyappend ( 1, 7 ) ;

    p1.polyappend ( 2, 6 ) ;

    p1.polyappend ( 3, 5 ) ;

    p1.polyappend ( 4, 4 ) ;

    p1.polyappend ( 5, 2 ) ;



    poly p2 ;

    p2.polyappend ( 1, 4 ) ;

    p2.polyappend ( 1, 3 ) ;

    p2.polyappend ( 1, 2 ) ;

    p2.polyappend ( 1, 1 ) ;

    p2.polyappend ( 2, 0 ) ;



    poly p3 ;

    p3.polyadd ( p1, p2 ) ;



    cout &lt;&lt; endl &lt;&lt; "First polynomial:" &lt;&lt; endl  ;

    p1.display( ) ;



    cout &lt;&lt; endl &lt;&lt; "Second polynomial:" &lt;&lt; endl ;

    p2.display( ) ;



    cout &lt;&lt; endl &lt;&lt; "Resultant polynomial:" &lt;&lt; endl ;

    p3.display( ) ;

    getch();

}

0 comments:

Post a Comment