i really dont understand what this problem all about
Write a program in ‘C’ programming language to add two polynomials using linked list?
The question is about writing a programme that will accept two polynomial equasions and add them. As an example:
3x^2 + 2x + 1
2x^2 - x +1
to yield:
3x^2 + x + 2
Further, the programme should represent the equasions as a linked list.
If you are still scratching your crust over this, then have a butcher's at the following code which reads two equasions from the command line and adds them together. A sample run is at the bottom. Best of luck.
#include %26lt;stdlib.h%26gt;
#include %26lt;unistd.h%26gt;
#include %26lt;string.h%26gt;
struct ele {
struct ele *next;
int coef;
int power;
};
/* read an equasion and build a linked list
equasion must be of the form nxp {+\-} nxp...
where n is the coefficent of x and p is the power (3x2 meaning
3-x-squared). The equasion must be oredered in decending power
and things like 0x3 can be omitted, and the last (constant)
does not need x0. 1x2 and x2 are the same.
*/
struct ele *build( char *buf )
{
struct ele *expr = NULL;
struct ele *tail = NULL;
struct ele *cep = NULL;
char *tok;
char *tp;
int sign = 1;
tok = strtok( buf, " \t" );
while( tok )
{
if( !isdigit( *tok ) %26amp;%26amp; !*(tok+1) )
{
switch( *tok )
{
case '+': sign = 1; break;
case '-': sign = -1; break;
}
}
else
{
cep = (struct ele *) malloc( sizeof( struct ele ) );
memset( cep, 0, sizeof( *cep ) );
if( !tail )
expr = tail = cep;
else
{
tail-%26gt;next = cep;
tail = cep;
}
if( *tok == 'x' )
cep-%26gt;coef = 1;
else
cep-%26gt;coef = atoi( tok ) * sign;
if( (tp = strchr( tok, 'x' )) )
if( *(tp+1)
cep-%26gt;power = atoi( tp+1 );
else
cep-%26gt;power = 1;
sign = 1;
}
tok = strtok( NULL, " \t" );
}
return expr;
}
/* add 2 expressions together and return an expression as result */
struct ele *add( struct ele *e1, struct ele *e2 )
{
struct ele *cep = NULL;
struct ele *result = NULL;
struct ele *tail = NULL;
while( e1 || e2 )
{
cep = (struct ele *) malloc( sizeof( struct ele ) );
memset( cep, 0, sizeof( *cep ) );
if( !tail )
result = tail = cep;
else
{
tail-%26gt;next = cep;
tail = cep;
}
if( (e1 %26amp;%26amp; e2) %26amp;%26amp; e1-%26gt;power == e2-%26gt;power )
{
cep-%26gt;power = e1-%26gt;power;
cep-%26gt;coef = e1-%26gt;coef + e2-%26gt;coef;
e1 = e1-%26gt;next;
e2 = e2-%26gt;next;
}
else
{
if( e1 %26amp;%26amp; (e2 == NULL || e1-%26gt;power %26gt; e2-%26gt;power) )
{
cep-%26gt;power = e1-%26gt;power;
cep-%26gt;coef = e1-%26gt;coef;
e1 = e1-%26gt;next;
}
else
{
cep-%26gt;power = e2-%26gt;power;
cep-%26gt;coef = e2-%26gt;coef;
e2 = e2-%26gt;next;
}
}
}
return result;
}
void print( struct ele *ep )
{
while( ep )
{
if( ep-%26gt;coef != 0 )
switch( ep-%26gt;power )
{
default:
printf( "%+dx^%d ", ep-%26gt;coef, ep-%26gt;power );
break;
case 1:
printf( "%+dx ", ep-%26gt;coef );
break;
case 0:
printf( "%+d", ep-%26gt;coef );
break;
}
ep = ep-%26gt;next;
}
printf( "\n" );
}
int main( int argc, char **argv )
{
struct ele *expr1 = NULL;
struct ele *expr2 = NULL;
struct ele *sum = NULL;
expr1 = build( argv[1] );
expr2 = build( argv[2] );
sum = add( expr1, expr2 );
print( expr1 );
print( expr2 );
print( sum );
}
======== sample output ===========
Yes, input format and output format are different on purpose!
$a.out "8x3 + 3x2 +2x1 + 1" "4x3 - 3x2 - 3"
+8x^3 +3x^2 +2x +1
+4x^3 -3x^2 -3
+12x^3 +2x -2
I wrote the parser to accept spaces round the addition subtraction, and it should get it right if someone enters - -3x2; it will interpret it as +3x2.
To keep the example simple there is no error checking, and it assumes that both equasions are entered in decending order of powers of x. It does allow the omission of values where the coefficent is 0, and allows x1 to be x.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment