SlideShare a Scribd company logo
1 of 11
Download to read offline
I wrote the following: change it to having a header, main and cpp file.. THERE ARE ERRORS
PLEASE FIX.. Picture of output is given below
The internal representation of a Polynomial is an array of terms. Each term contains a coefficient
and an exponent, e.g., the term
2x4
has the coefficient 2 and the exponent 4.
Develop a complete class containing proper constructor and destructor functions as well as set,
get, and print functions. The class should also provide the following overloaded operator
capabilities:
a) Overload the addition operator (+) to add two Polynomials.
b) Overload the subtraction operator (-) to subtract two Polynomials.
c) Overload the assignment operator to assign one Polynomial to another.
d) Overload the multiplication operator (*) to multiply two Polynomials.
e) Overload the addition assignment operator (+=), subtraction assignment operator (-=), and
multiplication assignment operator (*=).
Write an application that tests all the functionality provided by class Polynomial:
• create three Polynomials
• add two Polynomials, using + and += operators
• subtract two Polynomials, using – and -= operators
• assign one Polynomial to another Polynomial
• multiply two Polynomials, using * and *= operators.
---------------------------------------------------------------------------------------------
#include
#include
using namespace std;
using std::setiosflags;
using std::resetiosflags;
class Polynomial
{
public:
Polynomial();
Polynomial operator+( const Polynomial& ) const;
Polynomial operator-( const Polynomial& ) const;
Polynomial operator*( const Polynomial& );
const Polynomial operator=( const Polynomial&);
Polynomial& operator+=( const Polynomial& );
Polynomial& operator-=( const Polynomial& );
void enterTerms( void );
void printPolynomial( void ) const;
private:
int exponents[ 100 ];
int coefficients[ 100 ];
};
Polynomial::Polynomial()
{
for ( int t = 0; t < 100; ++t ) {
coefficients[ t ] = 0;
exponents[ t ] = 0;
}
}
void Polynomial::printPolynomial( void ) const
{
int start;
bool zero = false;
if ( coefficients[ 0 ] ) { // output constants
cout << coefficients[ 0 ];
start = 1;
zero = true; // at least one term exists
}
else {
if ( coefficients[ 1 ] ) {
cout << coefficients[ 1 ] << 'x'; // constant does not exist
// so output first term
// without a sign
if ( ( exponents[ 1 ] != 0 ) && ( exponents[ 1 ] != 1 ) )
cout << '^' << exponents[ 1 ];
zero = true; // at least one term exists
}
start = 2;
}
// output remaining polynomial terms
for ( int x = start; x < 100; ++x ) {
if ( coefficients[ x ] != 0 ) {
cout << setiosflags( ios::showpos ) << coefficients[ x ]
<< resetiosflags( ios::showpos ) << 'x';
if ( ( exponents[ x ] != 0 ) && ( exponents[ x ] != 1 ) )
cout << '^' << exponents[ x ];
zero = true; // at least one term exists
}
}
if ( !zero ) // no terms exist in the polynomial
cout << '0';
cout << endl;
}
const Polynomial Polynomial::operator=( const Polynomial& r )
{
exponents[ 0 ] = r.exponents[ 0 ];
coefficients[ 0 ] = r.coefficients[ 0 ];
for ( int s = 1; ( s < 100 ); ++s ) {
if ( r.exponents[ s ] != 0 ) {
exponents[ s ] = r.exponents[ s ];
coefficients[ s ] = r.coefficients[ s ];
}
else {
if ( exponents[ s ] == 0 )
break;
exponents[ s ] = 0;
coefficients[ s ] = 0;
}
}
return *this;
}
Polynomial Polynomial::operator+( const Polynomial& r ) const
{
Polynomial temp;
bool exponentExists;
// process element with a zero exponent
temp.coefficients[ 0 ] = coefficients[ 0 ] + r.coefficients[ 0 ];
for ( int s = 1; ( s < 100 ) && ( r.exponents[ s ] != 0 ); ++s ) {
temp.coefficients[ s ] = r.coefficients[ s ];
temp.exponents[ s ] = r.exponents[ s ];
}
for ( int x = 1; x < 100; ++x ) {
exponentExists = false; // assume exponent will not be found
for ( int t = 1; ( t < 100 ) && ( !exponentExists ); ++t )
if ( exponents[ x ] == temp.exponents[ t ] ) {
temp.coefficients[ t ] += coefficients[ x ];
exponentExists = true; // exponent found
}
// exponent was not found, insert into temp
if ( !exponentExists ) {
temp.exponents[ s ] = exponents[ x ];
temp.coefficients[ s ] += coefficients[ x ];
++s;
}
}
return temp;
}
Polynomial &Polynomial::operator+=( const Polynomial &r )
{
*this = *this + r;
return *this;
}
Polynomial Polynomial::operator-( const Polynomial& r ) const
{
Polynomial temp;
bool exponentExists;
// process element with a zero exponent
temp.coefficients[ 0 ] = coefficients[ 0 ] - r.coefficients[ 0 ];
// copy left arrays into temp object s will be used to keep
// track of first open coefficient element
for ( int s = 1; ( s < 100 ) && ( exponents[ s ] != 0 ); ++s ) {
temp.coefficients[ s ] = coefficients[ s ];
temp.exponents[ s ] = exponents[ s ];
}
for ( int x = 1; x < 100; ++x) {
exponentExists = false; // assume exponent will not be found
for ( int t = 1; ( t < 100 ) && ( !exponentExists ); ++t )
if ( r.exponents[ x ] == temp.exponents[ t ] ) {
temp.coefficients[ t ] -= r.coefficients[ x ];
exponentExists = true; // exponent found
}
// exponent was not found, insert into temp
if ( !exponentExists ) {
temp.exponents[ s ] = r.exponents[ x ];
temp.coefficients[ s ] -= r.coefficients[ x ];
++s;
}
}
return temp;
}
Polynomial &Polynomial::operator-=( const Polynomial& r )
{
*this = *this - r;
return *this;
}
Polynomial Polynomial::operator*( const Polynomial& r )
{
Polynomial temp;
int s = 1; // subscript location for temp coefficients and exponents
for ( int x = 0; ( x < 100 ) && ( x == 0 || coefficients[ x ] != 0 ); ++x )
for ( int y = 0; ( y < 100 ) && ( y == 0 || r.coefficients[ y ] != 0 ); ++y )
if ( coefficients[ x ] * r.coefficients[ y ] )
if ( ( exponents[ x ] == 0 ) && ( r.exponents[ y ] == 0 ) )
temp.coefficients[ 0 ] += coefficients[ x ] * r.coefficients[ y ];
else {
temp.coefficients[ s ] = coefficients[ x ] * r.coefficients[ y ];
temp.exponents[ s ] = exponents[ x ] + r.exponents[ y ];
++s;
}
polynomialCombine( temp ); // combine common terms
return temp;
}
void Polynomial::polynomialCombine( Polynomial& w )
{
Polynomial temp = w;
int exp;
// zero out elements of w
for ( int x = 0; x < 100; ++x ) {
w.coefficients[ x ] = 0;
w.exponents[ x ] = 0;
}
for ( x = 1; x < 100; ++x ) {
exp = temp.exponents[ x ];
for ( int y = x + 1; y < 100; ++y )
if ( exp == temp.exponents[ y ] ) {
temp.coefficients[ x ] += temp.coefficients[ y ];
temp.exponents[ y ] = 0;
temp.coefficients[ y ] = 0;
}
}
w = temp;
}
Polynomial &Polynomial::operator*=( const Polynomial& r )
{
*this = *this * r;
return *this;
}
void Polynomial::enterTerms( void )
{
bool found = false;
int numberOfTerms, c, e;
cout << " Enter number of polynomial terms: ";
cin >> numberOfTerms;
for ( int n = 1; n <= numberOfTerms; ++n ) {
cout << " Enter coefficient: ";
cin >> c;
cout << "Enter exponent: ";
cin >> e;
if ( c != 0 ) {
// exponents of zero are forced into first element
if ( e == 0 ) {
coefficients[ 0 ] += c;
continue;
}
for ( int term = 1; ( term < 100 ) &&
( coefficients[ term ] != 0 ); ++term )
if ( e == exponents[ term ] ) {
coefficients[ term ] += c;
exponents[ term ] = e;
found = true; // existing exponent updated
}
if ( !found ) { // add term
coefficients[ term ] += c;
exponents[ term ] = e;
}
}
}
}
int main()
{
Polynomial a, b, c, t;
a.enterTerms();
b.enterTerms();
t = a; //save the value of a
cout << "First polynomial is: ";
a.printPolynomial();
cout << "Second polynomial is: ";
b.printPolynomial();
cout << " Adding the polynomials yields: ";
c = a + b;
c.printPolynomial();
cout << " += the polynomials yields: ";
a += b;
a.printPolynomial();
cout << " Subtracting the polynomials yields: ";
a = t; // reset a to original value
c = a - b;
c.printPolynomial();
cout << " -= the polynomials yields: ";
a -= b;
a.printPolynomial();
cout << " Multiplying the polynomials yields: ";
a = t; // reset a to original value
c = a * b;
c.printPolynomial();
cout << " *= the polynomials yields: ";
a *= b;
a.printPolynomial();
cout << endl;
system("pause");
return 0;
}
Solution
Here is the exact code and let me know if any errors occurs
#include
#include
#include
struct Term
{
int coeff;
unsigned exp;
Term(int c, unsigned e) : coeff(c), exp(e) {};
};
class Polynomial
{
public:
Polynomial() = default;
Polynomial(const Polynomial&) = default;
Polynomial(const std::vector& c) : coeff(c) {};
Polynomial(const Term t);
Polynomial(const std::vector& t);
std::string print() const;
Polynomial& operator=(const Polynomial& other) = default;
Polynomial& operator-=(const Polynomial& other);
Polynomial& operator+=(const Polynomial& other);
private:
std::vector coeff;
};
Polynomial::Polynomial(const Term t) : coeff(t.exp + 1)
{
coeff[t.exp] = t.coeff;
}
Polynomial::Polynomial(const std::vector& t)
{
for(auto term: t)
*this += term;
}
std::string Polynomial::print() const
{
std::string temp;
for(signed long long i = coeff.size() - 1; i >= 0; --i) {
if (coeff[i] == 0)
continue;
if (temp != "")
temp += " + ";
if (coeff[i] != 1 || i == 0)
temp += std::to_string(coeff[i]);
if (i != 0)
temp += "x^" + std::to_string(i);
}
return temp;
}
Polynomial& Polynomial::operator+=(const Polynomial& other)
{
if(coeff.size() < other.coeff.size())
coeff.resize(other.coeff.size());
for(unsigned i = 0; i < std::min(coeff.size(), other.coeff.size()); ++i) {
coeff[i] += other.coeff[i];
}
return *this;
}
Polynomial& Polynomial::operator-=(const Polynomial& other)
{
if(coeff.size() < other.coeff.size())
coeff.resize(other.coeff.size());
for(unsigned i = 0; i < std::min(coeff.size(), other.coeff.size()); ++i) {
coeff[i] -= other.coeff[i];
}
return *this;
}
const Polynomial operator+(const Polynomial& lhs, const Polynomial& rhs)
{
Polynomial tmp(lhs);
tmp += rhs;
return tmp;
}
const Polynomial operator-(const Polynomial& lhs, const Polynomial& rhs)
{
Polynomial tmp(lhs);
tmp -= rhs;
return tmp;
}
std::ostream& operator<<(std::ostream& lhs, const Polynomial& rhs)
{
return lhs << rhs.print();
}
int main()
{
Polynomial s({2, 1, 3});
std::cout << s << std::endl;
Polynomial x(Term(8, 10)); //8x^10
std::cout << x << std::endl;
Polynomial r = s - x;
std::cout << r << std::endl;
r += Term(8, 10);
std::cout << r;
}

More Related Content

Similar to I wrote the following change it to having a header, main and cpp fi.pdf

introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptxManojKhadilkar1
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd StudyChris Ohk
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency명신 김
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfandreaplotner1
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
First C++ code written... I think its quite efficient with all the.pdf
First C++ code written... I think its quite efficient with all the.pdfFirst C++ code written... I think its quite efficient with all the.pdf
First C++ code written... I think its quite efficient with all the.pdfankit482504
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2docSrikanth
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESLeahRachael
 

Similar to I wrote the following change it to having a header, main and cpp fi.pdf (20)

introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
First C++ code written... I think its quite efficient with all the.pdf
First C++ code written... I think its quite efficient with all the.pdfFirst C++ code written... I think its quite efficient with all the.pdf
First C++ code written... I think its quite efficient with all the.pdf
 
Array Cont
Array ContArray Cont
Array Cont
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 
12
1212
12
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
 

More from rishteygallery

How many four-digit numbers are there formed from the digits 1, 2, 3.pdf
How many four-digit numbers are there formed from the digits 1, 2, 3.pdfHow many four-digit numbers are there formed from the digits 1, 2, 3.pdf
How many four-digit numbers are there formed from the digits 1, 2, 3.pdfrishteygallery
 
If compound inhibits cilia growth, what is the molecular mechanism.pdf
If compound inhibits cilia growth, what is the molecular mechanism.pdfIf compound inhibits cilia growth, what is the molecular mechanism.pdf
If compound inhibits cilia growth, what is the molecular mechanism.pdfrishteygallery
 
