SoFunction
Updated on 2025-03-08

Java One-Party Sparse Polynomial Calculator

Require:

One-part sparse polynomial calculator

[Problem Description] Design a single-part sparse polynomial simple calculator.

【Basic Requirements】The basic functions of the univariate sparse polynomial simple calculator are:

(1) Enter and create a polynomial;

(2) Output polynomial, the output form is an integer sequence: n, c1, e1, c2, e2,…, cn, en, where n is the number of terms of the polynomial, ci and ei are the coefficients and exponents of the i-th term, respectively, and the sequence is arranged in exponential descending order;

(3) Add the polynomial a and b to create the polynomial a +b;

(4) Subtract the polynomial a and b to create the polynomial a -b.

【Test data】
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) Interchange the first and last two polynomials in the above test data

[Implementation Tips] Use a single linked list with header nodes to store polynomials.

【Selected content】
1) Calculate the value of the polynomial at x.
2) Find the derivative function of the polynomial a.
3) Multiply the polynomial a and b to establish the product polynomial ab.
4) The output form of a polynomial is a mathematical expression. For example, the output form of the polynomial -3x8+6x3-18 is -3x8+6x3-18, and the output form of x15+(-8)x7-14 is s15+(-8)x7-14. Note that the output form of non-zero term with a value of 1 is omitted, such as the output form of term 1x8 is x8, and the output form of term -1x3 is -x3.
**

accomplish:

Main class

package usps;
/*
 Wu Le Hanjiang Normal University Software Engineering Class 2001
 Data structure end-of-term jobs, one-way sparse polynomial calculator
 Start 2021.12.18 22:00
 Completed 2021.12.26 00:21
 Optimization 2021.12.26 18:00
 */
import ;

public class Main
{
    public static void main(String[] args)
    {
        Scanner in=new Scanner();
        Polynomial poly = new Polynomial();
        int num;
        do
        {//menu            ("\n One-way sparse polynomial calculator");
            ("——————————————————————————————————————————————————————————");
            (  "1. Create polynomial a 2. Create polynomial a+b 3. Create polynomial a-b " +
                    "\n4. Create a polynomial axb 5. Derivative function of polynomial a 6. Value of polynomial at x\n7. Exit");
            ("——————————————————————————————————————————————————————————");
            ("Order: ");
            num=();
            switch (num)//Order            {
                case 1://Create polynomial                {
                    ("Please enter polynomial:");
                    ("Number of terms n <coefficient index>*n");
                    LinkList list = ();
                    ("\nPolynomial: ");
                    ();
                }break;

                case 2://Polynomial a + b                {
                    ("Please enter polynomial a:");
                    ("Number of terms n <coefficient index>*n");
                    LinkList listA = ();
                    ("Please enter polynomial b:");
                    LinkList listB = ();
                    ("\nPolynomial a : ");
                    ();
                    ("\nPolynomial b : ");
                    ();
                    ("\nPolynomial a+b : ");
                    (listA,listB).allPut();
                }break;

                case 3://Polynomial a - b                {
                    ("Please enter polynomial a:");
                    ("Number of terms n <coefficient index>*n");
                    LinkList listA = ();
                    ("Please enter polynomial b:");
                    LinkList listB = ();
                    ("\nPolynomial a : ");
                    ();
                    ("\nPolynomial b : ");
                    ();
                    ("\nPolynomial a-b : ");
                    (listA,listB).allPut();
                }break;

                case 4://Create polynomial axb                {
                    ("Please enter polynomial a:");
                    ("Number of terms n <coefficient index>*n");
                    LinkList listA = ();
                    ("Please enter polynomial b:");
                    LinkList listB = ();
                    ("\nPolynomial a : ");
                    ();
                    ("\nPolynomial b : ");
                    ();
                    ("\nPolynomial axb : ");
                    (listA,listB).allPut();
                }break;

                case 5://The derivative function of polynomial a                {
                    ("Please enter polynomial a:");
                    ("Number of terms n <coefficient index>*n");
                    LinkList listA = ();
                    ("\nPolynomial a : ");
                    ();
                    ("\nDerivative function of polynomial a: ");
                    (listA).allPut();
                }break;

                case 6://The value of the polynomial at x                {
                    ("Please enter polynomial a:");
                    ("Number of terms n <coefficient index>*n");
                    LinkList listA = ();
                    ("Please enter x: ");
                    double x = ();
                    ("\nPolynomial a : ");
                    ();
                    ("\nx: "+x);
                    ("\na(x)for(Keep three decimal places): %.3f",(listA,x));
                }break;

                case 7://quit                {
                    ("goodbye");
                }break;

                default:("You idiot was entered wrong");
            }
        }while(num!=7);
    }
}

Node class

package usps;

public class Node
{
    Node next;//Next node    double coefficient;//coefficient    double index;//index
    public Node(){
        super();
    }
    public Node(Node next)
    {
        =next;
    }
    public Node(Node next,double coefficient, double index)
    {
        =next;
        =coefficient;//coefficient        =index;//index    }

    public double getCoefficient() {
        return coefficient;
    }

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

    public double getIndex() {
        return index;
    }

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

    public Node getNext() {
        return next;
    }

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

LinkLsit class

package usps;

public class LinkList {
    Node head;//Head node    int length;//Single-linked list length
    public LinkList()//Construct an empty link table    {
        length = 0;
        head = new Node(null);
    }

    public void add(double s1,double s2, int pos)//Add data in the linked list    {
        int num=1;
        Node p=head;
        Node q=;
        while(num&lt;pos)
        {
            p=q;
            q=;
            num++;
        }
        =new Node(q,s1,s2);//The header node does not store data        length++;
    }

    public void allPut()//All outputs of linked list    {
        if(isEmpty())
        {
            ("null");
        }
        else
        {
            Node p=;

            ("(");
            if(!=0)//The coefficient is not equal to 0 before it will be output.            {
                if(!=1.0) //If the coefficient is equal to 1, no output is required                {
                    if( == -1 &amp;&amp;  != 0)
                        ("-");
                    else
                        ();//Output coefficient                }
                if( == 1.0 &amp;&amp;  ==0)
                {
                    (1);
                }

                if( != 0 &amp;&amp;  != 1.0)
                {
                    ("X^" + );
                }
                else if( == 1.0)//If the index is equal to 1, the index will not be output                {
                    ("X");
                }
            }

            p=;

            while(p!=null)
            {
                if(!=0)//The coefficient is not equal to 0 before it will be output.                {
                    if(&gt;0)
                    {
                        ("+");//If the coefficient is greater than 0, the + sign will be included in the front                    }

                    if(!=1) //If the coefficient is equal to 1, no output is required                    {
                        if( == -1 &amp;&amp;  != 0)
                            ("-");
                        else
                            ();//Output coefficient                    }

                    if( == 1 &amp;&amp;  == 0)
                    {
                        (1);
                    }

                    if( != 0 &amp;&amp;  != 1.0)
                    {
                        ("X^" + );
                    }
                    else if( == 1.0)//If the index is equal to 1, the index will not be output                    {
                        ("X");
                    }
                }
                p=;//Continue to the next node            }
            (")");
        }
    }

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

    public int getLength() // Find the length of the linked list    {
        return length;
    }

    public void setLength(int length)//Change the length of the linked list    {
         = length;
    }
}

Polynomial class

package usps;

import ;

public class Polynomial
{
    public Polynomial(){}

    public  LinkList inPoly()//Create a polynomial    {
        Scanner scn=new Scanner();

        int length;//Number of nodes
        LinkList list=new LinkList();

