SlideShare a Scribd company logo
1 of 11
Download to read offline
I have to write a polynomial class linked list program and i do not know what is wrong with my
code. Can someone please help. It is about adding, multiplying and subtraction polynomials.
c++. please change it as needed
#ifndef POLY_H
#define POLY_H
#include
#include
#include
using namespace std;
struct TermNode
{
int exp; //exponent
double coef; //coefficient
TermNode * next;
};
class PolyType
{
public:
PolyType();
PolyType(int r, double c);
PolyType(const PolyType &rhs);
~PolyType();
PolyType operator=(const PolyType &); //assignment
PolyType operator+ (const PolyType &rhs) const; //returns sum of parameter + self
PolyType operator* (const PolyType &rhs) const;
PolyType differ();
PolyType integr();
friend ostream & operator<< (ostream & out, const PolyType & rhs); //coefficients printed to a 2
decimal places
private:
TermNode *polyPtr;
};
PolyType::PolyType() {
polyPtr = nullptr;
}
PolyType::~PolyType() {
delete polyPtr;
polyPtr = nullptr;
}
PolyType::PolyType(int r, double c) {
polyPtr = new TermNode();
polyPtr->exp = c;
polyPtr->coef = r;
}
PolyType::PolyType(const PolyType & rhs) {
polyPtr = rhs.polyPtr;
}
PolyType PolyType::operator+(const PolyType & rhs) const {
PolyType temp;
if (this->polyPtr->exp >= rhs.polyPtr->exp) {
temp.polyPtr = this->polyPtr;
temp.polyPtr->next = rhs.polyPtr;
}
else {
temp.polyPtr = rhs.polyPtr;
temp.polyPtr->next = this->polyPtr;
}
return temp;
}
PolyType PolyType::operator=(const PolyType &rhs)
{
delete polyPtr;
TermNode * orig = rhs.polyPtr;
TermNode * newP = nullptr;
polyPtr = nullptr;
while (orig != nullptr)
{
if (newP == nullptr)
{
polyPtr = new TermNode;
polyPtr->exp = orig->exp;
polyPtr->coef = orig->coef;
polyPtr->next = nullptr;
newP = polyPtr;
}
else
{
newP->next = new TermNode;
newP = newP->next;
newP->exp = orig->exp;
newP->coef = orig->coef;
newP->next = nullptr;
}
orig = orig->next;
}
return *this;
}
PolyType PolyType::operator*(const PolyType &rhs) const
{
PolyType result;
TermNode * ptr = polyPtr;
while (ptr != nullptr)
{
result.polyPtr->exp = ptr->exp * rhs.polyPtr->exp;
result.polyPtr->coef = ptr->coef * rhs.polyPtr->coef;
ptr = ptr->next;
}
return result;
}
ostream & operator<<(ostream & out, const PolyType & rhs) {
TermNode *currentPtr = rhs.polyPtr;
out << currentPtr->coef << "^" << currentPtr->exp;
currentPtr = currentPtr->next;
while (currentPtr != nullptr) {
out << " + " << currentPtr->coef << "^" << currentPtr->exp;
currentPtr = currentPtr->next;
}
return out;
}
PolyType PolyType::integr() {
PolyType temp;
TermNode *currentPtr = this->polyPtr;
TermNode *tempPtr = temp.polyPtr;
while (currentPtr != nullptr) {
tempPtr = new TermNode();
tempPtr->coef = currentPtr->coef / (currentPtr->exp + 1);
tempPtr->exp = currentPtr->exp + 1;
currentPtr = currentPtr->next;
tempPtr = tempPtr->next;
}
return temp;
}
PolyType PolyType::differ() {
PolyType temp;
TermNode *currentPtr = this->polyPtr;
TermNode *tempPtr = temp.polyPtr;
while (currentPtr != nullptr) {
tempPtr = new TermNode();
tempPtr->coef = currentPtr->coef * currentPtr->exp;
tempPtr->exp = currentPtr->exp - 1;
currentPtr = currentPtr->next;
tempPtr = tempPtr->next;
}
return temp;
}
#endif
int main() {
PolyType poly1; //makes a null polynomial
PolyType poly2(2, 3); // makes the polynomial 2.00x^3
PolyType poly3(3, 4); // makes the polynomial 3.00x^4
poly1 = poly2 + poly3; // makes poly1 = 3.00x^4 + 2.00x^3
cout << poly1 << endl; // prints out 3.0x^4 + 2.00x^3
poly3 = poly2 * poly1; // sets poly3 to 6.0x^7+4.00x^6
system("pause");
return 0;
}
Solution
I have written it for addition and substraction. Hope you'll get an idea:
#include
#include
#include
// Creating a NODE Structure
struct node
{
int coe,exp; // data
struct node *next; // link to next node and previous node
};
// Creating a class Polynomial
class polynomial
{
struct node *start,*ptrn,*ptrp;
public:
void get_poly(); // to get a polynomial
void show(); // show
void add(polynomial p1,polynomial p2); // Add two polynomials
void subtract(polynomial p1,polynomial p2); //Subtract 2 polynomials
};
void polynomial::get_poly() // Get Polynomial
{
char c='y';
ptrn=ptrp=start=NULL;
while(c=='y' || c=='Y')
{
ptrn=new node;
ptrp->next=ptrn;
if(start==NULL)
start=ptrn;
ptrp=ptrn;
cout<<"
Enter the coefficient: ";
cin>>ptrn->coe;
cout<<"Enter the exponent: ";
cin>>ptrn->exp;
ptrn->next=NULL;
cout<<"Enter y to add more nodes: ";
cin>>c;
}
return;
}
void polynomial::show() // Show Polynomial
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
cout<coe<<"X^"<exp<<" + ";
ptr=ptr->next;
}
cout<<" ";
}
void polynomial::add(polynomial p1,polynomial p2) // Add Polynomials
{
struct node *p1ptr,*p2ptr;
int coe,exp;
ptrn=ptrp=start=NULL;
p1ptr=p1.start;
p2ptr=p2.start;
while(p1ptr!=NULL && p2ptr!=NULL)
{
if(p1ptr->exp==p2ptr->exp) // If coefficients are equal
{
coe=p1ptr->coe+p2ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->next;
p2ptr=p2ptr->next;
}
else if(p1ptr->exp>p2ptr->exp)
{
coe=p1ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->next;
}
else if(p1ptr->expexp)
{
coe=p2ptr->coe;
exp=p2ptr->exp;
p2ptr=p2ptr->next;
}
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
} // End of While
if(p1ptr==NULL)
{
while(p2ptr!=NULL)
{
coe=p2ptr->coe;
exp=p2ptr->exp;
p2ptr=p2ptr->next;
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
}
}
else if(p2ptr==NULL)
{
while(p1ptr!=NULL)
{
coe=p1ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->next;
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
}
}
} // End of addition
// Subtract two polynomials
void polynomial::subtract(polynomial p1,polynomial p2) // Subtract
{
struct node *p1ptr,*p2ptr;
int coe,exp;
ptrn=ptrp=start=NULL;
p1ptr=p1.start;
p2ptr=p2.start;
while(p1ptr!=NULL && p2ptr!=NULL)
{
if(p1ptr->exp==p2ptr->exp) // If coefficients are equal
{
coe=p1ptr->coe-p2ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->next;
p2ptr=p2ptr->next;
}
else if(p1ptr->exp>p2ptr->exp)
{
coe=p1ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->next;
}
else if(p1ptr->expexp)
{
coe=0-p2ptr->coe;
exp=p2ptr->exp;
p2ptr=p2ptr->next;
}
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
} // End of While
if(p1ptr==NULL)
{
while(p2ptr!=NULL)
{
coe=0-p2ptr->coe;
exp=p2ptr->exp;
p2ptr=p2ptr->next;
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
}
}
else if(p2ptr==NULL)
{
while(p1ptr!=NULL)
{
coe=p1ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->next;
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
}
}
} // End of subtraction
int main()
{
clrscr();
polynomial p1,p2,sum,diff;
cout<<"First Polynomial.
";
p1.get_poly();
cout<<"
Second polynomial.
";
p2.get_poly();
clrscr();
cout<<"
The First polynomial is: ";
p1.show();
cout<<"
The second polynomial is: ";
p2.show();
cout<<"
The sum of two polynomials is: ";
sum.add(p1,p2);
sum.show();
cout<<"
The difference of two polynomials is: ";
diff.subtract(p1,p2);
diff.show();
getch();
return 0;
}
Kindly provide feedback.

More Related Content

Similar to I have to write a polynomial class linked list program and i do not .pdf

Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfFraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfravikapoorindia
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdfChange the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdffatoryoutlets
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3ecomputernotes
 
A polynomial may be represented as a linked list where each node cont.pdf
A polynomial may be represented as a linked list where each node cont.pdfA polynomial may be represented as a linked list where each node cont.pdf
A polynomial may be represented as a linked list where each node cont.pdffashionfolionr
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdfexcellentmobilesabc
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfEdwardw5nSlaterl
 
COMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdfCOMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdffazalenterprises
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfCSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfmarketing413921
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control EtcGeshan Manandhar
 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfFashionBoutiquedelhi
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfsales98
 

Similar to I have to write a polynomial class linked list program and i do not .pdf (20)

Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfFraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdfChange the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdf
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3
 
A polynomial may be represented as a linked list where each node cont.pdf
A polynomial may be represented as a linked list where each node cont.pdfA polynomial may be represented as a linked list where each node cont.pdf
A polynomial may be represented as a linked list where each node cont.pdf
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdf
 
C programming
C programmingC programming
C programming
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
 
COMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdfCOMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdf
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfCSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
 
About Go
About GoAbout Go
About Go
 
iit c prog.ppt
iit c prog.pptiit c prog.ppt
iit c prog.ppt
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdf
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 

More from jibinsh

What part of the brain is used in sympathetic arousal (ANS)Solu.pdf
What part of the brain is used in sympathetic arousal (ANS)Solu.pdfWhat part of the brain is used in sympathetic arousal (ANS)Solu.pdf
What part of the brain is used in sympathetic arousal (ANS)Solu.pdfjibinsh
 
What type of cell-surface characteristic helps an epithelial tissue h.pdf
What type of cell-surface characteristic helps an epithelial tissue h.pdfWhat type of cell-surface characteristic helps an epithelial tissue h.pdf
What type of cell-surface characteristic helps an epithelial tissue h.pdfjibinsh
 
Which bond would be most polar whySolutionstarting with .pdf
Which bond would be most polar whySolutionstarting with .pdfWhich bond would be most polar whySolutionstarting with .pdf
Which bond would be most polar whySolutionstarting with .pdfjibinsh
 
What is NOT a requirement for evolution by natural selection to.pdf
What is NOT a requirement for evolution by natural selection to.pdfWhat is NOT a requirement for evolution by natural selection to.pdf
What is NOT a requirement for evolution by natural selection to.pdfjibinsh
 
What is James Wilsons leadership style and traits What made him dif.pdf
What is James Wilsons leadership style and traits What made him dif.pdfWhat is James Wilsons leadership style and traits What made him dif.pdf
What is James Wilsons leadership style and traits What made him dif.pdfjibinsh
 
This project calls for the modification of the DollarFormat clas.pdf
This project calls for the modification of the DollarFormat clas.pdfThis project calls for the modification of the DollarFormat clas.pdf
This project calls for the modification of the DollarFormat clas.pdfjibinsh
 
The method FRET ALEX is a more precise way to determine the number o.pdf
The method FRET ALEX is a more precise way to determine the number o.pdfThe method FRET ALEX is a more precise way to determine the number o.pdf
The method FRET ALEX is a more precise way to determine the number o.pdfjibinsh
 
The initial velocity is How do you solve the initial velocity.pdf
The initial velocity is How do you solve the initial velocity.pdfThe initial velocity is How do you solve the initial velocity.pdf
The initial velocity is How do you solve the initial velocity.pdfjibinsh
 
The following expression was simplified incorrectly. In which line er.pdf
The following expression was simplified incorrectly. In which line er.pdfThe following expression was simplified incorrectly. In which line er.pdf
The following expression was simplified incorrectly. In which line er.pdfjibinsh
 
The energy used in the CaMn cycle for the production of carbohydrate .pdf
The energy used in the CaMn cycle for the production of carbohydrate .pdfThe energy used in the CaMn cycle for the production of carbohydrate .pdf
The energy used in the CaMn cycle for the production of carbohydrate .pdfjibinsh
 
The CAN-SPAM Acta.   applies to virtually all promotional e-mails,.pdf
The CAN-SPAM Acta.   applies to virtually all promotional e-mails,.pdfThe CAN-SPAM Acta.   applies to virtually all promotional e-mails,.pdf
The CAN-SPAM Acta.   applies to virtually all promotional e-mails,.pdfjibinsh
 
SensesFor each of the special senses, understand how a stimulus is.pdf
SensesFor each of the special senses, understand how a stimulus is.pdfSensesFor each of the special senses, understand how a stimulus is.pdf
SensesFor each of the special senses, understand how a stimulus is.pdfjibinsh
 
Recent Presidential debates have brought about a series of polls.pdf
Recent Presidential debates have brought about a series of polls.pdfRecent Presidential debates have brought about a series of polls.pdf
Recent Presidential debates have brought about a series of polls.pdfjibinsh
 
Quality function deployment (QFD) is a structures approach for integ.pdf
Quality function deployment (QFD) is a structures approach for integ.pdfQuality function deployment (QFD) is a structures approach for integ.pdf
Quality function deployment (QFD) is a structures approach for integ.pdfjibinsh
 
please send edited code. I have posted this a few times with lots of.pdf
please send edited code. I have posted this a few times with lots of.pdfplease send edited code. I have posted this a few times with lots of.pdf
please send edited code. I have posted this a few times with lots of.pdfjibinsh
 
Please give me a custom example code of a complex PovRay Program tha.pdf
Please give me a custom example code of a complex PovRay Program tha.pdfPlease give me a custom example code of a complex PovRay Program tha.pdf
Please give me a custom example code of a complex PovRay Program tha.pdfjibinsh
 
Networking Question What are the key differences between forwarding.pdf
Networking Question What are the key differences between forwarding.pdfNetworking Question What are the key differences between forwarding.pdf
Networking Question What are the key differences between forwarding.pdfjibinsh
 
Most children with cystic fibrosis have frequent lung infections and.pdf
Most children with cystic fibrosis have frequent lung infections and.pdfMost children with cystic fibrosis have frequent lung infections and.pdf
Most children with cystic fibrosis have frequent lung infections and.pdfjibinsh
 
A)abductionB)deductive reasoningc) inductive reasoningd)a wild.pdf
A)abductionB)deductive reasoningc) inductive reasoningd)a wild.pdfA)abductionB)deductive reasoningc) inductive reasoningd)a wild.pdf
A)abductionB)deductive reasoningc) inductive reasoningd)a wild.pdfjibinsh
 
Lipid rafts are associated with which of the following activities re.pdf
Lipid rafts are associated with which of the following activities re.pdfLipid rafts are associated with which of the following activities re.pdf
Lipid rafts are associated with which of the following activities re.pdfjibinsh
 

More from jibinsh (20)

What part of the brain is used in sympathetic arousal (ANS)Solu.pdf
What part of the brain is used in sympathetic arousal (ANS)Solu.pdfWhat part of the brain is used in sympathetic arousal (ANS)Solu.pdf
What part of the brain is used in sympathetic arousal (ANS)Solu.pdf
 
What type of cell-surface characteristic helps an epithelial tissue h.pdf
What type of cell-surface characteristic helps an epithelial tissue h.pdfWhat type of cell-surface characteristic helps an epithelial tissue h.pdf
What type of cell-surface characteristic helps an epithelial tissue h.pdf
 
