In this homework assignment, you will practice working with dynamic memory, comp
ID: 3598535 • Letter: I
Question
In this homework assignment, you will practice working with dynamic memory, composition, and container classes by implementing a menu-based polynomial "management" system. Each polynomial will be represented as a list of "terms" where each term object contains a floating- point coefficient and a nonnegative exponent. Within the list, the terms should be maintained/presented in descending order based on exponent. When your program starts, you should ask the user how many polynomials he/she wants to maintain in the system. Then, you should dynamically allocate an array of polynomials. Your program should then display all the polynomials in the system together with its index every time the main menu is display. The menu should support the following options 1. Add term to polynomial 2. Print polynomial 3. Evaluate polynomial at given x value 4. Delete term from polynomial 5. Delete all terms from polynomial 6. Quit For options 1-5, you should first ask the user which polynomial in the array should be modified/evaluated/displayed. After a polynomial is selected then your program should do the following depending on the option selected: 1. Add term to polynomial: This option will add a term to the given polynomial and display the resulting polynomial. The exponent and coefficient of the term will be entered by the user. If the exponent of the term to add already exists, then a new term should not be inserted into the list. Instead, the coefficient of the existing term should be modified to reflect the addition of the term. If this would result in a coefficient that is (approximately) zero, then the term should be subsequently deleted from the list. Of course, if the exponent of the term does not already exist, then a new term will need to be created/inserted in the list representation of the polynomial. 2. Print polynomial: This option will print the polynomial to the screen in a format similar to the following: 2.5 x100 + 3.2x*21.2x+3 Note that a term with an exponent of 1 and a term with an exponent of 0 are special cases. 3. Evaluate polynomial at given x value: This option will print the result of evaluating the given polynomial at a given x value. The value ofx to be evaluated will be entered by the user. You will likely want to use the pow function to help evaluate the polynomial. 4. Delete term from polynomial: This option will delete one term from the polynomial, printing the resulting polynomial. The term to delete will be entered by the user (based on exponent) 5. Delete all terms from polynomial: This option will delete all terms from the polynomial, printing the resulting (empty) polynomial to demonstrate that all terms have been removed. 6. Quit: This option will cause the array of polynomials to be deallocated and the program to exit.Explanation / Answer
Given below is the code with output. Please do rate the answer if it helped. Thank you.
compile: g++ polynomial.cpp main.cpp
polynomial.h
#ifndef polynomial_h
#define polynomial_h
struct term
{
double coefficient;
unsigned int exponent;
struct term* next;
};
class polynomial
{
private:
struct term* headPtr;
public:
polynomial();
void addTerm(double coefficient, unsigned int exponent); //stores terms in decreasing order of exponent
void print();
bool deleteTerm(unsigned int exponent);
void deleteAll();
double evaluate(double x);
};
#endif /* polynomial_h */
polynomial.cpp
#include "polynomial.h"
#include <cmath>
#include <iostream>
using namespace std;
polynomial::polynomial()
{
headPtr = nullptr;
}
//adds terms in sorted decreasing order of exponent. If a term with same exponent exists, then the coefficients are added together
void polynomial::addTerm(double coefficient, unsigned int exponent)
{
if(coefficient == 0) //no needed to add a term with 0 coefficient
return;
struct term* prev = nullptr;
struct term* curr = headPtr;
//find a place where to add the new term if such an exponent is not already present. If exponent is present, then add the coefficients
while(curr != nullptr )
{
if(exponent > curr->exponent)
break;
else if(curr->exponent == exponent) //already a term with given exponent exists, add the coefficient
{
curr->coefficient += coefficient;
if(curr->coefficient == 0) //if the coefficient is 0, remove it from the polynomial list
deleteTerm(curr->exponent);
return;
}
else
{
prev = curr;
curr = curr->next;
}
}
//exponent not found, add a new term
struct term* t = new term;
t->coefficient = coefficient;
t->exponent = exponent;
t->next = nullptr;
if(prev == nullptr) //adding as 1st term
{
t->next = headPtr;
headPtr = t;
}
else
{
prev->next = t;
t->next = curr;
}
}
void polynomial::print()
{
struct term* curr = headPtr;
if(headPtr == nullptr)
cout << "0";
else
{
//display 1st term
cout << curr->coefficient << " x^" << curr->exponent ;
curr = curr->next;
while(curr != 0)
{
if(curr->coefficient > 0)
cout << " + " << curr->coefficient << " x^" << curr->exponent ;
else
cout << " - " << -(curr->coefficient) << " x^" << curr->exponent ;
curr = curr->next;
}
}
cout << endl;
}
bool polynomial::deleteTerm(unsigned int exponent)
{
struct term* curr = headPtr;
struct term* prev = nullptr;
while(curr != nullptr)
{
if(curr->exponent == exponent)
break;
prev = curr;
curr = curr->next;
}
if(curr == nullptr) //did not find a matching term
return false;
if(curr == headPtr) //deleting head term
headPtr = curr->next;
else
prev->next = curr->next;
delete curr;
return true;
}
void polynomial::deleteAll()
{
struct term* curr = headPtr;
struct term* next = nullptr;
while(curr != nullptr)
{
next = curr->next;
delete curr;
curr = next;
}
headPtr = nullptr;
}
double polynomial::evaluate(double x)
{
double value = 0;
struct term* curr = headPtr;
while(curr != nullptr)
{
value += curr->coefficient * pow(x , curr->exponent);
curr = curr->next;
}
return value;
}
main.cpp
#include "polynomial.h"
#include <iostream>
using namespace std;
int main()
{
double coefficient;
unsigned int exponent;
double x, value;
polynomial poly;
int choice = 0;
while(choice != 6)
{
cout << "1. Add term to polynomial" << endl;
cout << "2. Print polynomial" << endl;
cout << "3. Evaluate polynomial" << endl;
cout << "4. Delete term from polynomial" << endl;
cout << "5. Delete all terms from polynomial" << endl;
cout << "6. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter coefficient: ";
cin >> coefficient;
cout << "Enter exponent: ";
cin >> exponent;
poly.addTerm(coefficient, exponent);
cout << "Added term to polynomial" << endl;
break;
case 2:
poly.print();
break;
case 3:
cout << "Enter the value of x: ";
cin >> x;
value = poly.evaluate(x);
cout << "Value of polynomial for x = " << x << " is " << value << endl;
break;
case 4:
cout << "Enter the exponent of the term to delete: ";
cin >> exponent;
if(poly.deleteTerm(exponent))
cout << "The term with exponent " << exponent << " deleted." << endl;
else
cout << "The term with exponent " << exponent << " NOT deleted." << endl;
break;
case 5:
poly.deleteAll();
cout << "Deleted all terms from polynomial" << endl;
break;
case 6:
break;
default:
cout << "Invalid choice! " << endl;
}
}
}
output
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 2
0
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 1
Enter coefficient: 3
Enter exponent: 2
Added term to polynomial
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 2
3 x^2
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 1
Enter coefficient: 4
Enter exponent: 5
Added term to polynomial
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 1
Enter coefficient: 2
Enter exponent: 1
Added term to polynomial
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 1
Enter coefficient: 4
Enter exponent: 0
Added term to polynomial
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 2
4 x^5 + 3 x^2 + 2 x^1 + 4 x^0
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 3
Enter the value of x: 2
Value of polynomial for x = 2 is 148
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 4
Enter the exponent of the term to delete: 3
The term with exponent 3 NOT deleted.
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 4
Enter the exponent of the term to delete: 2
The term with exponent 2 deleted.
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 2
4 x^5 + 2 x^1 + 4 x^0
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 5
Deleted all terms from polynomial
1. Add term to polynomial
2. Print polynomial
3. Evaluate polynomial
4. Delete term from polynomial
5. Delete all terms from polynomial
6. Quit
Enter your choice: 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.