// *********************************************** //
// The grab function for FLOAT_POLY class //
// *********************************************** //
istream& FLOAT_POLY::grab(istream& input)
{
FLOAT_POLY_CELL *temp,*tail;
FLOAT c,test;
char choice;
unsigned int p;
cout << "Please Enter the FLOAT element which terminates input:" << endl;
input >> test;
//Find end of list
//see if list is empty but built
if(head==NULL){
head = new FLOAT_POLY_CELL();
//now we enter the data:
cout << "Input the first element as a FLOAT: " << endl;
input >> c;
if(!(c==test)){
head->element = c;
cout << "Enter the power you want for this term: " << endl;
input >> p;
head->power = p;
}
}
//find end of list
temp = head;
while(!(temp->next==NULL))
temp = temp->next;
tail = temp;
//temp is not NULL; tail is found
//assume tail stores the last polynomial term
//so degree of polynomial is head->power
//so we add at the tail; these new terms will not be
//in the right order, so later we will have to sort
cout << "Input the next element as a FLOAT: " << endl;
input >> c;
while(!(c==test)){
temp->next = new FLOAT_POLY_CELL();
temp->next->element = c;
cout << "Enter the power you want for this term: " << endl;
input >> p;
temp->next->power = p;
temp = temp->next;
cout << "Input the next element as a FLOAT: " << endl;
input >> c;
}
//polynomial is built on top of original polynomial or as new
//polynomial by adding p+1 new elements
//a_n x^p + ... + a_0 + c_0 x^n+1 ... c_p x^n+p
//apply merge sort to sort according to power
mergesort();
return input;
}
// *********************************************** //
// The print function for FLOAT_POLY class //
// *********************************************** //
ostream& FLOAT_POLY::print(ostream& output) const
{
FLOAT_POLY_CELL *temp;
temp = head;
while(!(temp==NULL)){
output << *temp;
temp = temp->next;
}
return(output);
}
\subsection{The Constructors:}
Note we set the head pointer to NULL initially.
\small
\begin{verbatim}
// ***************************************************** //
// default constructor for FLOAT_POLY class //
// ***************************************************** //
FLOAT_POLY::FLOAT_POLY()
{
head = NULL;
}
// ****************************************** //
// explicit constructor for FLOAT_POLY class //
// ****************************************** //
FLOAT_POLY::FLOAT_POLY(FLOAT x_in, unsigned int q)
{
head = new FLOAT_POLY_CELL(x_in,q);
}