Complete the code below for this task:
Task:
Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent—e.g., the term 2x^4 has the coefficient 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded capabilities:
Overload the addition operator (+) to add two polynomials.
Overload the subtraction operator (-) to subtract two polynomials.
Overload the assignment operator to assign one polynomial to another.
Overload the multiplication operator () to multiply two polynomials.
Overload the addition assignment operator (+=), subtraction assignment operator (-=), and multiplication assignment operator (=).
Code (Add namespace):
code
Assistant
User
Can you add namespace in necessary files and also simple comments
Assistant
User
I just wanted using namespace std; included in Polynomial.h
Assistant
Sure! Here's the modified Polynomial.h with the using namespace std; statement included:
Polynomial.h
cpp
User
Assistant
User
Write the program as if by a beginner. Dial back on unnecessary comments.
Assistant
// Polynomial.h
// Defines the Polynomial class
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <vector>
class Polynomial {
private:
std::vector<std::pair<int, int>> terms; // Stores terms as pairs (coefficient, exponent)
public:
// Constructors
Polynomial();
Polynomial(const std::vector<std::pair<int, int>>& terms);
// Destructor
~Polynomial();
// Getter and Setter
const std::vector<std::pair<int, int>>& getTerms() const;
void setTerms(const std::vector<std::pair<int, int>>& terms);
// Overloaded Operators
Polynomial operator+(const Polynomial& other) const;
Polynomial operator-(const Polynomial& other) const;
Polynomial operator*(const Polynomial& other) const;
Polynomial& operator=(const Polynomial& other);
Polynomial& operator+=(const Polynomial& other);
Polynomial& operator-=(const Polynomial& other);
Polynomial& operator*=(const Polynomial& other);
};
#endif
// Polynomial.cpp
// Implements the Polynomial class
#include "Polynomial.h"
// Constructors
Polynomial::Polynomial() {}
Polynomial::Polynomial(const std::vector<std::pair<int, int>>& terms) : terms(terms) {}
// Destructor
Polynomial::~Polynomial() {}
// Getter and Setter
const std::vector<std::pair<int, int>>& Polynomial::getTerms() const {
return terms;
}
void Polynomial::setTerms(const std::vector<std::pair<int, int>>& terms) {
this->terms = terms;
}
// Overloaded Operators
Polynomial Polynomial::operator+(const Polynomial& other) const {
// Implement addition logic here
}
Polynomial Polynomial::operator-(const Polynomial& other) const {
// Implement subtraction logic here
}
Polynomial& Polynomial::operator=(const Polynomial& other) {
if (this != &other) {
// Implement assignment logic here
}
return *this;
}
Polynomial Polynomial::operator*(const Polynomial& other) const {
// Implement multiplication logic here
}
Polynomial& Polynomial::operator+=(const Polynomial& other) {
// Implement addition assignment logic here
}
Polynomial& Polynomial::operator-=(const Polynomial& other) {
// Implement subtraction assignment logic here
}
Polynomial& Polynomial::operator*=(const Polynomial& other) {
// Implement multiplication assignment logic here
}
#include <iostream>
#include <iomanip>
#include "Polynomial.h"
using namespace std;
int main() {
int max_degree = 6; // Maximum degree of a polynomial
int num_terms1, num_terms2;
// Input for the first polynomial
std::cout << "Enter the number of polynomial terms for the first polynomial: ";
std::cin >> num_terms1;
Polynomial poly1(max_degree);
std::cout << "Enter coefficient and exponent for each term:" << std::endl;
for (int i = 0; i < num_terms1; i++) {
int coef, exp;
std::cin >> coef >> exp;
poly1.setTerm(coef, exp);
}
// Input for the second polynomial
std::cout << "Enter the number of polynomial terms for the second polynomial: ";
std::cin >> num_terms2;
Polynomial poly2(max_degree);
std::cout << "Enter coefficient and exponent for each term:" << std::endl;
for (int i = 0; i < num_terms2; i++) {
int coef, exp;
std::cin >> coef >> exp;
poly2.setTerm(coef, exp);
}
// Display the input polynomials
std::cout << "First Polynomial is:" << std::endl;
std::cout << poly1 << std::endl;
std::cout << "Second Polynomial is:" << std::endl;
std::cout << poly2 << std::endl;
// Perform polynomial operations
Polynomial result;
result = poly1 + poly2;
std::cout << "Adding polynomials yields:" << std::endl;
std::cout << result << std::endl;
result = poly1 - poly2;
std::cout << "Subtracting polynomials yields:" << std::endl;
std::cout << result << std::endl;
result = poly1 * poly2;
std::cout << "Multiplying polynomials yields:" << std::endl;
std::cout << result << std::endl;
return 0;
}
Sure! Here’s a complete implementation of the Polynomial class, including the necessary logical operations for addition, subtraction, multiplication, and operator overloading. I've included algorithmic details and a complete example of how the class can be used, including input and output.
// Polynomial.cpp
// Implements the Polynomial class
#include "Polynomial.h"
// Constructors
Polynomial::Polynomial() {}
Polynomial::Polynomial(const std::vector<std::pair<int, int>>& terms) {
for (const auto& term : terms) {
setTerm(term.first, term.second);
}
}
// Destructor
Polynomial::~Polynomial() {}
// Getter for terms
const std::map<int, int>& Polynomial::getTerms() const {
return terms;
}
// Set a term in the polynomial
void Polynomial::setTerm(int coefficient, int exponent) {
if (coefficient != 0) { // Ignore zero coefficients
terms[exponent] += coefficient;
if (terms[exponent] == 0) {
terms.erase(exponent); // Remove term if coefficient becomes zero
}
}
}
// Overloaded Operators
Polynomial Polynomial::operator+(const Polynomial& other) const {
Polynomial result = *this;
for (const auto& term : other.terms) {
result.setTerm(term.second, term.first); // (coef, exp)
}
return result;
}
Polynomial Polynomial::operator-(const Polynomial& other) const {
Polynomial result = *this;
for (const auto& term : other.terms) {
result.setTerm(-term.second, term.first); // Subtract terms
}
return result;
}
Polynomial& Polynomial::operator=(const Polynomial& other) {
if (this != &other) {
terms = other.terms; // Use assignment operator of map
}
return *this;
}
Polynomial Polynomial::operator*(const Polynomial& other) const {
Polynomial result;
for (const auto& term1 : terms) {
for (const auto& term2 : other.terms) {
int new_coef = term1.second * term2.second;
int new_exp = term1.first + term2.first;
result.setTerm(new_coef, new_exp);
}
}
return result;
}
Polynomial& Polynomial::operator+=(const Polynomial& other) {
return *this = *this + other;
}
Polynomial& Polynomial::operator-=(const Polynomial& other) {
return *this = *this - other;
}
Polynomial& Polynomial::operator*=(const Polynomial& other) {
return *this = *this * other;
}
// Friend function for output
std::ostream& operator<<(std::ostream& os, const Polynomial& poly) {
bool first = true;
for (auto it = poly.terms.rbegin(); it != poly.terms.rend(); ++it) {
int exponent = it->first;
int coefficient = it->second;
if (first) {
os << coefficient << "x^" << exponent;
first = false;
} else {
os << (coefficient >= 0 ? " + " : " - ") << abs(coefficient) << "x^" << exponent;
}
}
return os;
}
Main.cpp
Here’s how you can use the polynomial class in a simple main function:
cpp
#include <iostream>
#include "Polynomial.h"
using namespace std;
int main() {
int num_terms1, num_terms2;
// Input for the first polynomial
cout << "Enter the number of polynomial terms for the first polynomial: ";
cin >> num_terms1;
Polynomial poly1;
cout << "Enter coefficient and exponent for each term:" << endl;
for (int i = 0; i < num_terms1; i++) {
int coef, exp;
cin >> coef >> exp;
poly1.setTerm(coef, exp);
}
// Input for the second polynomial
cout << "Enter the number of polynomial terms for the second polynomial: ";
cin >> num_terms2;
Polynomial poly2;
cout << "Enter coefficient and exponent for each term:" << endl;
for (int i = 0; i < num_terms2; i++) {
int coef, exp;
cin >> coef >> exp;
poly2.setTerm(coef, exp);
}
// Display the input polynomials
cout << "First Polynomial: " << endl << poly1 << endl;
cout << "Second Polynomial: " << endl << poly2 << endl;
// Perform polynomial operations
Polynomial result;
result = poly1 + poly2;
cout << "Adding polynomials yields: " << endl << result << endl;
result = poly1 - poly2;
cout << "Subtracting polynomials yields: " << endl << result << endl;
result = poly1 * poly2;
cout << "Multiplying polynomials yields: " << endl << result << endl;
return 0;
}
Explanation of the Implementation
Internal Representation: The terms are stored as a map from exponent to coefficients, which allows easy manipulation and ensures that terms with the same exponent are combined properly.
Operators: Each operator is implemented in a way that creates a new Polynomial object and can be chained through assignment.
Output: The operator<< is overloaded to format the polynomial neatly when printed.
You can compile all parts together and run the program to perform the operations on polynomials as described. Ensure you have a suitable C++ environment to compile and run the code.
Certainly! Here’s the updated code for Polynomial.h, Polynomial.cpp, and the main.cpp file, which includes a namespace and simple comments for clarity.
#include <iostream>
#include "Polynomial.h"
using namespace std;
using namespace PolynomialNamespace; // Using the Polynomial namespace
int main() {
int num_terms1, num_terms2;
// Input for the first polynomial
cout << "Enter the number of polynomial terms for the first polynomial: ";
cin >> num_terms1;
Polynomial poly1;
cout << "Enter coefficient and exponent for each term:" << endl;
for (int i = 0; i < num_terms1; i++) {
int coef, exp;
cin >> coef >> exp; // Input coefficient and exponent
poly1.setTerm(coef, exp); // Set the term in the polynomial
}
// Input for the second polynomial
cout << "Enter the number of polynomial terms for the second polynomial: ";
cin >> num_terms2;
Polynomial poly2;
cout << "Enter coefficient and exponent for each term:" << endl;
for (int i = 0; i < num_terms2; i++) {
int coef, exp;
cin >> coef >> exp; // Input coefficient and exponent
poly2.setTerm(coef, exp); // Set the term in the polynomial
}
// Display the input polynomials
cout << "First Polynomial: " << endl << poly1 << endl;
cout << "Second Polynomial: " << endl << poly2 << endl;
// Perform polynomial operations
Polynomial result;
result = poly1 + poly2; // Adding the polynomials
cout << "Adding polynomials yields: " << endl << result << endl;
result = poly1 - poly2; // Subtracting the polynomials
cout << "Subtracting polynomials yields: " << endl << result << endl;
result = poly1 * poly2; // Multiplying the polynomials
cout << "Multiplying polynomials yields: " << endl << result << endl;
return 0; // Exit the program
}
Summary of Changes
Namespace: The code has been encapsulated in a PolynomialNamespace to avoid naming conflicts and provide structure.
Comments: Simple comments have been added throughout the code to explain the purpose of each section and the role of individual functions.
With these changes, the code is clearer and still functions as intended. Make sure to include all files when you compile the program.
Added using namespace std; to the top of the Polynomial.h file, allowing direct usage of std types like vector, map, pair, and ostream without the std:: prefix.
The rest of the files (Polynomial.cpp and main.cpp) remain unchanged and should continue to work correctly with these definitions. Make sure to include all the files in your project to compile and run the complete program.
Please simply complete the code without too much change. Include comments and using namespace std;.
Task:
Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent—e.g., the term 2x^4 has the coefficient 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded capabilities:
Overload the addition operator (+) to add two polynomials.
Overload the subtraction operator (-) to subtract two polynomials.
Overload the assignment operator to assign one polynomial to another.
Overload the multiplication operator () to multiply two polynomials.
Overload the addition assignment operator (+=), subtraction assignment operator (-=), and multiplication assignment operator (=).
code
// Polynomial.h
// Defines the Polynomial class
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <vector>
class Polynomial {
private:
std::vector<std::pair<int, int>> terms; // Stores terms as pairs (coefficient, exponent)
public:
// Constructors
Polynomial();
Polynomial(const std::vector<std::pair<int, int>>& terms);
// Destructor
~Polynomial();
// Getter and Setter
const std::vector<std::pair<int, int>>& getTerms() const;
void setTerms(const std::vector<std::pair<int, int>>& terms);
// Overloaded Operators
Polynomial operator+(const Polynomial& other) const;
Polynomial operator-(const Polynomial& other) const;
Polynomial operator*(const Polynomial& other) const;
Polynomial& operator=(const Polynomial& other);
Polynomial& operator+=(const Polynomial& other);
Polynomial& operator-=(const Polynomial& other);
Polynomial& operator*=(const Polynomial& other);
};
#endif
// Polynomial.cpp
// Implements the Polynomial class
#include "Polynomial.h"
// Constructors
Polynomial::Polynomial() {}
Polynomial::Polynomial(const std::vector<std::pair<int, int>>& terms) : terms(terms) {}
// Destructor
Polynomial::~Polynomial() {}
// Getter and Setter
const std::vector<std::pair<int, int>>& Polynomial::getTerms() const {
return terms;
}
void Polynomial::setTerms(const std::vector<std::pair<int, int>>& terms) {
this->terms = terms;
}
// Overloaded Operators
Polynomial Polynomial::operator+(const Polynomial& other) const {
// Implement addition logic here
}
Polynomial Polynomial::operator-(const Polynomial& other) const {
// Implement subtraction logic here
}
Polynomial& Polynomial::operator=(const Polynomial& other) {
if (this != &other) {
// Implement assignment logic here
}
return *this;
}
Polynomial Polynomial::operator*(const Polynomial& other) const {
// Implement multiplication logic here
}
Polynomial& Polynomial::operator+=(const Polynomial& other) {
// Implement addition assignment logic here
}
Polynomial& Polynomial::operator-=(const Polynomial& other) {
// Implement subtraction assignment logic here
}
Polynomial& Polynomial::operator*=(const Polynomial& other) {
// Implement multiplication assignment logic here
}
#include <iostream>
#include <iomanip>
#include "Polynomial.h"
using namespace std;
int main() {
int max_degree = 6; // Maximum degree of a polynomial
int num_terms1, num_terms2;
// Input for the first polynomial
std::cout << "Enter the number of polynomial terms for the first polynomial: ";
std::cin >> num_terms1;
Polynomial poly1(max_degree);
std::cout << "Enter coefficient and exponent for each term:" << std::endl;
for (int i = 0; i < num_terms1; i++) {
int coef, exp;
std::cin >> coef >> exp;
poly1.setTerm(coef, exp);
}
// Input for the second polynomial
std::cout << "Enter the number of polynomial terms for the second polynomial: ";
std::cin >> num_terms2;
Polynomial poly2(max_degree);
std::cout << "Enter coefficient and exponent for each term:" << std::endl;
for (int i = 0; i < num_terms2; i++) {
int coef, exp;
std::cin >> coef >> exp;
poly2.setTerm(coef, exp);
}
// Display the input polynomials
std::cout << "First Polynomial is:" << std::endl;
std::cout << poly1 << std::endl;
std::cout << "Second Polynomial is:" << std::endl;
std::cout << poly2 << std::endl;
// Perform polynomial operations
Polynomial result;
result = poly1 + poly2;
std::cout << "Adding polynomials yields:" << std::endl;
std::cout << result << std::endl;
result = poly1 - poly2;
std::cout << "Subtracting polynomials yields:" << std::endl;
std::cout << result << std::endl;
result = poly1 * poly2;
std::cout << "Multiplying polynomials yields:" << std::endl;
std::cout << result << std::endl;
return 0;
}
Here is the completed implementation of the Polynomial class based on your description, including the necessary methods and operator overloads. I've also added comments to clarify the functionality throughout the code.
Polynomial.h
cpp
// Polynomial.h
// Defines the Polynomial class
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <vector>
#include <utility> // For std::pair
using namespace std; // Include standard namespace
class Polynomial {
private:
vector<pair<int, int>> terms; // Stores terms as pairs (coefficient, exponent)
public:
// Constructors
Polynomial(); // Default constructor
Polynomial(const vector<pair<int, int>>& terms); // Constructor with terms
// Destructor
~Polynomial(); // Destructor
// Getter and Setter
const vector<pair<int, int>>& getTerms() const; // Get terms
void setTerms(const vector<pair<int, int>>& terms); // Set terms
void setTerm(int coefficient, int exponent); // Set a single term
// Overloaded Operators
Polynomial operator+(const Polynomial& other) const; // Addition
Polynomial operator-(const Polynomial& other) const; // Subtraction
Polynomial operator*(const Polynomial& other) const; // Multiplication
Polynomial& operator=(const Polynomial& other); // Assignment
Polynomial& operator+=(const Polynomial& other); // Addition assignment
Polynomial& operator-=(const Polynomial& other); // Subtraction assignment
Polynomial& operator*=(const Polynomial& other); // Multiplication assignment
// Friend function for output
friend ostream& operator<<(ostream& os, const Polynomial& poly); // Output operator
};
#endif
Polynomial.cpp
cpp
Main.cpp
cpp
#include <iostream>
#include "Polynomial.h"
using namespace std; // Include standard namespace
int main() {
int num_terms1, num_terms2;
// Input for the first polynomial
cout << "Enter the number of polynomial terms for the first polynomial: ";
cin >> num_terms1;
Polynomial poly1;
cout << "Enter coefficient and exponent for each term:" << endl;
for (int i = 0; i < num_terms1; i++) {
int coef, exp;
cin >> coef >> exp; // Input coefficient and exponent
poly1.setTerm(coef, exp); // Set the term in the polynomial
}
// Input for the second polynomial
cout << "Enter the number of polynomial terms for the second polynomial: ";
cin >> num_terms2;
Polynomial poly2;
cout << "Enter coefficient and exponent for each term:" << endl;
for (int i = 0; i < num_terms2; i++) {
int coef, exp;
cin >> coef >> exp; // Input coefficient and exponent
poly2.setTerm(coef, exp); // Set the term in the polynomial
}
// Display the polynomials
cout << "First Polynomial: " << endl << poly1 << endl;
cout << "Second Polynomial: " << endl << poly2 << endl;
// Perform polynomial operations
Polynomial result;
result = poly1 + poly2; // Adding the polynomials
cout << "Adding polynomials yields: " << endl << result << endl;
result = poly1 - poly2; // Subtracting the polynomials
cout << "Subtracting polynomials yields: " << endl << result << endl;
result = poly1 * poly2; // Multiplying the polynomials
cout << "Multiplying polynomials yields: " << endl << result << endl;
return 0; // Exit the program
}
Summary of the Implementation
Constructors and Destructor: The class includes default and parameterized constructors, as well as a destructor.
Term Management: Terms are stored as pairs (coefficient and exponent). The setTerm method manages term insertion and updates.
Operator Overloading: The class overloads the +, -, *, +=, -=, and *= operators for polynomial arithmetic.
Friend Function: The operator<< is defined as a friend function to facilitate pretty printing of the polynomial.
Basic User Interface: The main function allows users to enter data for two polynomials and computes their sum, difference, and product.
This implementation should function as expected, allowing polynomial arithmetic as specified in your requirements.
Sure! Here's a simplified version of the Polynomial class implementation, written in a straightforward manner suitable for a beginner, with only essential comments included.
// Polynomial.cpp
// Implements the Polynomial class
#include "Polynomial.h"
// Constructors
Polynomial::Polynomial() {}
Polynomial::Polynomial(const vector<pair<int, int>>& terms) : terms(terms) {}
// Destructor
Polynomial::~Polynomial() {}
// Getter and Setter
const vector<pair<int, int>>& Polynomial::getTerms() const {
return terms;
}
void Polynomial::setTerms(const vector<pair<int, int>>& terms) {
this->terms = terms;
}
void Polynomial::setTerm(int coefficient, int exponent) {
for (auto& term : terms) {
if (term.second == exponent) {
term.first += coefficient;
if (term.first == 0) {
terms.erase(remove(terms.begin(), terms.end(), term), terms.end());
}
return;
}
}
terms.push_back(make_pair(coefficient, exponent));
}
// Overloaded Operators
Polynomial Polynomial::operator+(const Polynomial& other) const {
Polynomial result = *this;
for (const auto& term : other.terms) {
result.setTerm(term.first, term.second);
}
return result;
}
Polynomial Polynomial::operator-(const Polynomial& other) const {
Polynomial result = *this;
for (const auto& term : other.terms) {
result.setTerm(-term.first, term.second);
}
return result;
}
Polynomial& Polynomial::operator=(const Polynomial& other) {
if (this != &other) {
terms = other.terms;
}
return *this;
}
Polynomial Polynomial::operator*(const Polynomial& other) const {
Polynomial result;
for (const auto& term1 : terms) {
for (const auto& term2 : other.terms) {
int new_coef = term1.first * term2.first;
int new_exp = term1.second + term2.second;
result.setTerm(new_coef, new_exp);
}
}
return result;
}
Polynomial& Polynomial::operator+=(const Polynomial& other) {
*this = *this + other;
return *this;
}
Polynomial& Polynomial::operator-=(const Polynomial& other) {
*this = *this - other;
return *this;
}
Polynomial& Polynomial::operator*=(const Polynomial& other) {
*this = *this * other;
return *this;
}
// Friend function for output
ostream& operator<<(ostream& os, const Polynomial& poly) {
if (poly.terms.empty()) {
return os << "0";
}
bool first = true;
for (const auto& term : poly.terms) {
int coef = term.first;
int exp = term.second;
if (first) {
os << (coef < 0 ? "-" : "") << abs(coef);
first = false;
} else {
os << (coef >= 0 ? " + " : " - ") << abs(coef);
}
if (exp != 0) {
os << "x^" << exp;
}
}
return os;
}
Main.cpp
cpp
#include <iostream>
#include "Polynomial.h"
using namespace std;
int main() {
int num_terms1, num_terms2;
// Input for the first polynomial
cout << "Enter the number of polynomial terms for the first polynomial: ";
cin >> num_terms1;
Polynomial poly1;
cout << "Enter coefficient and exponent for each term:" << endl;
for (int i = 0; i < num_terms1; i++) {
int coef, exp;
cin >> coef >> exp;
poly1.setTerm(coef, exp);
}
// Input for the second polynomial
cout << "Enter the number of polynomial terms for the second polynomial: ";
cin >> num_terms2;
Polynomial poly2;
cout << "Enter coefficient and exponent for each term:" << endl;
for (int i = 0; i < num_terms2; i++) {
int coef, exp;
cin >> coef >> exp;
poly2.setTerm(coef, exp);
}
// Display the polynomials
cout << "First Polynomial: " << endl << poly1 << endl;
cout << "Second Polynomial: " << endl << poly2 << endl;
// Perform polynomial operations
Polynomial result;
result = poly1 + poly2;
cout << "Adding polynomials yields: " << endl << result << endl;
result = poly1 - poly2;
cout << "Subtracting polynomials yields: " << endl << result << endl;
result = poly1 * poly2;
cout << "Multiplying polynomials yields: " << endl << result << endl;
return 0;
}
Summary
The code defines a Polynomial class that allows for basic polynomial arithmetic: addition, subtraction, and multiplication.
The implementation is straightforward, with minimal comments to keep it clear and focused for beginners.
Users can create polynomials by inputting their coefficients and exponents, and the results of operations are printed to the console.
This should provide a good balance of functionality and simplicity for someone who is learning to work with classes and basic operator overloading in C++.
// Polynomial.cpp
// Implements the Polynomial class
#include "Polynomial.h"
namespace PolynomialNamespace {
// Constructors
Polynomial::Polynomial() {} // Default constructor
Polynomial::Polynomial(const std::vector<std::pair<int, int>>& terms) {
// Initialize polynomial with given terms
for (const auto& term : terms) {
setTerm(term.first, term.second); // (coefficient, exponent)
}
}
// Destructor
Polynomial::~Polynomial() {}
// Getter for terms
const std::map<int, int>& Polynomial::getTerms() const {
return terms; // Return the map of terms
}
// Set a term in the polynomial
void Polynomial::setTerm(int coefficient, int exponent) {
if (coefficient != 0) { // Ignore zero coefficients
terms[exponent] += coefficient; // Add coefficient to the existing term
if (terms[exponent] == 0) {
terms.erase(exponent); // Remove term if coefficient becomes zero
}
}
}
// Overloaded Operators
Polynomial Polynomial::operator+(const Polynomial& other) const {
Polynomial result = *this; // Start with the current polynomial
for (const auto& term : other.terms) {
result.setTerm(term.second, term.first); // Add terms from the other polynomial
}
return result; // Return the resulting polynomial
}
Polynomial Polynomial::operator-(const Polynomial& other) const {
Polynomial result = *this; // Start with the current polynomial
for (const auto& term : other.terms) {
result.setTerm(-term.second, term.first); // Subtract terms from the other polynomial
}
return result; // Return the resulting polynomial
}
Polynomial& Polynomial::operator=(const Polynomial& other) {
if (this != &other) {
terms = other.terms; // Use assignment operator of map
}
return *this; // Return the current object
}
Polynomial Polynomial::operator*(const Polynomial& other) const {
Polynomial result; // Create a new polynomial for the result
for (const auto& term1 : terms) {
for (const auto& term2 : other.terms) {
// Multiply coefficients and add exponents
int new_coef = term1.second * term2.second;
int new_exp = term1.first + term2.first;
result.setTerm(new_coef, new_exp); // Set the resulting term
}
}
return result; // Return the resulting polynomial
}
Polynomial& Polynomial::operator+=(const Polynomial& other) {
return *this = *this + other; // Perform addition assignment
}
Polynomial& Polynomial::operator-=(const Polynomial& other) {
return *this = *this - other; // Perform subtraction assignment
}
Polynomial& Polynomial::operator*=(const Polynomial& other) {
return *this = *this * other; // Perform multiplication assignment
}
// Friend function for output
std::ostream& operator<<(std::ostream& os, const Polynomial& poly) {
bool first = true; // Flag for first term
for (auto it = poly.terms.rbegin(); it != poly.terms.rend(); ++it) {
int exponent = it->first; // Get the exponent
int coefficient = it->second; // Get the coefficient
if (first) {
os << coefficient << "x^" << exponent; // Output the first term without a sign
first = false; // Set the flag to false
} else {
os << (coefficient >= 0 ? " + " : " - ") << abs(coefficient) << "x^" << exponent; // Output other terms with signs
}
}
return os; // Return the output stream
}
} // namespace PolynomialNamespace
// Polynomial.cpp
// Implements the Polynomial class
#include "Polynomial.h"
// Constructors
Polynomial::Polynomial() {} // Default constructor
Polynomial::Polynomial(const vector<pair<int, int>>& terms) : terms(terms) {}
// Destructor
Polynomial::~Polynomial() {}
// Getter and Setter
const vector<pair<int, int>>& Polynomial::getTerms() const {
return terms; // Return the terms of the polynomial
}
void Polynomial::setTerms(const vector<pair<int, int>>& terms) {
this->terms = terms; // Set the terms of the polynomial
}
// Function to set a single term in the polynomial
void Polynomial::setTerm(int coefficient, int exponent) {
for (auto& term : terms) {
if (term.second == exponent) { // If the exponent exists
term.first += coefficient; // Add to existing coefficient
if (term.first == 0) { // Remove term if coefficient becomes zero
terms.erase(remove(terms.begin(), terms.end(), term), terms.end());
}
return;
}
}
terms.push_back(make_pair(coefficient, exponent)); // Add new term if not found
}
// Overloaded Operators
Polynomial Polynomial::operator+(const Polynomial& other) const {
Polynomial result = *this; // Start with a copy of the current polynomial
for (const auto& term : other.terms) {
result.setTerm(term.first, term.second); // Add each term from the other polynomial
}
return result; // Return the resulting polynomial
}
Polynomial Polynomial::operator-(const Polynomial& other) const {
Polynomial result = *this; // Start with a copy of the current polynomial
for (const auto& term : other.terms) {
result.setTerm(-term.first, term.second); // Subtract each term
}
return result; // Return the resulting polynomial
}
Polynomial& Polynomial::operator=(const Polynomial& other) {
if (this != &other) { // Check for self-assignment
terms = other.terms; // Copy the terms from the other polynomial
}
return *this; // Return the current object
}
Polynomial Polynomial::operator*(const Polynomial& other) const {
Polynomial result; // Create an empty polynomial to store the result
for (const auto& term1 : terms) {
for (const auto& term2 : other.terms) {
int new_coef = term1.first * term2.first; // Multiply coefficients
int new_exp = term1.second + term2.second; // Add exponents
result.setTerm(new_coef, new_exp); // Set the resulting term
}
}
return result; // Return the resulting polynomial
}
Polynomial& Polynomial::operator+=(const Polynomial& other) {
*this = *this + other; // Use the addition operator
return *this; // Return the current object
}
Polynomial& Polynomial::operator-=(const Polynomial& other) {
*this = *this - other; // Use the subtraction operator
return *this; // Return the current object
}
Polynomial& Polynomial::operator*=(const Polynomial& other) {
*this = *this * other; // Use the multiplication operator
return *this; // Return the current object
}
// Friend function for output
ostream& operator<<(ostream& os, const Polynomial& poly) {
if (poly.terms.empty()) {
return os << "0"; // If no terms, output "0"
}
bool first = true; // Flag to handle formatting
for (const auto& term : poly.terms) {
int coef = term.first;
int exp = term.second;
// Format output
if (first) {
os << (coef < 0 ? "-" : "") << abs(coef); // First term without leading sign
first = false;
} else {
os << (coef >= 0 ? " + " : " - ") << abs(coef); // Subsequent terms
}
if (exp != 0) {
os << "x^" << exp; // Output the exponent
}
}
return os; // Return the output stream
}