Use separate compilation to implement a polynomial ADT that manipulates polynomi
ID: 3682951 • Letter: U
Question
Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x5 + 7 x 3 – x2 + 9 ). For this problem, consider only polynomials whose exponents are nonnegative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions:
One default constructor, which creates a polynomial with no terms.
One method allowing one to get an entire polynomial by interacting with the user (by writing to cout and reading from cin) to obtain the degree and coefficient of each term in a polynomial. If the user enters nonsense, your program should not crash! It is up to you how to handle this case. The method can look something like this when called:
myPolynomial.readFromUser(); // prompts the user for the degree and
// coefficients and stores the
// results in myPolynomial
degree() // Returns the degree of a polynomial, which is the highest power of a term with a nonzero coefficient. E.g.: int deg = myPolynomial.degree();
coefficient(power) // Returns the coefficient of the x p o w e r term.
changeCoefficient(newCoefficient, power) // Replaces the coefficient of the x p o w e r term with newCoefficient.
A method that multiplies a polynomial by an integer (make this a normal named method - do not overload the multiplication operator).
A method that adds two polynomials (you can overload the addition operator or make this a normal named method).
A method that prints out a polynomial.
Overload the negation operator (-) as a member function to negate a polynomial. (NOTE: This is different from the subtraction operator! The subtraction operator takes an argument - the polynomial you are subtracting - whereas the negation operator takes no arguments.)
For this assignment, you should have a separate .h and .cpp file (like polynomial.h and polynomial.cpp), as well as another .cpp file with a main function from which you actually use your polynomial class. Your extra .cpp file should make use of all the things detailed above!
Explanation / Answer
polynomial.h:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <string>
using namespace std;
class polynomial{
private:
//Creates the private variables used for the polynomial
int coefficient[20];
string variable[20];
int power[20];
public:
//Constructor
polynomial();
//Functions for the ADT
//reads user polynomial
void readpolynomial(int &terms);
//prints highest degree in polynomial
int degree();
//gets the coefficient of a certain power
int getcoefficient(int degree);
//changes the coefficient of the degree provided
void changeCoefficient(int newCoefficient, int degree);
//multiplies by scalar variable
void multiplys(int terms, int value);
//adds two polynomials
void add(int &terms);
//prints out the polynomial
void printpolynomial(int termstoprint);
//Overload operators
//divides polynomial by scalar variable
polynomial operator/(int value);
//negates polynomial
polynomial operator-(int terms);
};
#endif /* POLYNOMIAL_H */
polynomial.cpp:
#include <ostream>
#include <string>
#include "polynomial.h"
using namespace std;
//Constructor that makes the polynomial the default values of 0!^1
polynomial::polynomial(){
for (int i = 0; i < 20; i++){
coefficient[i] = 0;
variable[i] = "!";
power[i] = 1;
}
}
//Reads a polynomial from the user
void polynomial::readpolynomial(int &terms){
//Resizes polynomial to user size
for (int i = 0; i < terms; i++){
coefficient[i] = 0;
variable[i] = "!";
power[i] = 1;
}
//User inputs polynomial
cout << "Enter the coefficient, variable and power of your terms. " << endl;
int i = 0;
while(i < terms){
if ((i + 1) == 1){
cout << "---Input your " << (i + 1) << "st polynomial---"<<endl;
cout<< "coefficient (If none input 0): ";
cin >> coefficient[i];
cout<< "variable (If none input !): ";
cin >> variable[i];
cout<< "power (If none input 1): ";
cin >> power[i];
}
else if ((i + 1) == 2){
cout << "---Input your " << (i + 1) << "nd polynomial---"<<endl;
cout<< "coefficient (If none input 0): ";
cin >> coefficient[i];
cout<< "variable (If none input !): ";
cin >> variable[i];
cout<< "power (If none input 1): ";
cin >> power[i];
}
else if ((i + 1) == 3){
cout << "---Input your " << (i + 1) << "rd polynomial---"<<endl;
cout<< "coefficient (If none input 0): ";
cin >> coefficient[i];
cout<< "variable (If none input !): ";
cin >> variable[i];
cout<< "power (If none input 1): ";
cin >> power[i];
}
else if ((i + 1) > 3){
cout << "---Input your " << (i + 1) << "th polynomial---"<<endl;
cout<< "coefficient (If none input 0): ";
cin >> coefficient[i];
cout<< "variable (If none input !): ";
cin >> variable[i];
cout<< "power (If none input 1): ";
cin >> power[i];
}
i++;
}
//Checks for like terms
int tempc[terms];
string tempv[terms];
int tempp[terms];
for (int i = 0; i < terms; i++){
tempc[i] = 0;
tempv[i] = "!";
tempp[i] = 1;
}
for (int i = 0; i < terms; i++){
tempc[i] = coefficient[i];
tempv[i] = variable[i];
tempp[i] = power[i];
}
for (int i = 0; i < terms; i++){
if (tempc[i] == 0)
tempc[i] = 1;
}
//keeps track of where the polynomial is
int track = 0;
//for loop to goes through each element checking for like terms
for (int i = 0; i < terms; i++){
for (int k = i+1; k < terms;k++){
if (tempc[i] != 1 || tempv[i] != "!" || tempp[i] != 1){
if (tempp[i] == tempp[k] && tempv[i] == tempv[k]){
coefficient[track] = tempc[i] +tempc[k];
variable[track] = tempv[i];
power[track] = tempp[i];
tempc[k] = 0;
tempv[k] = "!";
tempp[k] = 1;
track++;
tempc[i] = 1;
tempv[i] = "!";
tempp[i] = 1;
}
}
}
}
for (int i = 0; i < terms; i++){
if (tempc[i] == 1)
tempc[i] = 0;
}
for (int i = 0; i < terms; i++){
if (tempc[i] != 0 || tempv[i] != "!" || tempp[i] != 1){
coefficient[track] = tempc[i];
variable[track] = tempv[i];
power[track] = tempp[i];
track++;
}
}
terms = track;
//Checks for like terms one more time just in case
for (int i = 0; i < terms; i++){
tempc[i] = coefficient[i];
tempv[i] = variable[i];
tempp[i] = power[i];
}
for (int i = 0; i < terms; i++){
if (tempc[i] == 0)
tempc[i] = 1;
}
//keeps track of where the polynomial is
track = 0;
//for loop to goes through each element checking for like terms
for (int i = 0; i < terms; i++){
for (int k = i+1; k < terms;k++){
if (tempc[i] != 1 || tempv[i] != "!" || tempp[i] != 1){
if (tempp[i] == tempp[k] && tempv[i] == tempv[k]){
coefficient[track] = tempc[i] +tempc[k];
variable[track] = tempv[i];
power[track] = tempp[i];
tempc[k] = 0;
tempv[k] = "!";
tempp[k] = 1;
track++;
tempc[i] = 1;
tempv[i] = "!";
tempp[i] = 1;
}
}
}
}
for (int i = 0; i < terms; i++){
if (tempc[i] == 1)
tempc[i] = 0;
}
for (int i = 0; i < terms; i++){
if (tempc[i] != 0 || tempv[i] != "!" || tempp[i] != 1){
coefficient[track] = tempc[i];
variable[track] = tempv[i];
power[track] = tempp[i];
track++;
}
}
terms = track;
}
int polynomial::degree(){
//variables for the for loops and a min variable as well
int temp[20];
for (int e = 0; e < 20; e++) {
temp[e] = power[e];
}
int i,j;
int min;
//This for loop is to keep track of elements being compared
for (j = 0; j < 20-1; j++) {
min = j;
for ( i = j+1; i < 20; i++) {
if (temp[i] < temp[min]) {
min = i;
}
}
//If statement to swap which int is the min
if(min != j) {
swap(temp[j], temp[min]);
}
}
//returns the highest degree
return temp[19];
}
//Gets coefficient of a certain degree
int polynomial::getcoefficient(int degree){
for (int i = 0; i < 20; i++){
if(power[i] == degree ){
return coefficient[i];
}
}
return 0;
}
//Changes coefficient of certain degree
void polynomial::changeCoefficient(int newCoefficient, int degree){
int noMatch = 0;
for (int i = 0; i < 20; i++){
if(power[i] == degree ){
coefficient[i] = newCoefficient;
noMatch = 1;
}
}
if (noMatch == 0){
cout << "There is no term with that degree so no changes were made!" <<endl;
}
}
void polynomial::multiplys(int terms, int value){
//Gives error if no first polynomial is read
if(coefficient[0] == 0 && variable[0] == "!" && power[0] == 1){
cout << "You must first enter a polynomial"<<endl;
}
else{
//Multiplies all terms by scalar variable
for (int i = 0; i < terms; i++){
coefficient[i] = coefficient[i] * value;
}
}
}
void polynomial::add(int &terms){
//Gives error if no first polynomial is read
if(coefficient[0] == 0 && variable[0] == "!" && power[0] == 1){
cout << "You must first enter your first polynomial"<<endl;
}
else{
//Makes second polynomial
int terms2;
cout << "How many terms are you going to input into the second polynomial?: ";
cin >> terms2;
int coefficients[terms2];
string variables[terms2];
int powers[terms2];
for (int i = 0; i < terms2; i++){
coefficients[i] = 0;
variables[i] = "!";
powers[i] = 1;
}
//Ask user to enter there terms for second polynomial
cout << "Enter the coefficient, variable and power of your terms. " << endl;
int k = 0;
while(k < terms2){
if ((k + 1) == 1){
cout << "---Input your " << (k + 1) << "st polynomial---"<<endl;
cout<< "coefficient (If none input 0): ";
cin >> coefficients[k];
cout<< "variable (If none input !): ";
cin >> variables[k];
cout<< "power (If none input 1): ";
cin >> powers[k];
}
else if ((k + 1) == 2){
cout << "---Input your " << (k + 1) << "nd polynomial---"<<endl;
cout<< "coefficient (If none input 0): ";
cin >> coefficients[k];
cout<< "variable (If none input !): ";
cin >> variables[k];
cout<< "power (If none input 1): ";
cin >> powers[k];
}
else if ((k + 1) == 3){
cout << "---Input your " << (k + 1) << "rd polynomial---"<<endl;
cout<< "coefficient (If none input 0): ";
cin >> coefficients[k];
cout<< "variable (If none input !): ";
cin >> variables[k];
cout<< "power (If none input 1): ";
cin >> powers[k];
}
else if ((k + 1) > 3){
cout << "---Input your " << (k + 1) << "th polynomial---"<<endl;
cout<< "coefficient (If none input 0): ";
cin >> coefficients[k];
cout<< "variable (If none input !): ";
cin >> variables[k];
cout<< "power (If none input 1): ";
cin >> powers[k];
}
k++;
}
int track = 0;
int temp[20];
//Make a temporary list to hold the sum values
int addt = terms + terms2;
int tempc[addt];
string tempv[addt];
int tempp[addt];
for (int i = 0; i < addt; i++){
tempc[i] = 0;
tempv[i] = "!";
tempp[i] = 1;
}
//Copy the coefficients we are going to add so they wont be modified in original polynomial
for (int i = 0; i < terms; i++){
tempc[i] = coefficient[i];
tempv[i] = variable[i];
tempp[i] = power[i];
}
int offset = terms;
int zero = 0;
for (int i = 0; i < terms2; i++){
tempc[offset] = coefficients[zero];
tempv[offset] = variables[zero];
tempp[offset] = powers[zero];
zero++;
offset++;
}
for (int i = 0; i < addt; i++){
if (tempc[i] == 0)
tempc[i] = 1;
}
//keeps track of where the polynomial is
track = 0;
//for loop to add each polynomial respectively
for (int i = 0; i < addt; i++){
for (int k = i+1; k < addt;k++){
if (tempc[i] != 1 || tempv[i] != "!" || tempp[i] != 1){
if (tempp[i] == tempp[k] && tempv[i] == tempv[k]){
coefficient[track] = tempc[i] +tempc[k];
variable[track] = tempv[i];
power[track] = tempp[i];
tempc[k] = 1;
tempv[k] = "!";
tempp[k] = 1;
track++;
tempc[i] = 1;
tempv[i] = "!";
tempp[i] = 1;
}
}
}
}
for (int i = 0; i < addt; i++){
if (tempc[i] == 1 )
tempc[i] = 0;
}
for (int i = 0; i < addt; i++){
if (tempc[i] != 0 || tempv[i] != "!" || tempp[i] != 1){
coefficient[track] = tempc[i];
variable[track] = tempv[i];
power[track] = tempp[i];
track++;
}
}
//Updates the terms value to the actual terms we have now
terms = track;
}
}
void polynomial::printpolynomial(int termtoprint)
{
//Copies the coefficients in the polynomial to a temp list so we can print respectively to there sign
int temp[20];
for(int k = 0; k < termtoprint; k++ ){
temp[k] = coefficient[k];
}
//Here we begin to print the polynomial
for(int i = 0; i < termtoprint; i++ ){
if(temp[i] != 0 ){
cout <<temp[i];
}
if(variable[i] != "!"){
cout << variable[i];
}
if (power[i] != 1){
cout << "^" <<power[i];
}
//After a term it either prints + or - depended on the next term
if(i < termtoprint-1){
if(temp[i + 1] >= 0){
cout << " + ";
}
else if(temp[i + 1] < 0){
cout << " - ";
temp[i + 1] = temp[i + 1] * -1;
}
}
}
//If theres no polynomial to display it will let the user know
if(coefficient[0] == 0 && variable[0] == "!" && power[0] == 1){
cout << "No polynomial to display"<<endl;
}
}
polynomial polynomial::operator /(int value)
{
//Prints out error if there no polynomial to dive by
if(coefficient[0] == 0 && variable[0] == "!" && power[0] == 1){
cout << "You must first enter your first polynomial"<<endl;
return *this;
}
else{
//If there is a polynomial it divides he coefficients by the value user inputed
for (int i = 0; i < 20; i++){
coefficient[i] = coefficient[i] / value;
}
return *this;
}
}
polynomial polynomial::operator -(int terms)
{
//Prints out error if there no polynomial to dive by
if(coefficient[0] == 0 && variable[0] == "!" && power[0] == 1)
{
cout << "You must first enter your first polynomial"<<endl;
return *this;
}
else{
//Negates the polynomial by multiplying the coefficients of each term by -1
for (int i = 0; i < terms; i++)
{
coefficient[i] = coefficient[i] * -1;
}
return *this;
}
}
polynomial_test.cpp:
#include <iostream>
#include <string>
#include "polynomial.h"
using namespace std;
int main() {
//Declaring variable the user needs to input to get program going
int choice = 1;
polynomial poly;
int terms;
//Ask user what they want to do
while(choice != 0){
cout <<" Choose a command ------------------------------------------------------------------------------------------------------------------------------------------- "
" 1. Input a polynomial 2. Get degree 3. Display coefficient of a term 4. Change coefficient of a term 5. Multiply by scalar variable "
" 6. Add polynomials 7. Print out polynomial 8. Divide Polynomial 9. Negate polynomial 10. Output polynomial to a output stream "
" 0. To Exit Command: ";
cin >> choice;
//Checks for errors if a command is invalid
while (choice < 0 || choice > 10){
cout << "ERROR! Please choose a command between 0 - 10" <<endl;
cout <<" Choose a command ------------------------------------------------------------------------------------------------------------------------------------------- "
" 1. Input a polynomial 2. Get degree 3. Display coefficient of a term 4. Change coefficient of a term 5. Multiply by scalar variable "
" 6. Add polynomials 7. Print out polynomial 8. Divide Polynomial 9. Negate polynomial 10. Output polynomial to a output stream "
" 0. To Exit Command: ";
cin >> choice;
}
//Here we go to our designated function via command
if (choice == 1){
//Ask user how many terms he will input into the polynomial
cout << "How many terms are you going to input?: ";
cin >> terms;
poly.readpolynomial(terms);
}
else if(choice == 2){
//Gets highest degree in polynomial
cout << " Highest degree is " << poly.degree();
}
else if (choice == 3){
//This function get the coefficient given the users power
int degree;
cout << " Of what degree is the coefficient you want to see?: ";
cin >> degree;
while(degree == 0 || degree == 1){
cout <<"Invalid power!" << endl;
cout << " Of what degree is the coefficient you want to see?: ";
cin >> degree;
}
if(poly.getcoefficient(degree)== 0){
cout << "That degree doesnt have a coefficient or is not in this polynomial"<<endl;
}
else
cout << " The coefficient of that power is " << poly.getcoefficient(degree);
}
else if(choice == 4){
//Changes coefficient of a certain degree
int newCoefficient;
int power;
cout << " What degree does the coefficient you want to change have?: ";
cin >> power;
while(power == 0 || power == 1){
cout <<"Invalid power!" << endl;
cout << " What degree does the coefficient you want to change have?: ";
cin >> power;
}
cout << " What number would you like to replace the coefficient with?: ";
cin >> newCoefficient;
poly.changeCoefficient(newCoefficient, power);
}
else if(choice == 5){
//This function multiplies polynomial by a scalar variable
int value;
cout << "By what value would you like to multiply by?: ";
cin >> value;
poly.multiplys(terms, value);
}
else if (choice == 6){
//Function adds two polynomials
poly.add(terms);
}
else if(choice == 7){
//Function prints out the polynomial
poly.printpolynomial(terms);
}
else if(choice == 8){
//Divides polynomial by scalar variable
int value;
cout << "By what value would you like to divide by?: ";
cin >> value;
poly.operator /(value);
}
else if(choice == 9){
//Negates polynomial
poly.operator-(terms);
}
else if(choice == 10){
//Function prints out the polynomial
poly.printpolynomial(terms);
}
}
//Lets user know that the program has been ended
cout << "-------------------------- Program Terminated Goodbye!";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.