多項式の表示と演算


加算可能インタフェース
public interface Addible                      //     
{
    public void add(T t);                        //+=  ,         
    public boolean removable();                  //        
}

アイテムクラス、一元多項式のアイテム、比較可能なインタフェースと加算可能なインタフェースを実現
public class TermX implements Comparable,Addible 
{
    protected int coef, exp;                               //  coefficient   exponent(   、0)
 
    public TermX(int coef, int exp)                        //    
    {
        this.coef = coef;
        this.exp = exp;
    }
    public TermX(TermX term)                               //      
    {
        this(term.coef, term.exp);
    }
    
    // “  x^  ”               。
    //      :    1 -1   >0 ,  1,-1    “-”, x^2、-x^3;
    //    0 ,  x^0,    ;    1 ,  ^1,  x。
    public TermX(String termstr)                           
    {
        if (termstr.charAt(0)=='+')                        //  + 
            termstr=termstr.substring(1);
        int i = termstr.indexOf('x');
        if (i==-1)                                         //  x,    0
        {
            this.coef = Integer.parseInt(termstr);         //    
            this.exp = 0;
        }
        else                                               // x,x     ,x^     
        {
            if (i==0)                                      // x  ,    1
                this.coef = 1;
            else
            {
                String sub=termstr.substring(0,i);         //x        
                if (sub.equals("-"))                       //    - ,    1
                    this.coef=-1;
                else
                    this.coef = Integer.parseInt(sub);     //    
            }
            i = termstr.indexOf('^');
            if (i==-1)
                 this.exp=1;                               //  ^,    1
            else
                 this.exp = Integer.parseInt(termstr.substring(i+1));//    
        }
    }

    //             “  x^  ”        ,       TermX(String)    。
    public String toString()                     
    {
        String str=this.coef>0 ? "+" : "-";                //      
        if (this.exp==0 || this.exp>0 && this.coef!=1 && this.coef!=-1)
            str+=Math.abs(this.coef);                      //     ,    1
        if (this.exp>0)
            str+="x";                                      //   0 ,  x^0,    
        if (this.exp>1)
            str+="^"+this.exp;                             //   1 ,  ^1,  x
        return str;
    }

