#include #define MAX 10 struct term struct poly void initpoly ( struct poly * ) ; void main( ) clrscr( ) ; initpoly ( &p1 ) ; polyappend ( &p1, 1, 7 ) ; polyappend ( &p2, 1, 4 ) ; p3 = polyadd ( p1, p2 ) ; printf ( “\nFirst polynomial:\n” ) ; printf ( “\n\nSecond polynomial:\n” ) ; printf ( “\n\nResultant polynomial:\n” ) ; getch( ) ; /* initializes elements of struct poly */ /* adds the term of polynomial to the array t */ /* displays the polynomial equation */ } /* adds two polynomials p1 and p2 */ if ( p1.noofterms > p2.noofterms ) for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ )
#include
{
int coeff ;
int exp ;
} ;
{
struct term t [10] ;
int noofterms ;
} ;
void polyappend ( struct poly *, int c, int e ) ;
struct poly polyadd ( struct poly, struct poly ) ;
void display ( struct poly ) ;
{
struct poly p1, p2, p3 ;
initpoly ( &p2 ) ;
initpoly ( &p3 ) ;
polyappend ( &p1, 2, 6 ) ;
polyappend ( &p1, 3, 5 ) ;
polyappend ( &p1, 4, 4 ) ;
polyappend ( &p1, 5, 2 ) ;
polyappend ( &p2, 1, 3 ) ;
polyappend ( &p2, 1, 2 ) ;
polyappend ( &p2, 1, 1 ) ;
polyappend ( &p2, 2, 0 ) ;
display ( p1 ) ;
display ( p2 ) ;
display ( p3 ) ;
}
void initpoly ( struct poly *p )
{
int i ;
p -> noofterms = 0 ;
for ( i = 0 ; i < MAX ; i++ )
{
p -> t[i].coeff = 0 ;
p -> t[i].exp = 0 ;
}
}
void polyappend ( struct poly *p, int c, int e )
{
p -> t[p -> noofterms].coeff = c ;
p -> t[p -> noofterms].exp = e ;
( p -> noofterms ) ++ ;
}
void display ( struct poly p )
{
int flag = 0, i ;
for ( i = 0 ; i < p.noofterms ; i++ )
{
if ( p.t[i].exp != 0 )
printf ( “%d x^%d + “, p.t[i].coeff, p.t[i].exp ) ;
else
{
printf ( “%d”, p.t[i].coeff ) ;
flag = 1 ;
}
}
if ( !flag )
printf ( “\b\b ” ) ;
struct poly polyadd ( struct poly p1, struct poly p2 )
{
int i, j, c ;
struct poly p3 ;
initpoly ( &p3 ) ;
c = p1.noofterms ;
else
c = p2.noofterms ;
{
if ( p1.t[i].coeff == 0 && p2.t[j].coeff == 0 )
break ;
if ( p1.t[i].exp >= p2.t[j].exp )
{
if ( p1.t[i].exp == p2.t[j].exp )
{
p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;
p3.t[p3.noofterms].exp = p1.t[i].exp ;
i++ ;
j++ ;
}
else
{
p3.t[p3.noofterms].coeff = p1.t[i].coeff ;
p3.t[p3.noofterms].exp = p1.t[i].exp ;
i++ ;
}
}
else
{
p3.t[p3.noofterms].coeff = p2.t[j].coeff ;
p3.t[p3.noofterms].exp = p2.t[j].exp ;
j++ ;
}
}
return p3 ;
}
Labels
- ajax (1)
- Assembly Programming (3)
- Books on C++ (6)
- C Programs (35)
- Challenging Question (1)
- Class (1)
- Complete Script (1)
- Computer Graphics (1)
- CSS (2)
- Datastructure (6)
- Download (3)
- e-books (8)
- Errors (1)
- FTP (2)
- HTML (3)
- Interview Questions (8)
- Introduction (1)
- Methods (2)
- OOP (1)
- Operator Overloading (1)
- Operators (1)
- Others (5)
- PHP (4)
- PHP Errors (1)
- PHP Script (1)
- Programming (3)
- Question Bank (6)
- Short Questions (1)
- SQL (1)
- Tips (1)
- Tricks (1)
- Tricky Programs in C (2)
- Useful Scripts (1)
- WAP (1)
- Web (3)
- XHTML (1)
Addition of two polynomials in c
Sunday, August 29, 2010Posted by Xofth at 10:34 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment