欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java一元稀疏多項(xiàng)式計(jì)算器

 更新時(shí)間:2021年12月27日 08:47:50   作者:奏律  
大家好,本篇文章主要講的是Java一元稀疏多項(xiàng)式計(jì)算器,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽

要求:

一元稀疏多項(xiàng)式計(jì)算器

【問(wèn)題描述】 設(shè)計(jì)一個(gè)一元稀疏多項(xiàng)式簡(jiǎn)單計(jì)算器。

【基本要求】一元稀疏多項(xiàng)式簡(jiǎn)單計(jì)算器的基本功能是:

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

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

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

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

【測(cè)試數(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) 互換上述測(cè)試數(shù)據(jù)中的前后兩個(gè)多項(xiàng)式

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

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

實(shí)現(xiàn):

Main類

package usps;
/*
吳樂(lè) 漢江師范學(xué)院 軟件工程2001班
數(shù)據(jù)結(jié)構(gòu)期末大作業(yè) 一元稀疏多項(xiàng)式計(jì)算器
開始  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                  一元稀疏多項(xiàng)式計(jì)算器");
            System.out.println("——————————————————————————————————————————————————————————");
            System.out.println(  "1.建立多項(xiàng)式a      2.建立多項(xiàng)式a+b       3.建立多項(xiàng)式a-b  " +
                    "\n4.建立多項(xiàng)式axb    5.多項(xiàng)式a的導(dǎo)函數(shù)      6.多項(xiàng)式在x處的值\n7.退出");
            System.out.println("——————————————————————————————————————————————————————————");
            System.out.print("命令: ");
            num=in.nextInt();
            switch (num)//命令
            {
                case 1://建立多項(xiàng)式
                {
                    System.out.println("請(qǐng)輸入多項(xiàng)式:");
                    System.out.println("項(xiàng)的個(gè)數(shù)n <系數(shù) 指數(shù)>*n");
                    LinkList list = poly.inPoly();
                    System.out.print("\n多項(xiàng)式: ");
                    list.allPut();
                }break;

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

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

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

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

                case 6://多項(xiàng)式在x處的值
                {
                    System.out.println("請(qǐng)輸入多項(xiàng)式a:");
                    System.out.println("項(xiàng)的個(gè)數(shù)n <系數(shù) 指數(shù)>*n");
                    LinkList listA = poly.inPoly();
                    System.out.println("請(qǐng)輸入 x : ");
                    double x = in.nextDouble();
                    System.out.print("\n多項(xiàng)式 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("輸入錯(cuò)誤了你個(gè)蠢蛋");
            }
        }while(num!=7);
    }
}

Node類

package usps;

public class Node
{
    Node next;//下一個(gè)結(jié)點(diǎn)
    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é)點(diǎn)
    int length;//單鏈表長(zhǎng)度

    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é)點(diǎn)不存放數(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才會(huì)輸出。
            {
                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才會(huì)輸出。
                {
                    if(p.coefficient>0)
                    {
                        System.out.print("+");//如果系數(shù)大于0,前面就帶+號(hào)
                    }

                    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ù)下一個(gè)結(jié)點(diǎn)
            }
            System.out.print(")");
        }
    }

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

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

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

Polynomial類

package usps;

import java.util.Scanner;

public class Polynomial
{
    public Polynomial(){}

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

        int length;//結(jié)點(diǎn)個(gè)數(shù)

        LinkList list=new LinkList();

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

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

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

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

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

        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é)點(diǎn)
                }
                else
                {
                    if (a1.index > b1.index)
                        //如果a1結(jié)點(diǎn)系數(shù)大于b1結(jié)點(diǎn)系數(shù),就提出a1的值,因?yàn)閎中都是比a1指數(shù)小的,不需要再比
                    {
                        list.add(a1.coefficient, a1.index, length++);
                        a1 = a1.next;
                        //b1沒有入鏈表,下一次還要比較一遍
                    }
                    else//如果a1結(jié)點(diǎn)系數(shù)小于b1結(jié)點(diǎn)系數(shù),就提出b1的值,因?yàn)閍中都是比b1指數(shù)小的,不需要再比
                    {
                        list.add(b1.coefficient, b1.index, length++);
                        b1 = b1.next;
                        //a1沒有入鏈表,下一次還要再比一遍
                    }
                }
        }
        //將沒有比完的結(jié)點(diǎn)都賦值給結(jié)果鏈表,此時(shí)另外一個(gè)鏈表是空的
        while(a1!=null)
        {
            list.add(a1.coefficient,a1.index,length++);
            a1 = a1.next;//下一個(gè)結(jié)點(diǎn)
        }
        while(b1!=null)
        {
            list.add(b1.coefficient,b1.index,length++);
            b1 = b1.next;
        }
        return sort(list);
    }

    public LinkList minusPoly(LinkList a,LinkList b)//多項(xiàng)式相減
    {
        LinkList list = new LinkList();//存儲(chǔ)結(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;//下一個(gè)結(jié)點(diǎn)
        }
        while(b1!=null)
        {
            list.add(-b1.coefficient,b1.index,length++);
            b1 = b1.next;
        }
        return sort(list);
    }

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

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

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

        while(a1!=null )//將a的每個(gè)項(xiàng)乘b的每個(gè)項(xiàng)
        {
            Node b1=b.head.next;//b的頭結(jié)點(diǎn)
            //每個(gè)輪回讓b1回到第一個(gè)結(jié)點(diǎn)
            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)//多項(xiàng)式在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)//求導(dǎo)函數(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)//合并同類項(xiàng)
    {
        LinkList list = new LinkList();

        Node p=a.head.next;

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

到此這篇關(guān)于Java一元稀疏多項(xiàng)式計(jì)算器的文章就介紹到這了,更多相關(guān)Java計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論