Im having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdfIm having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdfrishteygallery
 
Green fluorescent protein is commonly used to label DNA sequences. .pdf
Green fluorescent protein is commonly used to  label DNA sequences.  .pdfGreen fluorescent protein is commonly used to  label DNA sequences.  .pdf
Green fluorescent protein is commonly used to label DNA sequences. .pdfrishteygallery
 
How can I align a face according to another face using eyes landma.pdf
How can I align a face according to another face using eyes landma.pdfHow can I align a face according to another face using eyes landma.pdf
How can I align a face according to another face using eyes landma.pdfrishteygallery
 
Harriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdf
Harriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdfHarriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdf
Harriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdfrishteygallery
 
Edouard Manets painting The Bar at the Folies Bergeres shows a girl.pdf
Edouard Manets painting The Bar at the Folies Bergeres shows a girl.pdfEdouard Manets painting The Bar at the Folies Bergeres shows a girl.pdf
Edouard Manets painting The Bar at the Folies Bergeres shows a girl.pdfrishteygallery
 
Does a cell in your big toe contain a gene for insulinA. Yes.B..pdf
Does a cell in your big toe contain a gene for insulinA. Yes.B..pdfDoes a cell in your big toe contain a gene for insulinA. Yes.B..pdf
Does a cell in your big toe contain a gene for insulinA. Yes.B..pdfrishteygallery
 

More from rishteygallery (8)

How many four-digit numbers are there formed from the digits 1, 2, 3.pdf
How many four-digit numbers are there formed from the digits 1, 2, 3.pdfHow many four-digit numbers are there formed from the digits 1, 2, 3.pdf
How many four-digit numbers are there formed from the digits 1, 2, 3.pdf
 
If compound inhibits cilia growth, what is the molecular mechanism.pdf
If compound inhibits cilia growth, what is the molecular mechanism.pdfIf compound inhibits cilia growth, what is the molecular mechanism.pdf
If compound inhibits cilia growth, what is the molecular mechanism.pdf
 
Im having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdfIm having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdf
 
Green fluorescent protein is commonly used to label DNA sequences. .pdf
Green fluorescent protein is commonly used to  label DNA sequences.  .pdfGreen fluorescent protein is commonly used to  label DNA sequences.  .pdf
Green fluorescent protein is commonly used to label DNA sequences. .pdf
 
How can I align a face according to another face using eyes landma.pdf
How can I align a face according to another face using eyes landma.pdfHow can I align a face according to another face using eyes landma.pdf
How can I align a face according to another face using eyes landma.pdf
 
Harriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdf
Harriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdfHarriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdf
Harriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdf
 
Edouard Manets painting The Bar at the Folies Bergeres shows a girl.pdf
Edouard Manets painting The Bar at the Folies Bergeres shows a girl.pdfEdouard Manets painting The Bar at the Folies Bergeres shows a girl.pdf
Edouard Manets painting The Bar at the Folies Bergeres shows a girl.pdf
 
