Note: The contents in Yin renkun's c ++ data structure must be completely copied. However, they cannot be correctly calculated or output, and must be improved ......
==========================================
# Include <iostream>
# Include <math. h>
Using namespace STD;
Struct term
{
Float ceof; // Coefficient
Int exp; // Exponent
Term * link;
Term (float C, int e, term * Next = NULL)
{
Ceof = C;
Exp = E;
Link = next;
}
Term * insertafter (float C, int e );
Friend ostream & operator <(ostream &, const term &);
};
Term * Term: insertafter (float C, int e)
{
// Insert a new item after the item currently pointed by the this pointer
Link = new term (C, E, link );
Return link;
}
Ostream & operator <(ostream & out, const term & X)
{
// Output an item to the output stream out.
If (X. ceof = 0.0)
Return out;
Out <X. ceof; // output coefficient
Switch (X. exp) // output index
{
Case 0:
Break;
Case 1:
Out <"X ";
Break;
Default:
Out <"x ^" <X. Exp;
Break;
}
Return out;
}
Class Polynomial
{
Public:
Polynomial ()
{
First = new term (0,-1); // create an empty linked list
}
Polynomial (Polynomial & R );
Int maxorder (); // calculates the maximum order.
Term * gethead () const // obtain the header pointer of a polynomial single-chain table
{
Return first;
}
PRIVATE:
Term * first;
Friend ostream & operator <(ostream & out, Polynomial & X );
Friend istream & operator> (istream & in, Polynomial & X );
Friend polynomial operator + (Polynomial & A, Polynomial & B );
Friend polynomial operator * (Polynomial & A, Polynomial & B );
};
Ostream & operator <(ostream & out, Polynomial & X)
{
// Output the linked list X with a polynomial with an additional head node
Term * Current = x. gethead ()-> link;
Cout <"the polynomial is:" <Endl;
Bool H = true;
While (current! = NULL)
{
If (H = false & Current-> ceof> 0.0)
Out <"+ ";
H = false;
Out <* Current; // call the overload operation of the term class "<"
Current = Current-> link;
}
Out <Endl;
Return out;
}
Polynomial: polynomial (Polynomial & R)
{
// Copy the constructor and use an existing polynomial object to initialize the current polynomial object.
First = new term (0,-1 );
Term * destptr = first;
Term * srcptr = R. gethead ()-> link;
While (srcptr! = NULL)
{
Destptr-> insertafter (srcptr-> ceof, srcptr-> exp );
// Insert a new node after the node indicated by destptr, and then point destptr to the new node.
Srcptr = srcptr-> link;
Destptr = destptr-> link;
}
}
Int polynomial: maxorder ()
{
// When the polynomials are arranged in ascending order, the last item is the largest index.
Term * Current = first;
While (current-> link! = NULL)
Current = Current-> link; // when the table is empty, current stays at first; otherwise, it stops at the end of the table.
Return Current-> exp;
}
Istream & operator> (istream & in, Polynomial & X)
{
// Input items from the input stream in, and create a polynomial using the ending Interpolation Method
Term * rear = x. gethead (); // rear is the end pointer
Int C, E;
While (1)
{
Cout <"input a term (coef, exp):" <Endl;
In> C> E;
If (E <0)
Break; // use e <0 to control the end of input
Rear = rear-> insertafter (C, E); // link to the node indicated by rear
}
Return in;
}
//************************************
// Return: Polynomial
// Parameter: The headers of two polynomial linked lists with additional head nodes listed in ascending power are A. First and B. First, respectively.
//************************************
Polynomial operator + (Polynomial & A, Polynomial & B)
{
Term * pA, * pb, * PC, * P;
Float temp;
Polynomial C;
PC = C. First; // PC indicates the end pointer of the result polynomial r during creation.
// Pa and Pb are located at the first node of A and B respectively, and are the detection pointers of two chains.
Pa = A. gethead ()-> link;
PB = B. gethead ()-> link;
While (Pa! = NULL & Pb! = NULL)
{
If (Pa-> exp = Pb-> exp) // The coefficient is equal.
{
Temp = pa-> ceof + Pb-> ceof;
If (FABS (temp)> 0.001) // The coefficient after addition is not zero.
PC = pc-> insertafter (temp, pa-> exp );
Pa = pa-> link;
PB = Pb-> link;
}
Else if (Pa-> exp <Pb-> exp)
{
PC = pc-> insertafter (Pa-> ceof, pa-> exp );
Pa = pa-> link;
}
Else
{
PC = pc-> insertafter (Pa-> ceof, pa-> exp );
PB = Pb-> link;
}
}
If (Pa! = NULL)
P = PA; // P points to the object of the remaining chain
Else
P = Pb;
While (P! = NULL) // process the remaining link
{
PC = pc-> insertafter (p-> ceof, p-> exp );
P = p-> link;
}
Return C;
}
//************************************
// Return: returns the first pointer to a single-chain table that stores the product polynomials.
// Parameter: multiply the polynomial A and polynomial B.
//************************************
Polynomial operator * (Polynomial & A, Polynomial & B)
{
Term * pA, * pb, * PC;
Int Al, BL, I, K, maxexp;
Polynomial C;
PC = C. gethead (); // The end pointer of the result polynomial during creation
// The order of two Polynomials
Al = A. maxorder ();
BL = B. maxorder ();
If (Al! =-1 & BL! =-1) // There is no zero polynomial to perform the multiplication operation
{
Maxexp = Al + BL; // the highest order of the result Polynomial
Float * result = new float [maxexp + 1];
For (I = 0; I <= maxexp; I ++)
Result [I] = 0.0;
Pa = A. gethead ()-> link; // detection pointer of Polynomial
While (Pa! = NULL)
{
PB = B. gethead ()-> link; // test pointer of polynomial B
While (PB! = NULL)
{
K = pa-> exp + Pb-> exp; // exponent of the Product
Result [k] = Result [k] + pa-> ceof * pb-> ceof;
PB = Pb-> link;
}
Pa = pa-> link;
}
// Generate the calculation result in result []
For (I = 0; I <= maxexp; I ++)
If (FABS (result [I])> 0.001) // This item can be inserted into the result polynomial linked list.
PC = pc-> insertafter (result [I], I );
Delete [] result;
}
PC-> link = NULL;
Return C;
}
Int main ()
{
Polynomial A, B, C;
Cout <"input polynomial A:" <Endl;
Cin>;
Cout <"input polynomial B:" <Endl;
Cin> B;
C = A + B;
Cout <"A + B =" <C <Endl;
C = A * B;
Cout <"a * B =" <C <Endl;
Return 1;
}