Helping About Adding Two Polynamials - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Helping About Adding Two Polynamials

s.smaryams.smaryam Posts: 13Member
Hi Every body
I have problem in adding two polynamilas
Can anybody help?

import weiss.nonstandard.*;
import java.util.Scanner;

//Polynomial class
//
//****************PUBLIC OPERATIONS**************
//static Polynomial add( p1, p2) --> Return sum of 2 polynomials, p1 + p2
//static Polynomial mult( p1, p2) --> Return p1 * p2
//

public class Polynomial
{
private SortedLinkedList poly; // the polynomial as sorted list

/**
* Construct the zero polynomial
*/
public Polynomial( )
{
poly = new SortedLinkedList ( );
}

/**
* Construct the polynomial
* @param s the string contains terms in sequence of coefficient-and-exponent pairs
* for example: 10 100 -2 10 1 0 is 10x^100 -2x^10 + 1
*/
public Polynomial ( String s )
{
double c;
int power = 0;

poly = new SortedLinkedList ( );

Scanner sc = new Scanner (s);
while ( sc.hasNext( ) )
{
c = sc.nextDouble( );
if ( sc.hasNext( ) )
power = sc.nextInt( );
// insert (c, power)
poly.insert( new Term ( c, power) );
}
}

/**
* Return a string representation of the polynomial
*/
public String toString( )
{
StringBuilder s = new StringBuilder( );

if( poly.isEmpty( ) )
return "0" ; // zero polynomial
else
{
LinkedListIterator itr = poly.first( );
if ( itr.isValid( ) ) // print the first term
{
s.append( itr.retrieve( ) );
itr.advance( );
}
for( ; itr.isValid( ); itr.advance( ) )
if ( itr.retrieve( ).getCoeff( ) > 0 )
s.append( " +" + itr.retrieve( )) ;
else
s.append( " " + itr.retrieve( )) ;
}
return s.toString( );

}

/**
* @param p1 the first polynomial
* @param p2 the second polynomial
* @return addition of two polynomials, or p1 + p2
*/
public static Polynomial add( Polynomial p1, Polynomial p2)
{ // your implementation here
Polynomial p = new Polynomial( );
return p;
}


public static void main ( String [] args )
{
Polynomial p1 = new Polynomial ( "-100 1000 10 100 -1 10 20 5 -1 0 ");
Polynomial p2 = new Polynomial ( "50 50 -5 100 1 10 -5 5 4 4 10 0");
System.out.println( p1 );
System.out.println( p2 );

Polynomial p3 = add(p1, p2);
System.out.println( p3 );
}
}

Comments

  • clown1985clown1985 Posts: 1Member
    : Hi Every body
    : I have problem in adding two polynamilas
    : Can anybody help?
    :
    : import weiss.nonstandard.*;
    : import java.util.Scanner;
    :
    : //Polynomial class
    : //
    : //****************PUBLIC OPERATIONS**************
    : //static Polynomial add( p1, p2) --> Return sum of 2 polynomials, p1 + p2
    : //static Polynomial mult( p1, p2) --> Return p1 * p2
    : //
    :
    : public class Polynomial
    : {
    : private SortedLinkedList poly; // the polynomial as sorted list
    :
    : /**
    : * Construct the zero polynomial
    : */
    : public Polynomial( )
    : {
    : poly = new SortedLinkedList ( );
    : }
    :
    : /**
    : * Construct the polynomial
    : * @param s the string contains terms in sequence of coefficient-and-exponent pairs
    : * for example: 10 100 -2 10 1 0 is 10x^100 -2x^10 + 1
    : */
    : public Polynomial ( String s )
    : {
    : double c;
    : int power = 0;
    :
    : poly = new SortedLinkedList ( );
    :
    : Scanner sc = new Scanner (s);
    : while ( sc.hasNext( ) )
    : {
    : c = sc.nextDouble( );
    : if ( sc.hasNext( ) )
    : power = sc.nextInt( );
    : // insert (c, power)
    : poly.insert( new Term ( c, power) );
    : }
    : }
    :
    : /**
    : * Return a string representation of the polynomial
    : */
    : public String toString( )
    : {
    : StringBuilder s = new StringBuilder( );
    :
    : if( poly.isEmpty( ) )
    : return "0" ; // zero polynomial
    : else
    : {
    : LinkedListIterator itr = poly.first( );
    : if ( itr.isValid( ) ) // print the first term
    : {
    : s.append( itr.retrieve( ) );
    : itr.advance( );
    : }
    : for( ; itr.isValid( ); itr.advance( ) )
    : if ( itr.retrieve( ).getCoeff( ) > 0 )
    : s.append( " +" + itr.retrieve( )) ;
    : else
    : s.append( " " + itr.retrieve( )) ;
    : }
    : return s.toString( );
    :
    : }
    :
    : /**
    : * @param p1 the first polynomial
    : * @param p2 the second polynomial
    : * @return addition of two polynomials, or p1 + p2
    : */
    : public static Polynomial add( Polynomial p1, Polynomial p2)
    : { // your implementation here
    : Polynomial p = new Polynomial( );
    : return p;
    : }
    :
    :
    : public static void main ( String [] args )
    : {
    : Polynomial p1 = new Polynomial ( "-100 1000 10 100 -1 10 20 5 -1 0 ");
    : Polynomial p2 = new Polynomial ( "50 50 -5 100 1 10 -5 5 4 4 10 0");
    : System.out.println( p1 );
    : System.out.println( p2 );
    :
    : Polynomial p3 = add(p1, p2);
    : System.out.println( p3 );
    : }
    : }
    :


    Are you studying in Kristianstad University in Sweden ????????
Sign In or Register to comment.