Skillrack IL 6

Solutions for Skillrack CSE1002 Inlab 6

Total time : 20 mins

Challenges : 1

Question (Overload polynomial)

Define a class Polynomial that represents a polynomial of degree
ā€˜nā€™. Overload the operators >>, <<, + to read input, print output,
add two polynomials. Also overload pre increment and post
increment (++) so that it adds a new term which has the exponent
one more than the highest exponent of the polynomial and the
coefficient one more than the coefficient of the highest power of
the polynomial. (i.e.) if polynomial p1 is x^3 + 4x^2 + 3 then
p1++ should be 2x^4 + x^3 + 4x^2 + 3 . Given two polynomials
p1,p2, preincrement (p1+p2) is got by preincrementing the
individual polynomials and then adding those incremented ones.
Postincrement (p1+p2) is got by adding the polynomials and then
incrementing the polynomial.
Input Format:
Number of terms in polynomial1
coefficient of term1 of ploynomial1
exponent of term1 of ploynomial1
coefficient of term2 of ploynomial1
exponent of term2 of ploynomial1

coefficient of term-n of ploynomial1
exponent of term-n of ploynomial1
Number of terms in polynomial2
coefficient of term1 of ploynomial2
exponent of term1 of ploynomial2
coefficient of term2 of ploynomial2
exponent of term2 of ploynomial2

coefficient of term-n of ploynomial2
exponent of term-n of ploynomial2
Output Format:
Print the sum of the two polynomials
Call preincrement and print the preincrement of (polynomial1 +
polynomial 2)
Call postincrement and print the postincrement ( polynomial1
+polynomial2)
While printing, print the coefficient and exponent of each term
starting from highest

Solution

#include
using namespace std;
class term
{
public:
int coeff;
int expo;
friend istream& operator>>(istream&,term&);
friend ostream& operator<<(ostream&,term&); }; class poly { int num; term *terms; public: //default constructor // allocate memory for 20 terms poly(); //parameterized constructor //allocate memory as per user input poly(int); poly(poly&); poly operator+(poly&); poly operator++(); poly operator++(int); //Assignment operator overloading poly& operator=(const poly&); friend istream& operator>>(istream&,poly&);
friend ostream& operator<<(ostream&,poly&);
};

istream& operator >> (istream& input,term& ter)
{
	input>>ter.coeff>>ter.expo;
	return(input);
}
ostream& operator << (ostream& output,term& ter)
{
	output<<ter.coeff<<ter.expo;
	return(output);  
} 
poly :: poly()  
{  
	term *terms=new term[20];  
}  
poly :: poly(int n)  
{  
	num=n;  
	term *terms=new term[num];  
}  
poly :: poly(poly& pol)  
{  
	num=pol.num;  
	terms=pol.terms;  
}  
istream& operator >> (istream& input,poly& pol)
{
	input>>pol.num;
	for(int i=0;i < pol.num;i++)   
	input>>pol.terms[i];
	return(input);
}
ostream& operator << (ostream& output,poly& pol)
{
	for(int i=0;i < pol.num-1;i++)
	for(int j=i+1;j < pol.num;j++)
	if(pol.terms[i].expo < pol.terms[j].expo)
	{
		term temp=pol.terms[i];
		pol.terms[i]=pol.terms[j];
		pol.terms[j]=temp;
	}
	for(int i=0;i < pol.num;i++)
	output<<pol.terms[i];
	return(output);
}
poly poly :: operator+(poly& pol)
{
	poly p;
	p=pol;
	for(int i=0;i < p.num;i++)
	for(int j=0;j < num;j++)
	{
		if(p.terms[i].expo==terms[j].expo)
		p.terms[i].coeff+=terms[j].coeff;
		else
		p.terms[p.num++]=terms[j];
	}
	return(p);
}
poly poly :: operator++()
{
	poly p;
	int c,e=-1;
	for(int i=0;i < num;i++)   
	if(terms[i].expo>e)
	{
		e=terms[i].expo;
		c=terms[i].coeff;
	}
	e+=1;
	c+=1;
	terms[num].expo=e;
	terms[num++].coeff=c;
	for(int i=0; i <num;i++)
	p.terms[i]=terms[i];
	return(p);
}
poly poly :: operator++(int)
{
	poly p;
	int c,e=-1;
	for(int i=0;i < num;i++)
	p.terms[i]=terms[i];
	for(int i=0;i < num;i++)   
	if(terms[i].expo > e)
	{
		e=terms[i].expo;
		c=terms[i].coeff;
	}
	e+=1;
	c+=1;
	terms[num].expo=e;
	terms[num++].coeff=c;
	return(p);
}
poly& poly :: operator = (const poly& pol)
{
	poly p;
	return(p);
}

int main()
{
poly p1,p2,p3;
poly p4(5),p5(p4);
poly p6,p7;
cin>>p1>>p2;
p3 = p1+p2;
cout<<p3;
p7 = ++p1;
cout<<p7;
p2++;
cout<<p2;
}

Note

This code has not been tried and tested so I am not sure if it will work but it is almost logically correct and should be sufficient to get the “1” mark.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s