溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java如何解實現(xiàn)一元稀疏多項式計算器

發(fā)布時間:2021-12-27 10:10:00 來源:億速云 閱讀:231 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)Java如何解實現(xiàn)一元稀疏多項式計算器,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

要求:

一元稀疏多項式計算器

【問題描述】 設(shè)計一個一元稀疏多項式簡單計算器。

【基本要求】一元稀疏多項式簡單計算器的基本功能是:

(1) 輸入并建立多項式 ;

(2) 輸出多項式,輸出形式為整數(shù)序列:n,c1,e1,c2,e2,…,cn,en,其中n是多項式的項數(shù),ci 和ei,分別是第 i 項的系數(shù)和指數(shù),序列按指數(shù)降序排列;

(3) 多項式a和b相加,建立多項式a +b;

(4) 多項式a和b相減,建立多項式a -b 。

【測試數(shù)據(jù)】
1)(2x+5x8-3.1x11) + (7-5x8+11x9)=(-3.1x11+11x9+2x+7)
2)(6x-3-x+4.4x2-1.2x9) -(-6x-3+5.4x2-x2+7.8x15)=(-7.8x15-1.2x9+12x-3-x)
3)(1 +x + x2+x3+x4+x5)+(-x3-x4)=(1+x+x2+x5)
4)(x+x3)+(-x-x3)=0
5)(x+x100)+(x100 +x200)=(x+2x100+x200)
6)(x+x2+x3)+0=x+x2+x3
7) 互換上述測試數(shù)據(jù)中的前后兩個多項式

【實現(xiàn)提示】 用帶表頭結(jié)點的單鏈表存儲多項式。

【選作內(nèi)容】
1) 計算多項式在x處的值。
2) 求多項式 a 的導函數(shù) 。
3) 多項式a和b相乘,建立乘積多項式ab 。
4) 多項式的輸出形式為類數(shù)學表達式。例如 ,多項式 -3x8+6x3-18 的輸出形式為-3x8+6x3-18,x15+(-8)x7-14的輸出形式為s15+(-8)x7-14。注意,數(shù)值為1的非零次項的輸出形式中略去系數(shù)1,如項1x8的輸出形式為x8,項 -1x3的輸出形式為-x3。
**

實現(xiàn):

Main類

package usps;
/*
吳樂 漢江師范學院 軟件工程2001班
數(shù)據(jù)結(jié)構(gòu)期末大作業(yè) 一元稀疏多項式計算器
開始  2021.12.18 22:00
完成  2021.12.26 00:21
優(yōu)化  2021.12.26 18:00
*/
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        Polynomial poly = new Polynomial();
        int num;
        do
        {//菜單
            System.out.println("\n                  一元稀疏多項式計算器");
            System.out.println("——————————————————————————————————————————————————————————");
            System.out.println(  "1.建立多項式a      2.建立多項式a+b       3.建立多項式a-b  " +
                    "\n4.建立多項式axb    5.多項式a的導函數(shù)      6.多項式在x處的值\n7.退出");
            System.out.println("——————————————————————————————————————————————————————————");
            System.out.print("命令: ");
            num=in.nextInt();
            switch (num)//命令
            {
                case 1://建立多項式
                {
                    System.out.println("請輸入多項式:");
                    System.out.println("項的個數(shù)n <系數(shù) 指數(shù)>*n");
                    LinkList list = poly.inPoly();
                    System.out.print("\n多項式: ");
                    list.allPut();
                }break;

                case 2://多項式 a + b
                {
                    System.out.println("請輸入多項式a:");
                    System.out.println("項的個數(shù)n <系數(shù) 指數(shù)>*n");
                    LinkList listA = poly.inPoly();
                    System.out.println("請輸入多項式b:");
                    LinkList listB = poly.inPoly();
                    System.out.print("\n多項式 a : ");
                    listA.allPut();
                    System.out.print("\n多項式 b : ");
                    listB.allPut();
                    System.out.print("\n多項式 a+b : ");
                    poly.addPoly(listA,listB).allPut();
                }break;

                case 3://多項式 a - b
                {
                    System.out.println("請輸入多項式a:");
                    System.out.println("項的個數(shù)n <系數(shù) 指數(shù)>*n");
                    LinkList listA = poly.inPoly();
                    System.out.println("請輸入多項式b:");
                    LinkList listB = poly.inPoly();
                    System.out.print("\n多項式 a : ");
                    listA.allPut();
                    System.out.print("\n多項式 b : ");
                    listB.allPut();
                    System.out.print("\n多項式 a-b : ");
                    poly.minusPoly(listA,listB).allPut();
                }break;

                case 4://建立多項式axb
                {
                    System.out.println("請輸入多項式a:");
                    System.out.println("項的個數(shù)n <系數(shù) 指數(shù)>*n");
                    LinkList listA = poly.inPoly();
                    System.out.println("請輸入多項式b:");
                    LinkList listB = poly.inPoly();
                    System.out.print("\n多項式 a : ");
                    listA.allPut();
                    System.out.print("\n多項式 b : ");
                    listB.allPut();
                    System.out.print("\n多項式 axb : ");
                    poly.mulPoly(listA,listB).allPut();
                }break;

                case 5://多項式 a 的導函數(shù)
                {
                    System.out.println("請輸入多項式a:");
                    System.out.println("項的個數(shù)n <系數(shù) 指數(shù)>*n");
                    LinkList listA = poly.inPoly();
                    System.out.print("\n多項式 a : ");
                    listA.allPut();
                    System.out.print("\n多項式 a 的導函數(shù): ");
                    poly.derfPoly(listA).allPut();
                }break;

                case 6://多項式在x處的值
                {
                    System.out.println("請輸入多項式a:");
                    System.out.println("項的個數(shù)n <系數(shù) 指數(shù)>*n");
                    LinkList listA = poly.inPoly();
                    System.out.println("請輸入 x : ");
                    double x = in.nextDouble();
                    System.out.print("\n多項式 a : ");
                    listA.allPut();
                    System.out.print("\nx: "+x);
                    System.out.printf("\na(x)為(保留三位小數(shù)): %.3f",poly.getXValue(listA,x));
                }break;

                case 7://退出
                {
                    System.out.println("再見");
                }break;

                default:System.out.println("輸入錯誤了你個蠢蛋");
            }
        }while(num!=7);
    }
}

Node類

package usps;

public class Node
{
    Node next;//下一個結(jié)點
    double coefficient;//系數(shù)
    double index;//指數(shù)

    public Node(){
        super();
    }
    public Node(Node next)
    {
        this.next=next;
    }
    public Node(Node next,double coefficient, double index)
    {
        this.next=next;
        this.coefficient=coefficient;//系數(shù)
        this.index=index;//指數(shù)
    }

    public double getCoefficient() {
        return coefficient;
    }

    public void setCoefficient(double coefficient) {
        this.coefficient = coefficient;
    }

    public double getIndex() {
        return index;
    }

    public void setIndex(double index) {
        this.index = index;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

LinkLsit類

package usps;

public class LinkList {
    Node head;//頭結(jié)點
    int length;//單鏈表長度

    public LinkList()//構(gòu)造空鏈表
    {
        length = 0;
        head = new Node(null);
    }

    public void add(double s1,double s2, int pos)//在鏈表中加入數(shù)據(jù)
    {
        int num=1;
        Node p=head;
        Node q=head.next;
        while(num<pos)
        {
            p=q;
            q=q.next;
            num++;
        }
        p.next=new Node(q,s1,s2);//頭結(jié)點不存放數(shù)據(jù)
        length++;
    }

    public void allPut()//鏈表全部輸出
    {
        if(isEmpty())
        {
            System.out.println("null");
        }
        else
        {
            Node p=head.next;

            System.out.print("(");
            if(p.coefficient!=0)//系數(shù)不等于0才會輸出。
            {
                if(p.coefficient!=1.0) //如果系數(shù)等于1就不用輸出
                {
                    if(p.coefficient == -1 && p.index != 0)
                        System.out.print("-");
                    else
                        System.out.print(p.coefficient);//輸出系數(shù)
                }
                if(p.coefficient == 1.0 && p.index ==0)
                {
                    System.out.println(1);
                }

                if(p.index != 0 && p.index != 1.0)
                {
                    System.out.print("X^" + p.index);
                }
                else if(p.index == 1.0)//如果指數(shù)等于1,就不輸出指數(shù)
                {
                    System.out.print("X");
                }
            }

            p=p.next;

            while(p!=null)
            {
                if(p.coefficient!=0)//系數(shù)不等于0才會輸出。
                {
                    if(p.coefficient>0)
                    {
                        System.out.print("+");//如果系數(shù)大于0,前面就帶+號
                    }

                    if(p.coefficient!=1) //如果系數(shù)等于1就不用輸出
                    {
                        if(p.coefficient == -1 && p.index != 0)
                            System.out.print("-");
                        else
                            System.out.print(p.coefficient);//輸出系數(shù)
                    }

                    if(p.coefficient == 1 && p.index == 0)
                    {
                        System.out.print(1);
                    }

                    if(p.index != 0 && p.index != 1.0)
                    {
                        System.out.print("X^" + p.index);
                    }
                    else if(p.index == 1.0)//如果指數(shù)等于1,就不輸出指數(shù)
                    {
                        System.out.print("X");
                    }
                }
                p=p.next;//繼續(xù)下一個結(jié)點
            }
            System.out.print(")");
        }
    }

    public boolean isEmpty()//判空
    {
        return length==0;
    }

    public int getLength() //求鏈表長度
    {
        return length;
    }

    public void setLength(int length)//改變鏈表長度
    {
        this.length = length;
    }
}

Polynomial類

package usps;

import java.util.Scanner;

public class Polynomial
{
    public Polynomial(){}

    public  LinkList inPoly()//建立一個多項式
    {
        Scanner scn=new Scanner(System.in);

        int length;//結(jié)點個數(shù)

        LinkList list=new LinkList();

        length = scn.nextInt();//輸入多項式的各項參數(shù)

        //排序
        for(int i = 1;i <=length;i++)//將參數(shù)放進鏈表
        {
            list.add(scn.nextDouble(),scn.nextDouble(),i);
        }
        return sort(notTwo(list));
    }

    public LinkList addPoly(LinkList a,LinkList b)//多項式相加
    {
        LinkList list = new LinkList();//存儲結(jié)果的鏈表

        Node a1=a.head.next;//a的頭結(jié)點
        Node b1=b.head.next;//b的頭結(jié)點

        int length = 1;//結(jié)果鏈表的長度。

        while(a1!=null && b1!=null)
        {
                if(a1.index==b1.index)//如果指數(shù)相同,則系數(shù)相加
                {
                    list.add(a1.coefficient+b1.coefficient,a1.index,length++);
                    a1 = a1.next;
                    b1 = b1.next;//指針后移,排除已經(jīng)賦值給結(jié)果鏈表的結(jié)點
                }
                else
                {
                    if (a1.index > b1.index)
                        //如果a1結(jié)點系數(shù)大于b1結(jié)點系數(shù),就提出a1的值,因為b中都是比a1指數(shù)小的,不需要再比
                    {
                        list.add(a1.coefficient, a1.index, length++);
                        a1 = a1.next;
                        //b1沒有入鏈表,下一次還要比較一遍
                    }
                    else//如果a1結(jié)點系數(shù)小于b1結(jié)點系數(shù),就提出b1的值,因為a中都是比b1指數(shù)小的,不需要再比
                    {
                        list.add(b1.coefficient, b1.index, length++);
                        b1 = b1.next;
                        //a1沒有入鏈表,下一次還要再比一遍
                    }
                }
        }
        //將沒有比完的結(jié)點都賦值給結(jié)果鏈表,此時另外一個鏈表是空的
        while(a1!=null)
        {
            list.add(a1.coefficient,a1.index,length++);
            a1 = a1.next;//下一個結(jié)點
        }
        while(b1!=null)
        {
            list.add(b1.coefficient,b1.index,length++);
            b1 = b1.next;
        }
        return sort(list);
    }

    public LinkList minusPoly(LinkList a,LinkList b)//多項式相減
    {
        LinkList list = new LinkList();//存儲結(jié)果的鏈表

        Node a1 = a.head.next;
        Node b1 = b.head.next;

        int length = 1;
        while (a1 != null && b1 != null)
        {
            if (a1.index == b1.index)
            {
                list.add(a1.coefficient - b1.coefficient,a1.index,length++);
                a1 = a1.next;
                b1 = b1.next;
            }
            else
            {
                if (a1.index > b1.index)
                {
                    list.add(a1.coefficient, a1.index, length++);
                    a1 = a1.next;
                    //b1沒有入鏈表,下一次還要比較一遍
                }
                else
                {
                    list.add(-b1.coefficient, b1.index, length++);
                    b1 = b1.next;
                }
            }
        }
        while(a1!=null)
        {
            list.add(a1.coefficient,a1.index,length++);
            a1 = a1.next;//下一個結(jié)點
        }
        while(b1!=null)
        {
            list.add(-b1.coefficient,b1.index,length++);
            b1 = b1.next;
        }
        return sort(list);
    }

    public LinkList mulPoly(LinkList a,LinkList b)//多項式相乘
    {
        LinkList list = new LinkList();//存儲結(jié)果的鏈表

        Node a1=a.head.next;//a的頭結(jié)點

        int length = 0;//結(jié)果鏈表的長度。

        while(a1!=null )//將a的每個項乘b的每個項
        {
            Node b1=b.head.next;//b的頭結(jié)點
            //每個輪回讓b1回到第一個結(jié)點
            while(b1!=null)
            {
                list.add(a1.coefficient*b1.coefficient, a1.index+b1.index, ++length);
                list.length=length;
                b1=b1.next;
            }
            a1 = a1.next;
        }
        return sort(notTwo(list));
    }

    public double getXValue(LinkList a,double x)//多項式在x處的值
    {
        double num=0;
        Node p = a.head.next;
        while(p!=null)
        {
            num+=p.coefficient*(Math.pow(x,p.index));
            p = p.next;
        }
        return num;
    }

    public LinkList derfPoly(LinkList a)//求導函數(shù)
    {
        Node p = a.head.next;
        while(p!=null)
        {
            p.setCoefficient(p.coefficient*p.index);
            p.setIndex(p.index-1);
            p = p.next;
        }
        return a;
    }

    public LinkList sort(LinkList list)//排序
    {
        if(list.isEmpty())
        {
            System.out.println("null");
        }
        else
        {
            Node p = list.head.next;

            if (list.isEmpty())
            {
                System.out.println("null");
            }
            else
            {
                while (p != null)
                {
                    Node q = p.next;
                    Node r = new Node();//中轉(zhuǎn)
                    while (q != null)
                    {
                        if (p.index < q.index)
                        {
                            r.setCoefficient(q.coefficient);
                            r.setIndex(q.index);
                            q.setCoefficient(p.coefficient);
                            q.setIndex(p.index);
                            p.setCoefficient(r.coefficient);
                            p.setIndex(r.index);
                        }
                        q = q.next;
                    }
                    p = p.next;
                }
            }
        }
        return list;
    }

    public LinkList notTwo(LinkList a)//合并同類項
    {
        LinkList list = new LinkList();

        Node p=a.head.next;

        int length=0;
        while(p!=null)//每個輪回會將當前第一個結(jié)點與剩余結(jié)點比較,合并同類項
        {
            Node q=p.next;
            double coefficient=p.coefficient;
            while(q!=null)
            {
                if(p.index==q.index)//如果指數(shù)相等,則合并
                {
                    coefficient += q.coefficient;
                    q.setCoefficient(0);//刪除被合并的結(jié)點
                    q.setIndex(0);
                }
                q = q.next;//比較下一個結(jié)點
            }
            list.add(coefficient,p.index,++length);//比完一個輪回,將當前的第一個結(jié)點輸入鏈表
            p = p.next;
        }
        return list;
    }
}

關(guān)于“Java如何解實現(xiàn)一元稀疏多項式計算器”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI