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
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....
#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...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private...
C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private data fields to store the account holder's name and the account balance A constructor with 0 arguments A constructor with 1 argument (account holder's name) A constructor with 2 arguments(account holder's name and account balance) Public properties for the account holder's name and the account balance. Do not use auto-implemented properties. A method to increase the balance (deposit) A method to decrease the balance...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
Create a State machine diagram, class diagram and a sequence diagram A description of the System...
Create a State machine diagram, class diagram and a sequence diagram A description of the System Hjolaleidir.is - Better Biking routes The group “Better biking routes” has got the idea of making the website: hjolaleidir.is for cyclists in the greater Reykjavik area. The group “Better biking routes” want to make the website accessible for a broad spectrum of users. They are catering to users who want information about good routes from a place to a destination and users who wish...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT