ColdFusion

Moderators: None (Apply to moderate this forum)
Number of threads: 19
Number of posts: 37

This Forum Only

Helping About Adding Two Polynamials Posted by s.smaryam on 5 Jan 2006 at 9:27 AM
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<Term> poly; // the polynomial as sorted list

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

/**
* 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<Term> ( );

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<Term> 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 );
}
}
Re: Helping About Adding Two Polynamials Posted by clown1985 on 7 Jan 2007 at 11:11 AM
: 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<Term> poly; // the polynomial as sorted list
:
: /**
: * Construct the zero polynomial
: */
: public Polynomial( )
: {
: poly = new SortedLinkedList<Term> ( );
: }
:
: /**
: * 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<Term> ( );
:
: 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<Term> 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 ????????

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.