//this is the header file
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <cstdlib>
#include <iostream>
usingnamespace std;
constint MAX_TERMS = 10;
class polynomial{
public:
//Default constructor
polynomial();
//gets the user-input for polynomial
void getData();
//constructor with user-input parameters
explicit polynomial(int& x);
//accessor for the degree
int degree() const;
//returns the coefficient of the x-power term
int coefficient(int& power) const;
//replaces the coefficient of the x-power term
void changeCoefficient(int& newCoefficient, int& power);
//multiplies a polynomial by a scalar variable
polynomial scalarMulitply(int& scalar);
//adds two polynomials
void addPolynomials(polynomial& P1, polynomial& P2);
//prints out polynomial
void printPolynomial() const;
//overloading operator to divide a polynomial by a scalar variable
polynomial operator/(int& scalar);
//overloading operator to negate a polynomial
polynomial operator-();
//overloading operator to output a polynomial on an output stream
friend ostream& operator<<(ostream& outStream, polynomial& P1);
//struct for polynomial terms
struct poly{
int coefficient;
int power;
};
private:
int num_terms;
int new_terms;
poly p[MAX_TERMS];
};
#endif /* POLYNOMIAL_H */
************************************************************
//This is the .cpp file
#include "polynomial.h"
//#include "polynomial.h"
#include <iostream>
#include <string>
usingnamespace std;
//Default constructor
polynomial::polynomial() {
num_terms = 1;
int i = 0;
for (i = 0; i < num_terms; i++) {
p[i].coefficient = 0;
p[i].power = 0;
}
}
//get data from user
void polynomial::getData() {
int i =0;
for(i=0; i<num_terms; i++){
cout <<" Enter the coefficient (Please include '+' or '-' before integer)";
cin >> p[i].coefficient;
cout <<" Enter the power";
cin >> p[i].power;
}
}
//Polynomial constructor with user-input parameters
polynomial::polynomial(int& x) {
num_terms = x;
int i = 0;
for (i = 0; i < num_terms; i++) {
p[i].coefficient = 0;
p[i].power = 0;
}
}
//returns degree of a polynomial
int polynomial::degree() const {
int degree = 0;
int i = 0;
for (i = 0; i < num_terms; i++) {
if (degree <= p[i].power) {
degree = p[i].power;
}
}
}
//returns the coefficient of the x-power term
int polynomial::coefficient(int& power) const {
int i = 0;
for (i = 0; i < num_terms; i++) {
if (p[i].power == power) {
return p[i].coefficient;
}
}
}
//replaces the coefficient of the x=power term with newCoefficient
void polynomial::changeCoefficient(int& newCoefficient, int& power) {
int i = 0;
for (i = 0; i < num_terms; i++) {
if (p[i].power == power) {
p[i].coefficient = newCoefficient;
}
}
}
//Multiplies a polynomial by a scalar variable
polynomial polynomial::scalarMulitply(int& scalar) {
polynomial p1(num_terms);
for (int i = 0; i < num_terms; i++) {
p1.p[i].coefficient = p[i].coefficient * scalar;
p1.p[i].power = p[i].power;
}
return p1;
}
//Adds two polynomials
void polynomial::addPolynomials(polynomial& a1, polynomial& a2) {
int term = 0;
int i = 0;
int j = 0;
bool found = false;
//loops through the first polynomial comparing terms from second poylynomial
for (i = 0; i < a1.num_terms; i++) {
for (j = 0; j < a2.num_terms; i++) {
if (a1.p[i].power == a2.p[i].power) {
p[term].coefficient = a1.p[i].coefficient + a2.p[j]coefficient;
p[term].power = a1.p[i].power;
term++;
found = true;
} else {
found = false;
}
}
}
//iterate through second polynomial comparing terms from first polynomial
for (i = 0; i < a2.num_terms; i++) {
for (j = 0; j < a2.num_terms; i++) {
if (a2.p[i].power == a1.p[j].power) {
found = true;
break;
} else {
found = false;
}
}
if (!found) {
p[term].coefficient = a2.p[i].coefficient;
p[term].power = a2.p[i].power;
term++;
}
found = true;
}
}
// prints out a polynomial
void polynomial::printPolynomial() const {
int i = 0;
cout << " Your Polynomial: ";
for(i=0; i< num_terms; i++){
cout << p[i].coefficient << p[i].power << endl;
}
}
// Overloading operator to divide a polynomial by a scalar variable
polynomial polynomial::operator/(int& scalar) {
int i = 0;
polynomial p1(num_terms);
if (scalar == 0) {
cout << "Scalar cannot be 0";
} else {
for (i = 0; i < 10; i++) {
p1.p[i].coefficient = p[i].coefficient / scalar;
p1.p[i].power = p[i].power;
}
}
return p1;
}
//Overloading operator to negate a polynomial
polynomial polynomial::operator-() {
int i = 0;
for (i = 0; i < num_terms; i++) {
p[i].coefficient *= -coefficient;
}
return *this;
}
ostream &operator<<(ostream& outStream, polynomial& p1) {
int i = 0;
for (i = 0; i < p1.num_terms; i++) {
if ((p1.p[i].coefficient > 0) && (i == 0)) {
outStream << p1.p[i].coefficient << "x^" << p1.p[i].power << " ";
} elseif ((p1.p[i].coefficient > 0) && (i > 0)) {
outStream << "+ " << p1.p[i].coefficient << "x^" << p1.p[i].power << " ";
} elseif ((p1.p[i].coefficient < 0) && (i == 0)) {
outStream << p1.p[i].coefficient << "x^" << p1.p[i].power << " ";
} elseif ((p1.p[i].coefficient < 0) && (i > 0)) {
outStream << "+ " << p1.p[i].coefficient << "x^" << p1.p[i].power << " ";
}
}
outStream << endl << endl;
return outStream;
}
#include "ADT.h" // Not a problem since you have header guards but why the header is including itself?
#include "ADT.cpp" // You should not include .cpp files.