Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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 *head;
		node *current;
		int size;
		public:
		list()
		{
			head=new node();
			head->setnext(NULL);
			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);
				head->setnext(newnode);
				current=newnode;
			}
			size++;
		}
		int coef()
		{
			return current->coef();
		}
		int e()
		{
			return current->e();
		}
		node *cur()
		{
			return current;
		}
		int length()
		{
			return size;
		}
		void start()
		{
			current=head->getnext();
		}
		void next()
		{
			current=current->getnext();
		}
		void display()
		{
			if(current!=NULL)
			{
			cout<<current->coef()<<"x^"<<current->e();
		    }
		}
		void add(int co,int 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);
				head->setnext(newnode);
				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();
    				l3.add(sum,l1.e());
    			}
    			l2.next();
    		}
    		l1.next();
    	}
    	l1.cc(temp1);
    	l2.cc(temp2);
    	if(l1.e()>l2.e())
    	{
    		l3.add(l1.coef(),l1.e());
    	}
    	else
    	{
    		l3.add(l2.coef(),l2.e());
    	}
    }
    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
Comments
Richard MacCutchan 12-Oct-17 16:28pm    
"but all I am receiving is error"
What error, and where does it occur?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900