1. 程式人生 > >用連結串列實現一元多項式加減、求導(Java)

用連結串列實現一元多項式加減、求導(Java)

Lnode.java

package PloyItem;  

 /**
*@Author wzy
*@Date 2017年11月12日
*@Version JDK 1.8
*@Description 
*/ 
public class Lnode implements Comparable<Lnode>,Cloneable{  
    public double coef;  
    public int exp;  
    public Lnode next;  
    public Lnode (double coef,int exp){  
        this
.exp=exp; this.coef=coef; next=null; } public Lnode(double coef,int exp,Lnode next){ this.exp=exp; this.coef=coef; this.next=next; } public boolean equals(Object e){ Lnode node=(Lnode)e; return (exp==node.exp); } @Override protected
Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } @Override public int compareTo(Lnode o) { // TODO Auto-generated method stub return 0; } }

PloyItemList.java

package PloyItem;

import
java.text.DecimalFormat; import java.util.Scanner; /** *@Author wzy *@Date 2017年11月12日 *@Version JDK 1.8 *@Description */ public class PloyItemList implements Cloneable{ private int length; DecimalFormat df = new DecimalFormat("######0.0"); Lnode first; @Override protected PloyItemList clone() throws CloneNotSupportedException { // TODO Auto-generated method stub PloyItemList p=(PloyItemList) super.clone(); if(this.first!=null) p.first=(Lnode) this.first.clone(); return p; } public PloyItemList(int length) { first=null; this.length=length; } public PloyItemList(){ first=null; length=0; } public int size()//獲取連結串列的長度 { Lnode p=first; int count=0; while(p!=null&&p.coef!=0){ count++; p=p.next; } return count; } public boolean add(double coef,int exp)//向連結串列當中新增元素的方法 { Lnode p=first,s; s=new Lnode(coef,exp,null); if(first==null){ first=s; s.next=null; return true; } else { while(p.next!=null){ p=p.next; } p.next=s; s.next=null; return true; } } public boolean add(int coef)//向連結串列當中新增元素的方法 { Lnode p=first,s; s=new Lnode(coef,0,null); if(first==null){ first=s; s.next=null; return true; } else { while(p.next!=null){ p=p.next; } p.next=s; s.next=null; return true; } } public void sort(){//對多項式進行降序排列的方法 Lnode p,q;int i,j,m;double n; for( i=0,p=first;i<this.size()-1;i++,p=p.next) for(j=i+1,q=p.next;j<this.size();j++,q=q.next) if(p.exp<q.exp) { m=p.exp;p.exp=q.exp;q.exp=m; n=p.coef;p.coef=q.coef;q.coef=n; } } public void union()//對多項式進行合併同類項的方法 { Lnode p,q,r; sort(); p=first; q=p.next; while(p!=null&& q!=null){ if(p.exp==q.exp) { r=q; p.coef=Double.parseDouble(df.format(p.coef+q.coef)); remove(q.coef,q.exp); p=r; q=r.next; } else { p=q; q=q.next; } } } public void remove (double coef,int exp)//刪除連結串列當中的某一個節點的方法 { Lnode p=first,q=p; for(q=p;q!=null;q=q.next) if(q.next.coef==coef && q.next.exp==exp) break; q.next=q.next.next; } public String toString()//將連結串列轉化為一個字串輸出的方法 { String s=""; Lnode p=first; sort(); union(); while(p!=null){ if(p.coef==0) s=s; else if(p.coef==-1&&p.exp>1) s=s+"-"+"x^"+p.exp+"+"; else if(p.coef==1) s=s+"x"+"x^"+p.exp+"+"; else if(p.exp==0) s=s+p.coef+"+"; else if(p.exp==1) s=s+p.coef+"x"+"+"; else if(p.next!=null&&p.next.coef<0) s=s+p.coef+"x^"+p.exp; else s=s+p.coef+"x^"+p.exp+"+"; p=p.next; } if(s.contains(".0")&&!s.isEmpty()) return s.substring(0, s.length()-1).replace(".0", "")+"\n"; else if(!s.isEmpty())return s.substring(0, s.length()-1); else s="0";return s; } public void addPloyItem(PloyItemList p2)//多項式相加的方法 { this.sort();p2.sort(); Lnode p=this.first,q=p2.first; while(p!=null || q!=null) { if(p!=null && q!=null) { if(p.exp==q.exp){ p.coef+=q.coef; p=p.next;q=q.next; } else if(p.exp<q.exp){ this.add(q.coef, q.exp); q=q.next; } else { this.add(q.coef, q.exp); q=q.next; } } else if(p==null && q!=null) this.add(q.coef, q.exp); else if(p!=null && q==null) p=p.next; } } public void subPloyItem(PloyItemList p2) //多項式相減的方法 { this.sort();p2.sort(); Lnode p=this.first,q=p2.first; while(p!=null || q!=null) { if(p!=null && q!=null) { if(p.exp==q.exp){ p.coef-=q.coef; p=p.next;q=q.next; } else if(p.exp<q.exp){ this.add(-q.coef, q.exp); q=q.next; } else { this.add(-q.coef, q.exp); q=q.next; } } else if(p==null && q!=null) this.add(-q.coef, q.exp); else if(p!=null && q==null) p=p.next; } } public double Evaluation(double x) //多項式求值 { this.sort(); Lnode p=this.first; double sum=0; while (p!=null) { sum+=p.coef*Factorial(x, p.exp); p=p.next; } sum=Double.parseDouble(df.format(sum)); return sum; } public PloyItemList Differentiation()//多項式求導 { this.sort(); Lnode p=this.first;PloyItemList q=new PloyItemList(); while (p!=null) { if(p.exp==0) q.add(0,0); if(p.coef*p.exp!=0) q.add(p.coef*p.exp,(p.exp-1)); p=p.next; } return q; } public double Factorial(double x,int y) { double m=1; while(y>0) { m*=x; y--; } return m; } }

test.java

package PloyItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test {
    public static void main(String[] args) throws CloneNotSupportedException{

        System.out.println("請輸入第一個多項式:");
        PloyItemList p1=shuru();
        PloyItemList s=p1.clone();
        System.out.println("第一個多項式:"+"p1(x)="+p1);
        System.out.println(p1.size());
        /*System.out.println("請輸入第二個多項式:");

        PloyItemList p2=shuru();
        System.out.println("第二個多項式:"+"p2(x)="+p2);  
        System.out.println("--------------------------------------------");
        System.out.println("第一個多項式:"+"p1(x)="+p1); 
        System.out.println("第二個多項式:"+"p2(x)="+p2);  

        System.out.println("------------------給定x求值------------------");
        System.out.println("輸入x求p1,p2的值,請輸入x的值:");
        Scanner in=new Scanner(System.in);
        double x=in.nextDouble();
        System.out.println("p1(x="+x+")="+p1.Evaluation(x));
        System.out.println("p2(x="+x+")="+p2.Evaluation(x));
        System.out.println("------------------求導----------------------");
        System.out.println("第一個多項式求導:"+"p1'(x)="+p1.Differentiation()); 
        System.out.println("第二個多項式求導:"+"p2'(x)="+p2.Differentiation()); 
        System.out.println("--------------------------------------------");
        System.out.println("-----------------加減運算--------------------");
        System.out.println("第一個多項式:"+"p1(x)="+p1); 
        System.out.println("第二個多項式:"+"p2(x)="+p2);  
        p1.subPloyItem(p2);
        System.out.println("兩個多項式相減:"+"p1(x)-p2(x)="+p1);
//      System.out.println(p2);
        p1.addPloyItem(p2);
//      System.out.println(p1);
        p1.addPloyItem(p2);
        System.out.println("兩個多項式相加:"+"p1(x)+p2(x)="+p1);*/


    }
    public static PloyItemList shuru(){
        System.out.println("------------------------------------------");
        @SuppressWarnings("resource")
        Scanner sc=new Scanner(System.in);
        List<String> lits=new ArrayList<>();
        while (true) {
            String s=sc.nextLine();
            if(s.equals("0 0")) break;
            lits.add(s);
        }
        PloyItemList p1=new PloyItemList();
        for (String string : lits) {
            String []t=string.split(" ");
            p1.add(Double.parseDouble(t[0]),Integer.parseInt(t[1]));
        }
        System.out.println("------------------------------------------");
        return p1;

    }
}