Use linked lists to represent two polynomials in C++. Each polynomial will be an
ID: 3889986 • Letter: U
Question
Use linked lists to represent two polynomials in C++. Each polynomial will be an object of the Polynomial class. Each node in the linked list will represent a term of the polynomial, so data members of the nodes will include coefficient and exponent.
Processing: The program should accept appropriate values (i.e., coefficient and exponent values) from the user to populate two polynomials. Simple data checking should verify that the integer exponents are non-negative and that coefficients are not zero. Polynomials may have varying numbers of terms. The terms should be stored in the linked list in decreasing order of their exponents.
The program will perform three basic operations on the polynomials: add the two original polynomials together; output the sum subtract one of the original polynomials from the other one; output the difference perform monomial multiplication on one of the original polynomials (i.e., multiply each polynomial term by a given monomial term, not necessarily a scalar); output the monomial and the result
Note: Do not change the original two polynomials during the addition, subtraction or multiplication operations. Include appropriate functions in the class (constructors, etc.) to produce an effective object-oriented program.
Input: Be sure the program is flexible enough that polynomial terms may be supplied by the user out of proper order, but still be stored in proper order.
Output: Test program should allow the user to input the two polynomials and the monomial multiplier. Then output the following:
a) each of the two original polynomials
b) the sum of the polynomials
c) the difference (first polynomial minus the second)
d) another difference (second polynomial minus the first)
e) the result for each of the two polynomials being multiplied by a (different) monomial
Make sure your program in program is well documented both internally and externally.
Explanation / Answer
#include<iostream>
using namespace std;
class Node //This node
{
public :
int coef;
int exp;
Node *next;
Node(int x, int y) //constructor to assign the values
{
coef = x;
exp = y;
next=NULL;
}
Node(Node &n) //constructor to assign objects
{
coef=n.coef;
exp=n.exp;
next=NULL;
}
Node() //default constructor
{}
};
class List{ // polynamial class
private:
Node *head; //head node for every polynamial
public:
void display(); // display function declaration
void insert(int x, int y); //insert eleemnt into the polynamial
void add(List,List); //Addition of two polynamials
void sub(List,List); //subtraction of two polynamials
void monomialMul(List l,int mn); //monomial multiplication
List() //constructor to assign default value
{
head = NULL; // head assign null value
}
}; // end of the class
//insert nodes into the polynamial
void List :: insert(int x, int y)
{
//inserting node
Node *newNode = new Node(x,y);
if(head==NULL)
{
head = newNode;
}
else{
Node *currNode = head;
while(currNode->next!=0){
currNode = currNode->next;
}
currNode->next = newNode;
}
}
void List :: display(){
//display the polynamial
Node *currNode = head;
if(currNode!=NULL) //check polynamial have the elements are not
{
cout<<" ";
while(currNode->next!=0){
cout<<currNode->coef<<"X^"<<currNode->exp<< "+";
currNode = currNode->next;
}
cout<<currNode->coef<<"X^"<<currNode->exp<<endl;
}
else
cout<<"empty polynamial ";
}
void List :: sub(List l1,List l2) //subtraction of two polynamials
{
Node *temp1=new Node; // create nodes for traversing the two polynamials
Node *temp2=new Node;
int ex,co;
temp1=l1.head;
temp2=l2.head;
while((temp1 !=NULL)&&(temp2 !=NULL)) // repeat until either end of the first list or second list
{
if(temp1->exp==temp2->exp) // both expo are equal
{
co=temp1->coef-temp2->coef;
ex=temp1->exp;
temp1=temp1->next;
temp2=temp2->next;
}
else
if(temp1->exp>temp2->exp) //first poly exp is grater than second poly exp
{
co=temp1->coef;
ex=temp1->exp;
temp1=temp1->next;
}
else //second poly exp is grater than first poly exp
{
co=temp2->coef;
ex=temp2->exp;
temp2=temp2->next;
}
insert(co,ex);
}
while(temp1!=NULL) // add remaing elements from first list to results
{
insert(temp1->coef,temp1->exp);
temp1=temp1->next;
}
while(temp2!=NULL) // add remaing elements from second list to results
{
insert(temp2->coef,temp2->exp);
temp2=temp2->next;
}
}
void List :: monomialMul(List l1,int mn) // monomial multiplication
{
Node *temp1=new Node;
temp1=l1.head;
int co,ex;
while(temp1!=NULL) //repeat loop for all the terms
{
co=mn*temp1->coef;
ex=temp1->exp;
insert(co,ex);
temp1=temp1->next;
}
}
void List :: add(List l1,List l2) //addition of two polynamials
{
Node *temp1=new Node; // create nodes for traversing the two polynamials
Node *temp2=new Node;
int ex,co;
temp1=l1.head;
temp2=l2.head;
while((temp1 !=NULL)&&(temp2 !=NULL)) // repeat until either end of the first list or second list
{
if(temp1->exp==temp2->exp) // both expo are equal
{
co=temp1->coef+temp2->coef;
ex=temp1->exp;
temp1=temp1->next;
temp2=temp2->next;
}
else
if(temp1->exp>temp2->exp) //first poly exp is grater than second poly exp
{
co=temp1->coef;
ex=temp1->exp;
temp1=temp1->next;
}
else //second poly exp is grater than first poly exp
{
co=temp2->coef;
ex=temp2->exp;
temp2=temp2->next;
}
insert(co,ex);
}
while(temp1!=NULL) // add remaing elements from first list to results
{
insert(temp1->coef,temp1->exp);
temp1=temp1->next;
}
while(temp2!=NULL) // add remaing elements from second list to results
{
insert(temp2->coef,temp2->exp);
temp2=temp2->next;
}
}
int main()
{
List p1,p2,p4,p3,p5,p6,p7;
int co,ex,op,mn;
do //repeat until select 5
// display the menu
{
cout<<" Menu 1.create two polynamials 2.Addition 3.Subtraction 4. Monomial 5.Results 6. Exit ";
cout<<" Enter your choice ";
cin>>op;
switch(op) // select your option
{
case 1: cout<<" enter polynamial terms enter 0 0 to exit "; //create first polynamial
while(1)
{
cout<<"enter coefficent and exponent ";
cin>>co>>ex;
if(co!=0)
p1.insert(co,ex);
else
break;
}
cout<<" enter second polynamial terms enter 0 0 to exit "; //create second polynamial
while(1)
{
cout<<"enter coefficent and exponent ";
cin>>co>>ex;
if(co!=0)
p2.insert(co,ex);
else
break;
}
cout<<"given first polynamial is ";
p1.display();
cout<<"given second polynamial is ";
p2.display();
break; //call addition function
case 2: p3.add(p1,p2);
break;
case 3: p4.sub(p1,p2); //subtract p1 from p2;
p5.sub(p2,p1); //subtract p2 from p1;
break;
case 4: cout<<"enter one monomial any whole number";
cin>>mn;
p6.monomialMul(p1,mn);
p7.monomialMul(p2,mn);
break;
case 5: cout<<"resultent polynamial are ";
cout<<"given first polynamial is ";
p1.display();
cout<<"given second polynamial is ";
p2.display();
cout<<"Addition of two polynamial is ";
p3.display();
cout<<"subtraction of polynamial one from polynomial two ";
p4.display();
cout<<"subtraction of polynamial two from polynomial one ";
p5.display();
cout<<"monomial multiplicaiton of first polynamial ";
p6.display();
cout<<"monomial multiplicaiton of second polynamial ";
p7.display();
break;
}
}while(op!=6);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.