15,996,673 members
See more:
I have tried to develop a program that takes two polynomials from user and then add them also the remaining entries are written just like that I have difficulty as how to do that.
For example
if I give input
2x^3+8x^4+5x^6
and
3x^4+4x^6
then output should be
2x^3+11x^4+9x^6
but all I am receiving is error

What I have tried:

```#include<iostream>
using namespace std;
class node{
private:
int coeff;
int exp;
node *next;
public:
void set(int coeff,int exp)
{
this->coeff=coeff;
this->exp=exp;
}
node *getnext()
{
return next;
}
void setnext(node *next)
{
this->next=next;
}
int coef()
{
return coeff;
}
int e()
{
return exp;
}
};
class list{
private:
node *current;
int size;
public:
list()
{
current=NULL;
size=0;
}
void create()
{
int co,ex;
node *newnode=new node();
cout<<"Enter coefficient :";
cin>>co;
cout<<"Enter exponent :";
cin>>ex;
newnode->set(co,ex);
if(current!=NULL)
{
newnode->setnext(current->getnext());
current->setnext(newnode);
current=newnode;
}
else
{
newnode->setnext(NULL);
current=newnode;
}
size++;
}
int coef()
{
return current->coef();
}
int e()
{
return current->e();
}
node *cur()
{
return current;
}
int length()
{
return size;
}
void start()
{
}
void next()
{
current=current->getnext();
}
void display()
{
if(current!=NULL)
{
cout<<current->coef()<<"x^"<<current->e();
}
}
{
node *newnode=new node();
newnode->set(co,e);
if(current!=NULL)
{
newnode->setnext(current->getnext());
current->setnext(newnode);
current=newnode;
}
else
{
newnode->setnext(NULL);
current=newnode;
}
size++;
}
void cc(node *current)
{
this->current=current;
}
};
int main()
{
list l1,l2,l3;
int n1,n2;
cout<<"Please enter elements in ordered form "<<endl;
cout<<"Enter number of terms you want to enter in first polynomial :";
cin>>n1;
for(int i=1;i<=n1;i++)
{
l1.create();
}
cout<<"Enter number of terms you want to enter in second polynomial :";
cin>>n2;
for(int j=1;j<=n2;j++)
{
l2.create();
}
n1=l1.length();
n2=l2.length();
l1.start();
l2.start();
for(int i=1;i<=n1;i++)
{
l1.display();
if(i<n1)
{
cout<<"+";
}
l1.next();
}
cout<<endl;
for(int i=1;i<=n2;i++)
{
l2.display();
if(i<n2)
{
cout<<"+";
}
l2.next();
}
int n,sum;
if(n1>n2)
{
n=n1;
}
else
{
n=n2;
}
node *temp1;
node *temp2;
l1.start();
l2.start();
for(int i=1;i<=n;i++)
{
temp1=l1.cur();
temp2=l2.cur();
for(int i=1;i<=n1;i++)
{
for(int i=1;i<=n2;i++)
{
if(l1.e()==l2.e())
{
sum=l1.coef()+l2.coef();
}
l2.next();
}
l1.next();
}
l1.cc(temp1);
l2.cc(temp2);
if(l1.e()>l2.e())
{
}
else
{
}
}
cout<<endl;
int n3=l3.length();
l3.start();
for(int i=1;i<=n3;i++)
{
l3.display();
if(i<n3)
{
cout<<"+";
}
l3.next();
}
return 0;
}```
Posted
Updated 12-Oct-17 13:13pm
Richard MacCutchan 12-Oct-17 16:28pm
"but all I am receiving is error"
What error, and where does it occur?
Patrice T 12-Oct-17 18:27pm

## Solution 1

Quote:
but all I am receiving is error

What error message? with which input?

I see a major flow in your code:
Your code do not enforce the order of coeffs in the storage list. By enforcing the order of coeffs when you put in storage list, it will simplify later usage.
One you know the coeffs are in order, adding the 2 polynomials is not really complicated.
Take a sheet of paper and a pencil. note the coeffs as stored in lists and add the 2 lists in a third. How did you proceed ?
This is your algorithm; your code will have to do the same.