Which bond would be most polar whySolutionstarting with .pdf
Which bond would be most polar whySolutionstarting with .pdfWhich bond would be most polar whySolutionstarting with .pdf
Which bond would be most polar whySolutionstarting with .pdf
 
What is NOT a requirement for evolution by natural selection to.pdf
What is NOT a requirement for evolution by natural selection to.pdfWhat is NOT a requirement for evolution by natural selection to.pdf
What is NOT a requirement for evolution by natural selection to.pdf
 
What is James Wilsons leadership style and traits What made him dif.pdf
What is James Wilsons leadership style and traits What made him dif.pdfWhat is James Wilsons leadership style and traits What made him dif.pdf
What is James Wilsons leadership style and traits What made him dif.pdf
 
This project calls for the modification of the DollarFormat clas.pdf
This project calls for the modification of the DollarFormat clas.pdfThis project calls for the modification of the DollarFormat clas.pdf
This project calls for the modification of the DollarFormat clas.pdf
 
The method FRET ALEX is a more precise way to determine the number o.pdf
The method FRET ALEX is a more precise way to determine the number o.pdfThe method FRET ALEX is a more precise way to determine the number o.pdf
The method FRET ALEX is a more precise way to determine the number o.pdf
 
The initial velocity is How do you solve the initial velocity.pdf
The initial velocity is How do you solve the initial velocity.pdfThe initial velocity is How do you solve the initial velocity.pdf
The initial velocity is How do you solve the initial velocity.pdf
 
The following expression was simplified incorrectly. In which line er.pdf
The following expression was simplified incorrectly. In which line er.pdfThe following expression was simplified incorrectly. In which line er.pdf
The following expression was simplified incorrectly. In which line er.pdf
 
The energy used in the CaMn cycle for the production of carbohydrate .pdf
The energy used in the CaMn cycle for the production of carbohydrate .pdfThe energy used in the CaMn cycle for the production of carbohydrate .pdf
The energy used in the CaMn cycle for the production of carbohydrate .pdf
 
The CAN-SPAM Acta.   applies to virtually all promotional e-mails,.pdf
The CAN-SPAM Acta.   applies to virtually all promotional e-mails,.pdfThe CAN-SPAM Acta.   applies to virtually all promotional e-mails,.pdf
The CAN-SPAM Acta.   applies to virtually all promotional e-mails,.pdf
 
SensesFor each of the special senses, understand how a stimulus is.pdf
SensesFor each of the special senses, understand how a stimulus is.pdfSensesFor each of the special senses, understand how a stimulus is.pdf
SensesFor each of the special senses, understand how a stimulus is.pdf
 
Recent Presidential debates have brought about a series of polls.pdf
Recent Presidential debates have brought about a series of polls.pdfRecent Presidential debates have brought about a series of polls.pdf
Recent Presidential debates have brought about a series of polls.pdf
 
Quality function deployment (QFD) is a structures approach for integ.pdf
Quality function deployment (QFD) is a structures approach for integ.pdfQuality function deployment (QFD) is a structures approach for integ.pdf
Quality function deployment (QFD) is a structures approach for integ.pdf
 
please send edited code. I have posted this a few times with lots of.pdf
please send edited code. I have posted this a few times with lots of.pdfplease send edited code. I have posted this a few times with lots of.pdf
please send edited code. I have posted this a few times with lots of.pdf
 
Please give me a custom example code of a complex PovRay Program tha.pdf
Please give me a custom example code of a complex PovRay Program tha.pdfPlease give me a custom example code of a complex PovRay Program tha.pdf
Please give me a custom example code of a complex PovRay Program tha.pdf
 
Networking Question What are the key differences between forwarding.pdf
Networking Question What are the key differences between forwarding.pdfNetworking Question What are the key differences between forwarding.pdf
Networking Question What are the key differences between forwarding.pdf
 
Most children with cystic fibrosis have frequent lung infections and.pdf
Most children with cystic fibrosis have frequent lung infections and.pdfMost children with cystic fibrosis have frequent lung infections and.pdf
Most children with cystic fibrosis have frequent lung infections and.pdf
 
A)abductionB)deductive reasoningc) inductive reasoningd)a wild.pdf
A)abductionB)deductive reasoningc) inductive reasoningd)a wild.pdfA)abductionB)deductive reasoningc) inductive reasoningd)a wild.pdf
A)abductionB)deductive reasoningc) inductive reasoningd)a wild.pdf
 
Lipid rafts are associated with which of the following activities re.pdf
Lipid rafts are associated with which of the following activities re.pdfLipid rafts are associated with which of the following activities re.pdf
Lipid rafts are associated with which of the following activities re.pdf
 

Recently uploaded

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 

Recently uploaded (20)

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

I have to write a polynomial class linked list program and i do not .pdf

  • 1. I have to write a polynomial class linked list program and i do not know what is wrong with my code. Can someone please help. It is about adding, multiplying and subtraction polynomials. c++. please change it as needed #ifndef POLY_H #define POLY_H #include #include #include using namespace std; struct TermNode { int exp; //exponent double coef; //coefficient TermNode * next; }; class PolyType { public: PolyType(); PolyType(int r, double c); PolyType(const PolyType &rhs); ~PolyType(); PolyType operator=(const PolyType &); //assignment PolyType operator+ (const PolyType &rhs) const; //returns sum of parameter + self PolyType operator* (const PolyType &rhs) const; PolyType differ(); PolyType integr(); friend ostream & operator<< (ostream & out, const PolyType & rhs); //coefficients printed to a 2 decimal places private: TermNode *polyPtr; }; PolyType::PolyType() { polyPtr = nullptr; }
  • 2. PolyType::~PolyType() { delete polyPtr; polyPtr = nullptr; } PolyType::PolyType(int r, double c) { polyPtr = new TermNode(); polyPtr->exp = c; polyPtr->coef = r; } PolyType::PolyType(const PolyType & rhs) { polyPtr = rhs.polyPtr; } PolyType PolyType::operator+(const PolyType & rhs) const { PolyType temp; if (this->polyPtr->exp >= rhs.polyPtr->exp) { temp.polyPtr = this->polyPtr; temp.polyPtr->next = rhs.polyPtr; } else { temp.polyPtr = rhs.polyPtr; temp.polyPtr->next = this->polyPtr; } return temp; } PolyType PolyType::operator=(const PolyType &rhs) { delete polyPtr; TermNode * orig = rhs.polyPtr; TermNode * newP = nullptr; polyPtr = nullptr; while (orig != nullptr) { if (newP == nullptr) { polyPtr = new TermNode; polyPtr->exp = orig->exp;
  • 3. polyPtr->coef = orig->coef; polyPtr->next = nullptr; newP = polyPtr; } else { newP->next = new TermNode; newP = newP->next; newP->exp = orig->exp; newP->coef = orig->coef; newP->next = nullptr; } orig = orig->next; } return *this; } PolyType PolyType::operator*(const PolyType &rhs) const { PolyType result; TermNode * ptr = polyPtr; while (ptr != nullptr) { result.polyPtr->exp = ptr->exp * rhs.polyPtr->exp; result.polyPtr->coef = ptr->coef * rhs.polyPtr->coef; ptr = ptr->next; } return result; } ostream & operator<<(ostream & out, const PolyType & rhs) { TermNode *currentPtr = rhs.polyPtr; out << currentPtr->coef << "^" << currentPtr->exp; currentPtr = currentPtr->next; while (currentPtr != nullptr) { out << " + " << currentPtr->coef << "^" << currentPtr->exp; currentPtr = currentPtr->next; }
  • 4. return out; } PolyType PolyType::integr() { PolyType temp; TermNode *currentPtr = this->polyPtr; TermNode *tempPtr = temp.polyPtr; while (currentPtr != nullptr) { tempPtr = new TermNode(); tempPtr->coef = currentPtr->coef / (currentPtr->exp + 1); tempPtr->exp = currentPtr->exp + 1; currentPtr = currentPtr->next; tempPtr = tempPtr->next; } return temp; } PolyType PolyType::differ() { PolyType temp; TermNode *currentPtr = this->polyPtr; TermNode *tempPtr = temp.polyPtr; while (currentPtr != nullptr) { tempPtr = new TermNode(); tempPtr->coef = currentPtr->coef * currentPtr->exp; tempPtr->exp = currentPtr->exp - 1; currentPtr = currentPtr->next; tempPtr = tempPtr->next; } return temp; } #endif int main() { PolyType poly1; //makes a null polynomial PolyType poly2(2, 3); // makes the polynomial 2.00x^3 PolyType poly3(3, 4); // makes the polynomial 3.00x^4 poly1 = poly2 + poly3; // makes poly1 = 3.00x^4 + 2.00x^3 cout << poly1 << endl; // prints out 3.0x^4 + 2.00x^3 poly3 = poly2 * poly1; // sets poly3 to 6.0x^7+4.00x^6
  • 5. system("pause"); return 0; } Solution I have written it for addition and substraction. Hope you'll get an idea: #include #include #include // Creating a NODE Structure struct node { int coe,exp; // data struct node *next; // link to next node and previous node }; // Creating a class Polynomial class polynomial { struct node *start,*ptrn,*ptrp; public: void get_poly(); // to get a polynomial void show(); // show void add(polynomial p1,polynomial p2); // Add two polynomials void subtract(polynomial p1,polynomial p2); //Subtract 2 polynomials }; void polynomial::get_poly() // Get Polynomial { char c='y'; ptrn=ptrp=start=NULL; while(c=='y' || c=='Y') { ptrn=new node; ptrp->next=ptrn; if(start==NULL) start=ptrn;
  • 6. ptrp=ptrn; cout<<" Enter the coefficient: "; cin>>ptrn->coe; cout<<"Enter the exponent: "; cin>>ptrn->exp; ptrn->next=NULL; cout<<"Enter y to add more nodes: "; cin>>c; } return; } void polynomial::show() // Show Polynomial { struct node *ptr; ptr=start; while(ptr!=NULL) { cout<coe<<"X^"<exp<<" + "; ptr=ptr->next; } cout<<" "; } void polynomial::add(polynomial p1,polynomial p2) // Add Polynomials { struct node *p1ptr,*p2ptr; int coe,exp; ptrn=ptrp=start=NULL; p1ptr=p1.start; p2ptr=p2.start; while(p1ptr!=NULL && p2ptr!=NULL) { if(p1ptr->exp==p2ptr->exp) // If coefficients are equal { coe=p1ptr->coe+p2ptr->coe;
  • 7. exp=p1ptr->exp; p1ptr=p1ptr->next; p2ptr=p2ptr->next; } else if(p1ptr->exp>p2ptr->exp) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; } else if(p1ptr->expexp) { coe=p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; } ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } // End of While if(p1ptr==NULL) { while(p2ptr!=NULL) { coe=p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe;
  • 8. ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } else if(p2ptr==NULL) { while(p1ptr!=NULL) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } } // End of addition // Subtract two polynomials void polynomial::subtract(polynomial p1,polynomial p2) // Subtract { struct node *p1ptr,*p2ptr; int coe,exp; ptrn=ptrp=start=NULL; p1ptr=p1.start; p2ptr=p2.start; while(p1ptr!=NULL && p2ptr!=NULL) { if(p1ptr->exp==p2ptr->exp) // If coefficients are equal {
  • 9. coe=p1ptr->coe-p2ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; p2ptr=p2ptr->next; } else if(p1ptr->exp>p2ptr->exp) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; } else if(p1ptr->expexp) { coe=0-p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; } ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } // End of While if(p1ptr==NULL) { while(p2ptr!=NULL) { coe=0-p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; ptrn=new node; if(start==NULL) start=ptrn;
  • 11. p2.get_poly(); clrscr(); cout<<" The First polynomial is: "; p1.show(); cout<<" The second polynomial is: "; p2.show(); cout<<" The sum of two polynomials is: "; sum.add(p1,p2); sum.show(); cout<<" The difference of two polynomials is: "; diff.subtract(p1,p2); diff.show(); getch(); return 0; } Kindly provide feedback.