Does a cell in your big toe contain a gene for insulinA. Yes.B..pdf
Does a cell in your big toe contain a gene for insulinA. Yes.B..pdfDoes a cell in your big toe contain a gene for insulinA. Yes.B..pdf
Does a cell in your big toe contain a gene for insulinA. Yes.B..pdf
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

I wrote the following change it to having a header, main and cpp fi.pdf

  • 1. I wrote the following: change it to having a header, main and cpp file.. THERE ARE ERRORS PLEASE FIX.. Picture of output is given below The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent, e.g., the term 2x4 has the coefficient 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set, get, and print functions. The class should also provide the following overloaded operator capabilities: a) Overload the addition operator (+) to add two Polynomials. b) Overload the subtraction operator (-) to subtract two Polynomials. c) Overload the assignment operator to assign one Polynomial to another. d) Overload the multiplication operator (*) to multiply two Polynomials. e) Overload the addition assignment operator (+=), subtraction assignment operator (-=), and multiplication assignment operator (*=). Write an application that tests all the functionality provided by class Polynomial: • create three Polynomials • add two Polynomials, using + and += operators • subtract two Polynomials, using – and -= operators • assign one Polynomial to another Polynomial • multiply two Polynomials, using * and *= operators. --------------------------------------------------------------------------------------------- #include #include using namespace std; using std::setiosflags; using std::resetiosflags; class Polynomial { public: Polynomial(); Polynomial operator+( const Polynomial& ) const; Polynomial operator-( const Polynomial& ) const; Polynomial operator*( const Polynomial& );
  • 2. const Polynomial operator=( const Polynomial&); Polynomial& operator+=( const Polynomial& ); Polynomial& operator-=( const Polynomial& ); void enterTerms( void ); void printPolynomial( void ) const; private: int exponents[ 100 ]; int coefficients[ 100 ]; }; Polynomial::Polynomial() { for ( int t = 0; t < 100; ++t ) { coefficients[ t ] = 0; exponents[ t ] = 0; } } void Polynomial::printPolynomial( void ) const { int start; bool zero = false; if ( coefficients[ 0 ] ) { // output constants cout << coefficients[ 0 ]; start = 1; zero = true; // at least one term exists } else { if ( coefficients[ 1 ] ) { cout << coefficients[ 1 ] << 'x'; // constant does not exist // so output first term // without a sign if ( ( exponents[ 1 ] != 0 ) && ( exponents[ 1 ] != 1 ) ) cout << '^' << exponents[ 1 ]; zero = true; // at least one term exists } start = 2; }
  • 3. // output remaining polynomial terms for ( int x = start; x < 100; ++x ) { if ( coefficients[ x ] != 0 ) { cout << setiosflags( ios::showpos ) << coefficients[ x ] << resetiosflags( ios::showpos ) << 'x'; if ( ( exponents[ x ] != 0 ) && ( exponents[ x ] != 1 ) ) cout << '^' << exponents[ x ]; zero = true; // at least one term exists } } if ( !zero ) // no terms exist in the polynomial cout << '0'; cout << endl; } const Polynomial Polynomial::operator=( const Polynomial& r ) { exponents[ 0 ] = r.exponents[ 0 ]; coefficients[ 0 ] = r.coefficients[ 0 ]; for ( int s = 1; ( s < 100 ); ++s ) { if ( r.exponents[ s ] != 0 ) { exponents[ s ] = r.exponents[ s ]; coefficients[ s ] = r.coefficients[ s ]; } else { if ( exponents[ s ] == 0 ) break; exponents[ s ] = 0; coefficients[ s ] = 0; } } return *this; } Polynomial Polynomial::operator+( const Polynomial& r ) const { Polynomial temp; bool exponentExists;
  • 4. // process element with a zero exponent temp.coefficients[ 0 ] = coefficients[ 0 ] + r.coefficients[ 0 ]; for ( int s = 1; ( s < 100 ) && ( r.exponents[ s ] != 0 ); ++s ) { temp.coefficients[ s ] = r.coefficients[ s ]; temp.exponents[ s ] = r.exponents[ s ]; } for ( int x = 1; x < 100; ++x ) { exponentExists = false; // assume exponent will not be found for ( int t = 1; ( t < 100 ) && ( !exponentExists ); ++t ) if ( exponents[ x ] == temp.exponents[ t ] ) { temp.coefficients[ t ] += coefficients[ x ]; exponentExists = true; // exponent found } // exponent was not found, insert into temp if ( !exponentExists ) { temp.exponents[ s ] = exponents[ x ]; temp.coefficients[ s ] += coefficients[ x ]; ++s; } } return temp; } Polynomial &Polynomial::operator+=( const Polynomial &r ) { *this = *this + r; return *this; } Polynomial Polynomial::operator-( const Polynomial& r ) const { Polynomial temp; bool exponentExists; // process element with a zero exponent temp.coefficients[ 0 ] = coefficients[ 0 ] - r.coefficients[ 0 ]; // copy left arrays into temp object s will be used to keep // track of first open coefficient element for ( int s = 1; ( s < 100 ) && ( exponents[ s ] != 0 ); ++s ) {
  • 5. temp.coefficients[ s ] = coefficients[ s ]; temp.exponents[ s ] = exponents[ s ]; } for ( int x = 1; x < 100; ++x) { exponentExists = false; // assume exponent will not be found for ( int t = 1; ( t < 100 ) && ( !exponentExists ); ++t ) if ( r.exponents[ x ] == temp.exponents[ t ] ) { temp.coefficients[ t ] -= r.coefficients[ x ]; exponentExists = true; // exponent found } // exponent was not found, insert into temp if ( !exponentExists ) { temp.exponents[ s ] = r.exponents[ x ]; temp.coefficients[ s ] -= r.coefficients[ x ]; ++s; } } return temp; } Polynomial &Polynomial::operator-=( const Polynomial& r ) { *this = *this - r; return *this; } Polynomial Polynomial::operator*( const Polynomial& r ) { Polynomial temp; int s = 1; // subscript location for temp coefficients and exponents for ( int x = 0; ( x < 100 ) && ( x == 0 || coefficients[ x ] != 0 ); ++x ) for ( int y = 0; ( y < 100 ) && ( y == 0 || r.coefficients[ y ] != 0 ); ++y ) if ( coefficients[ x ] * r.coefficients[ y ] ) if ( ( exponents[ x ] == 0 ) && ( r.exponents[ y ] == 0 ) ) temp.coefficients[ 0 ] += coefficients[ x ] * r.coefficients[ y ]; else { temp.coefficients[ s ] = coefficients[ x ] * r.coefficients[ y ]; temp.exponents[ s ] = exponents[ x ] + r.exponents[ y ];
  • 6. ++s; } polynomialCombine( temp ); // combine common terms return temp; } void Polynomial::polynomialCombine( Polynomial& w ) { Polynomial temp = w; int exp; // zero out elements of w for ( int x = 0; x < 100; ++x ) { w.coefficients[ x ] = 0; w.exponents[ x ] = 0; } for ( x = 1; x < 100; ++x ) { exp = temp.exponents[ x ]; for ( int y = x + 1; y < 100; ++y ) if ( exp == temp.exponents[ y ] ) { temp.coefficients[ x ] += temp.coefficients[ y ]; temp.exponents[ y ] = 0; temp.coefficients[ y ] = 0; } } w = temp; } Polynomial &Polynomial::operator*=( const Polynomial& r ) { *this = *this * r; return *this; } void Polynomial::enterTerms( void ) { bool found = false; int numberOfTerms, c, e; cout << " Enter number of polynomial terms: "; cin >> numberOfTerms;
  • 7. for ( int n = 1; n <= numberOfTerms; ++n ) { cout << " Enter coefficient: "; cin >> c; cout << "Enter exponent: "; cin >> e; if ( c != 0 ) { // exponents of zero are forced into first element if ( e == 0 ) { coefficients[ 0 ] += c; continue; } for ( int term = 1; ( term < 100 ) && ( coefficients[ term ] != 0 ); ++term ) if ( e == exponents[ term ] ) { coefficients[ term ] += c; exponents[ term ] = e; found = true; // existing exponent updated } if ( !found ) { // add term coefficients[ term ] += c; exponents[ term ] = e; } } } } int main() { Polynomial a, b, c, t; a.enterTerms(); b.enterTerms(); t = a; //save the value of a cout << "First polynomial is: "; a.printPolynomial(); cout << "Second polynomial is: "; b.printPolynomial(); cout << " Adding the polynomials yields: ";
  • 8. c = a + b; c.printPolynomial(); cout << " += the polynomials yields: "; a += b; a.printPolynomial(); cout << " Subtracting the polynomials yields: "; a = t; // reset a to original value c = a - b; c.printPolynomial(); cout << " -= the polynomials yields: "; a -= b; a.printPolynomial(); cout << " Multiplying the polynomials yields: "; a = t; // reset a to original value c = a * b; c.printPolynomial(); cout << " *= the polynomials yields: "; a *= b; a.printPolynomial(); cout << endl; system("pause"); return 0; } Solution Here is the exact code and let me know if any errors occurs #include #include #include struct Term { int coeff; unsigned exp; Term(int c, unsigned e) : coeff(c), exp(e) {}; };
  • 9. class Polynomial { public: Polynomial() = default; Polynomial(const Polynomial&) = default; Polynomial(const std::vector& c) : coeff(c) {}; Polynomial(const Term t); Polynomial(const std::vector& t); std::string print() const; Polynomial& operator=(const Polynomial& other) = default; Polynomial& operator-=(const Polynomial& other); Polynomial& operator+=(const Polynomial& other); private: std::vector coeff; }; Polynomial::Polynomial(const Term t) : coeff(t.exp + 1) { coeff[t.exp] = t.coeff; } Polynomial::Polynomial(const std::vector& t) { for(auto term: t) *this += term; } std::string Polynomial::print() const { std::string temp; for(signed long long i = coeff.size() - 1; i >= 0; --i) { if (coeff[i] == 0) continue; if (temp != "") temp += " + "; if (coeff[i] != 1 || i == 0) temp += std::to_string(coeff[i]); if (i != 0) temp += "x^" + std::to_string(i);
  • 10. } return temp; } Polynomial& Polynomial::operator+=(const Polynomial& other) { if(coeff.size() < other.coeff.size()) coeff.resize(other.coeff.size()); for(unsigned i = 0; i < std::min(coeff.size(), other.coeff.size()); ++i) { coeff[i] += other.coeff[i]; } return *this; } Polynomial& Polynomial::operator-=(const Polynomial& other) { if(coeff.size() < other.coeff.size()) coeff.resize(other.coeff.size()); for(unsigned i = 0; i < std::min(coeff.size(), other.coeff.size()); ++i) { coeff[i] -= other.coeff[i]; } return *this; } const Polynomial operator+(const Polynomial& lhs, const Polynomial& rhs) { Polynomial tmp(lhs); tmp += rhs; return tmp; } const Polynomial operator-(const Polynomial& lhs, const Polynomial& rhs) { Polynomial tmp(lhs); tmp -= rhs; return tmp; } std::ostream& operator<<(std::ostream& lhs, const Polynomial& rhs) { return lhs << rhs.print();
  • 11. } int main() { Polynomial s({2, 1, 3}); std::cout << s << std::endl; Polynomial x(Term(8, 10)); //8x^10 std::cout << x << std::endl; Polynomial r = s - x; std::cout << r << std::endl; r += Term(8, 10); std::cout << r; }