        length = ();//Enter the various parameters of the polynomial
        //Sort        for(int i = 1;i &lt;=length;i++)//Put the parameters into the link table        {
            ((),(),i);
        }
        return sort(notTwo(list));
    }

    public LinkList addPoly(LinkList a,LinkList b)//Add polynomials    {
        LinkList list = new LinkList();//The linked list of results stored
        Node a1=;//a's head node        Node b1=;//b's head node
        int length = 1;//The length of the result list.
        while(a1!=null &amp;&amp; b1!=null)
        {
                if(==)//If the exponents are the same, then the coefficients are added                {
                    (+,,length++);
                    a1 = ;
                    b1 = ;// Move the pointer backwards to exclude nodes that have been assigned to the result list                }
                else
                {
                    if ( &gt; )
                        //If the node coefficient a1 is greater than the node coefficient b1, the value of a1 is proposed, because b is smaller than the exponent of a1 and there is no need to compare it again                    {
                        (, , length++);
                        a1 = ;
                        //b1 has not been entered into the linked list, and you will have to compare it again next time                    }
                    else//If the node coefficient a1 is smaller than the node coefficient b1, the value of b1 is proposed, because all the node coefficient in a are smaller than the index of b1 and there is no need to compare                    {
                        (, , length++);
                        b1 = ;
                        //A1 has not been entered into the linked list, and I will compare it again next time                    }
                }
        }
        // Assign all nodes that have not been compared to the result list. At this time, the other linked list is empty        while(a1!=null)
        {
            (,,length++);
            a1 = ;//Next node        }
        while(b1!=null)
        {
            (,,length++);
            b1 = ;
        }
        return sort(list);
    }

    public LinkList minusPoly(LinkList a,LinkList b)//Polynomial subtraction    {
        LinkList list = new LinkList();//The linked list of results stored
        Node a1 = ;
        Node b1 = ;

        int length = 1;
        while (a1 != null &amp;&amp; b1 != null)
        {
            if ( == )
            {
                ( - ,,length++);
                a1 = ;
                b1 = ;
            }
            else
            {
                if ( &gt; )
                {
                    (, , length++);
                    a1 = ;
                    //b1 has not been entered into the linked list, and you will have to compare it again next time                }
                else
                {
                    (-, , length++);
                    b1 = ;
                }
            }
        }
        while(a1!=null)
        {
            (,,length++);
            a1 = ;//Next node        }
        while(b1!=null)
        {
            (-,,length++);
            b1 = ;
        }
        return sort(list);
    }

    public LinkList mulPoly(LinkList a,LinkList b)//Multiple of polynomials    {
        LinkList list = new LinkList();//The linked list of results stored
        Node a1=;//a's head node
        int length = 0;//The length of the result list.
        while(a1!=null )//Multiply each term of ab for each term of b        {
            Node b1=;//b's head node            //Each reincarnation lets b1 return to the first node            while(b1!=null)
            {
                (*, +, ++length);
                =length;
                b1=;
            }
            a1 = ;
        }
        return sort(notTwo(list));
    }

    public double getXValue(LinkList a,double x)//The value of the polynomial at x    {
        double num=0;
        Node p = ;
        while(p!=null)
        {
            num+=*((x,));
            p = ;
        }
        return num;
    }

    public LinkList derfPoly(LinkList a)// Find the derivative function    {
        Node p = ;
        while(p!=null)
        {
            (*);
            (-1);
            p = ;
        }
        return a;
    }

    public LinkList sort(LinkList list)//Sort    {
        if(())
        {
            ("null");
        }
        else
        {
            Node p = ;

            if (())
            {
                ("null");
            }
            else
            {
                while (p != null)
                {
                    Node q = ;
                    Node r = new Node();//Transfer                    while (q != null)
                    {
                        if ( &lt; )
                        {
                            ();
                            ();
                            ();
                            ();
                            ();
                            ();
                        }
                        q = ;
                    }
                    p = ;
                }
            }
        }
        return list;
    }

    public LinkList notTwo(LinkList a)//Merge similar items    {
        LinkList list = new LinkList();

        Node p=;

        int length=0;
        while(p!=null)//Each cycle will compare the current first node with the remaining node and merge the same items        {
            Node q=;
            double coefficient=;
            while(q!=null)
            {
                if(==)//If the index is equal, merge                {
                    coefficient += ;
                    (0);//Delete the merged node                    (0);
                }
                q = ;//Compare the next node            }
            (coefficient,,++length);//After comparing a cycle, enter the current first node into the link table            p = ;
        }
        return list;
    }
}

This is all about this article about Java 1000 sparse polynomial calculator. For more related Java calculator content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!