    public int compareTo(TermX term)                       //           
    {
        if (this.exp == term.exp)                          //    
            return 0;                                      //     equals(Object obj)  
        return this.exp

多項式並べ替え単鎖表クラス、並べ替え単鎖表クラスを継承し、並べ替え単鎖表の多項式加算演算を提供する.
public class PolySLinkedList & Addible> extends SortedSinglyLinkedList
{
    public PolySLinkedList()                               //      
    {
        super();                                           //      ,             
    }
    public PolySLinkedList(T terms[])                      //            
    {
        super(terms);
    }
    public PolySLinkedList(PolySLinkedList polylist)    //       
    {
        super(polylist);                                   //      ,      ,        
    }
       
    public void add(PolySLinkedList polylist)           //     ,this+=polylist
    {
        Node front=this.head, p=front.next;
        Node q=polylist.head.next;
        while (p!=null && q!=null)
            if (p.data.compareTo(q.data)==0)               //      
            {
                p.data.add(q.data);                        //    ,add()   Addible    
                if (p.data.removable())                    //           
                {                                          //removable()   Addible    
                    front.next=p.next;                     //          ,  p  
                    p=front.next;
                }
                else 
                {
                    front = p;                             //front p     
                    p = p.next;
                }
                q = q.next;
            }
            else if (p.data.compareTo(q.data)<0)
                 {
                     front = p;       
                     p = p.next;
                 }
                 else
                 {
                     front.next = new Node(q.data, p);  //  q      front    
                     q = q.next;
                 }
        while (q!=null)                                    // polylist                   
        {
            front.next = new Node(q.data, null);
            front = front.next;
            q = q.next;
        }
    }
}
/* 
public void insert(Term term)                          //   
{
    list.insert(term);                                 //           ,     term     
}*/
/*
//(2)        
public Polynomial plus(Polynomial pol)                 //  +,C=this+pol
{
    Polynomial cpol = new Polynomial();
    Node p=this.list.head.next;            
    Node q=pol.list.head.next;
    Node rear=cpol.list.head;
    while (p!=null && q!=null)
    {
        if (p.data.compareTo(q.data)==0)               //       
        {
            double sum=p.data.coef + q.data.coef;      //      
            if (Math.abs(sum)>0.00001)                 //      0     
            {
                rear.next=new Node(new Term(sum, p.data.exp), null);//      0    
                rear=rear.next;
            }
            p = p.next;
            q = q.next;
        }
        else if (p.data.compareTo(q.data)<0)
        {
            rear.next = new Node(p.data, null);  //  p      rear    
            rear=rear.next;
            p = p.next;
        }
        else
        {
            rear.next = new Node(q.data, null);  //  q      rear    
            rear=rear.next;
            q = q.next;
        }
    }
    if (p==null)
        p=q;
    while (p!=null)                                    //      pol             cpol   
    {
        rear.next = new Node(p.data, null);
        rear=rear.next;
        p = p.next;
    }
    return cpol;  
}
    public PolySLinkedList plus(PolySLinkedList polylist)    //  +,C=this+polylist
    {
    	PolySLinkedList polyc=new PolySLinkedList(this);   //   
        polyc.add(polylist);
        return polyc;                                      //      
    }*/

メンバー変数として単一チェーンテーブルクラスを並べ替え、多項式加算演算を提供する多項式クラス
public class Polynomial                                    //    
{
    private PolySLinkedList list;                   //        ,TermX          

    public Polynomial()                                    //      
    {
        this.list = new PolySLinkedList();          //      ,             
    }
    public Polynomial(TermX terms[])                       //    ,            
    {
        this.list = new PolySLinkedList(terms);
    }
    public Polynomial(String polystr)                      //    ,               
    {
        this();
        if (polystr==null || polystr.length()==0)
            return;
        Node rear = this.list.head;
        int start=0, end=0;                                //  start~end      
        while (start(new TermX(polystr.substring(start,end)), null);
                       //   ,   start~end       ,    ,      
            rear = rear.next; 
            start=end;
        }
    }
    
    public Polynomial(Polynomial poly)                     //        ,               
    {
        this();                                            //      ,     
        Node p = poly.list.head.next; 
        Node rear = this.list.head;
        while (p!=null)
        {
            rear.next = new Node(new TermX(p.data), null);//    ,      
            rear = rear.next; 
            p = p.next;
        }
    }
    
    public String toString()                               //           
    {
        String str="";
        for (Node p=this.list.head.next;  p!=null;  p=p.next)
            str+=p.data.toString();
        return str;
    }

    public void add(Polynomial poly)                       //     ,this+=poly
    {
        this.list.add(poly.list);
    }
    public Polynomial plus(Polynomial poly)                //  +,C=this+poly
    {
    	Polynomial polyc=new Polynomial(this);             //    ,           
        polyc.add(poly);
        return polyc;                                      //      
    }
    
    public boolean equals(Object obj)                      //           
    {
        return this==obj || obj instanceof Polynomial && this.list.equals(((Polynomial)obj).list);  
                                                           //           
    }
}
/*  
    public Polynomial(String var, String polystr)                        //            
    {
        this();
        if (polystr.length()==0)
        	return;
        Node rear = this.list.head;
        int start=0, end=0;                       //  start~end     1 
        while (start(new TermX(polystr.substring(start,end)), null);
            if (var.equals("xy"))
//                rear.next = new Node(new TermXY(polystr.substring(start,end)), null);
            rear = rear.next; 
            start=end;
        }
    }

}
   /* 
    public void insert(Term term)                          //   
    {
        list.insert(term);                                 //           ,     term     
    }
    public double value(TermX term)                        //     
    {
        double sum=0;
        Node p=this.list.head.next;
        while (p!=null)
        {
             sum+=p.data.value(x);                         //     
             p=p.next;
        }
        return sum;
    }

public double value(double x)//, double y)                         //  
{
    double sum=0;
    Node p=list.head.next;
    while (p!=null)
    {
         sum+=p.data.value(x);
         p=p.next;
    }
    return sum;
}
    

 */