Question

Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent...

Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. • in the term 4X2, the coefficient is 4 and the exponent 2 • in -6X8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent II. The Polynomial Class Now create a class to represent a polynomial. As defined here, a polynomial is a sequence of terms. E.g. 1. 3X2 + 4X4 + X6 2. 2 + 5X2 + 6X3 + 2X7 3. 4X10 The terms of polynomial 1 are (3,2), (4,4) and (1,6). The terms of polynomial 2 are (2,0), (5,2), (6,3) and (2,7). Polynomial 3 has only one term (4,10) F To receive credit for this assignment, your class must use a generic “List of Term” to store the terms of a Polynomial object Your class will have a constructor that creates an empty list and additional methods to do each of the following: 1. insert a new term in its proper place in a polynomial (see “Additional Specifications,” below) 2. return all the terms of a polynomial as a single line string, as shown here: 3x^2 + 4x^4 + x^6 3. delete a term from a polynomial (see “Additional Specifications,” below) 4. Compute and return the derivative of all the polynomial 5. reverse the order of the terms in a polynomial (see “Additional Specifications,” below) III. The Test Class The main method of your test class will create a Polynomial object and then read and process a series of operations The operations are: 1. INSERT X Y Insert a new term with coefficient X and exponent Y into its proper place in the polynomial (insert method : adds terms to the list in descending order of power) 2. DELETE X Y Remove the term with coefficient X and exponent Y from the polynomial 3. REVERSE Reverse the order of the terms of the polynomial 4. 1st DEV to find the first derivatives of the polynomial 2nd DEV to find the second derivatives of the polynomial Each operation is to be carried out by calling a method of the Polynomial class Each operation read must be “echo printed” to the screen After each operation, print the updated polynomial by calling the toString() method For the Derivatives operation, print the string returned.

Homework Answers

Answer #1

Well commented C++ program:

#include<iostream>
using namespace std;

//Term class
template<class T>
class Term{

    private:
    int coeff;
    int exp;

    public:
    Term<T> * next;


    public:

    //constructor
    Term(T val, T pow){
        coeff = val;
        exp  = pow;
        next = NULL;
    }

    //method returns coefficlent
    T cf(){
        return coeff;
    }

    //method returns exponent
    T ex(){
        return exp;
    }

};



//Polynomial class
template<class T>
class Polynomial{

    Term<T> *head;

    public:

    //constructor
    Polynomial(){
        head = NULL;
    }


    //insert term in polynomial
    void insert(int x, int y){

        Term<T> * new_term = new Term<T>(x, y);
        
        //finding the proper place for insertion of the term

        //if the list is empty
        if(head == NULL){
            head = new_term;
        }
        else{
            Term<T> *temp = head, *prev = NULL;
            while(temp && temp->ex() < y){
                prev = temp;
                temp = temp->next;
            }

            //if the new_term has the lowest exponent
            if(temp == head){

                //assign head to new_term;
                new_term->next = head;
                head = new_term;
            }
            else{
                prev->next = new_term;
                new_term->next = temp;
            }
        }
    }



    //return polynomial as string
    string toString(){
        
        //if the list is empty
        if(head == NULL){
            return "List is empty!";
        }
        string pol = "";
        
        Term<T> *temp = head;

        //traverse the list
        while(temp->next){
            pol += to_string(temp->cf()) + "x^" + to_string(temp->ex()) + " + ";
            temp = temp->next;
        }

        pol += to_string(temp->cf()) + "x^" + to_string(temp->ex());
        return pol;
    }


    //method to delete a term from polynomial
    void Delete(int x, int y){

        Term<T> *temp =  head, *prev = NULL;

        //find the term to delete
        while(temp){
            
            if(temp->cf() == x && temp->ex() == y)
                break;
            prev = temp;
            temp = temp->next;
        }

        //if term is not found
        if(temp == NULL){
            cout<<"Not found the term to be deleted!"<<endl;
        }
        //if head is the term to be deleted
        if(temp == head){
            head = head->next;
            delete(temp);
        }
        else{
            prev->next = temp->next;
            delete(temp);
        }
    }


    //method to reverse the order of the terms in polynomial
    void reverse(){
        Term<T> *curr = head, *prev = NULL, *next = NULL; 

        //reverse the linked list while traversing
        while (curr != NULL) { 
            next = curr->next; 
            curr->next = prev; 
            prev = curr; 
            curr = next; 
        } 

        //reassigning the head
        head = prev;  
    }


    //method to find first derivative of polynomial
    string firstDev(){
        string first_dev = " ";

        //if the list is empty
        if(head == NULL){
            return "List is empty!";
        }


        Term<T> *temp = head;

        //traverse the list
        while(temp->next){

            if(temp->ex() != 0){
                first_dev += to_string((temp->ex() * temp->cf())) + "x^" + to_string((temp->ex() - 1)) + " + ";
            }
            else{
                first_dev += "0 +";
            }
            temp = temp->next;
        }

        if(temp->ex() != 0){
            first_dev += to_string((temp->ex() * temp->cf())) + "x^" + to_string((temp->ex() - 1));
        }
        else{
            first_dev += "0";
        }

        return first_dev;

    }


    //method to find second derivative of polynomial
    string secondDev(){
        string sec_dev = " ";

        //if the list is empty
        if(head == NULL){
            return "List is empty!";
        }


        Term<T> *temp = head;

        //traverse the list
        while(temp->next){

            if(temp->ex() != 0 && temp->ex() != 1){
                sec_dev += to_string((temp->ex()*(temp->ex() - 1) * temp->cf())) + "x^" + to_string((temp->ex() - 2)) + " + ";
            }
            else{
                sec_dev += "0 +";
            }
            temp = temp->next;
        }

        if(temp->ex() != 0 && temp->ex() != 1){
            sec_dev += to_string((temp->ex() *(temp->ex() - 1)* temp->cf())) + "x^" + to_string((temp->ex() - 2));
        }
        else{
            sec_dev += "0";
        }

        return sec_dev;

    }

};


int main(){

    Polynomial<int> *p = new Polynomial<int>();
    p->insert(10, 2);
    p->insert(-4, 1);
    p->insert(1, -2);
    p->insert(100, 5);
    
    cout<<p->toString()<<endl;

    p->Delete(10, 2);
    cout<<p->toString()<<endl;

    cout<<p->firstDev()<<endl;
    
    cout<<p->secondDev()<<endl;
    
    return 0;
}

Operation performed:

insert(10, 2);

insert(-4, 1);

insert(1, -2);

insert(100, 5);

toString();

Delete(10, 2);

toString();

firstDev();

secondDev();

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
1. Create a linked list using the Node class that contains the first million prime numbers...
1. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options: 1 = Search for a Number (let the user enter a number to search for) 2 = Add a new Number (let the user enter a number and add it to the head of the list) 3 = Delete a Number (let the user enter a number and delete it if found) 4 = Exit...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions: One...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
write code using python or repl.it 1. List 4 attributes that you'd create for class "student"....
write code using python or repl.it 1. List 4 attributes that you'd create for class "student". 2. List 2 setters and 2 getters for class student including their parameters 3. Write the complete definition for class student including attributes and methods created above using the correct Python syntax. 4. Create one object of class student and print the values of all its attributes. You can either call the getter method or access the attribute directly using the dot notation.
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
C# programming Create a class called QuadraticEq that has a custom constructor that takes in a,...
C# programming Create a class called QuadraticEq that has a custom constructor that takes in a, b and c as the quadratic equation coefficients. Implement also the following: A readonly property double Discriminant that represents discriminant to the equation A method double[] GetRealRoots() that returns an array of the real roots of the equation; size can be 2, 1 or 0 Please pay attention to encapsulation concerns. Use your created class in the following way inside a Main method to...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT