Hi, I really need help with the below problem, I am really stuck. Based on the s
ID: 3871848 • Letter: H
Question
Hi,
I really need help with the below problem, I am really stuck.
Based on the sample output provided, and the polynomial.h and task.cpp files shown below, implement the required member functions and nonmember function in your polynomial.cpp file.
Write a class Polynomial that stores a polynomial such as
y = 5x10 + 9x7 - x – 10
as a linked list of terms. A term contains the coefficient and the power of x. For example, you
would store p(x) as
(5,10), (9,7), (-1,1), (-10,0)
We allow decimal numbers such as 4.5 and 6.78 etc to be used as coefficients.
Function get_pts(xStart, xEnd, xStepSize):
The member function get_pts() calculates and returns a queue of x, y coordinate pairs. The y coordinate is calculated from the Polynomial’s equation based on the passed parameters of:
xStart to xEnd with an increment of xStepSize.
Function plot(coords):
The nonmember function plot() provides a formatted printout of the x, y coordinates returned by the get_pts() function. See sample output provided.
polynomial.h
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include
#include
#include
using namespace std;
class Polynomial
{
public:
Polynomial();
Polynomial(pair);
void add(Polynomial p);
void add_term(pair t);
queue< pair > get_pts(double, double, double);
void print();
private:
list< pair > terms;
};
void plot(queue< pair >);
#endif
task.cpp
#include
#include
#include
#include
#include
#include "polynomial.h"
int main()
{
queue< pair > pts; // Coords
// 2x^3 - 3x^2 -3x+2
Polynomial p(pair(2, 3));
p.add(Polynomial(pair(-3, 2)));
p.add(Polynomial(pair(-3, 1)));
p.add(Polynomial(pair(2, 0)));
// Print the Polynomial
p.print();
// Calculate x,y coordinates for the Polynomial
// from xStart to xEnd with xStepSize
pts = p.get_pts(-1.5, 2.5, 0.5);
// Print x, y coords
plot(pts);
return 0;
}
Sample output:
+ 2 -3*x -3*x^2 + 2*x^3
x y
-1.50 -7.00
-1.00 0.00
-0.50 2.50
0.00 2.00
0.50 0.00
1.00 -2.00
1.50 -2.50
2.00 0.00
2.50 7.00
Explanation / Answer
# include <polynomial.h>
# include <task.cpp>
# include <polynomial.cpp>
using namespace std;
class Leftnode{
public:
Leftnode();
Leftnode(int, double);
double coefficients;
int exponent;
Leftnode *next;
};
Leftnode::Leftnode(){
coefficient = 0.0;
exponent = 0;
next = NULL;
}
Leftnode::Leftnode(int exp, double coef){
coefficients = coef;
exponent = exp;
next = NULL;
}
class polynomial{
public:
polynomial();
polynomial(int);
void setCoef(int exp, double coef);
void print();
private:
int size;
Leftnode *head, *tail;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.