Implementation of Polynomial operations in C ++

Source: Internet
Author: User

# Include "Define. H"

Typedef struct
{
Float coef; // Coefficient
Int EXPN; // Index
} Term, elemtype;
Typedef struct lnode
{
Elemtype data;
Struct lnode * next;
} * Link;
Typedef struct
{
Link head, tail;
Int Len;
} Linklist;

Typedef linklist polynomial;

Status makenode (link & P, elemtype E)
{
// Assign a node with the value pointed to by P to E.
P = (Link) malloc (sizeof (lnode ));
If (P)
{
P-> data. coef = E. coef;
P-> data. EXPN = E. EXPN;
P-> next = NULL;
Return OK;
}
Else
Return 0;
}

Link gethead (linklist L)
{
Return L. head;
}

Status setcurelem (link & P, elemtype E)
{
// Use e to update the node specified by P
If (! P)
{
Printf ("P is null! ");
Return Error;
}
P-> data. coef = E. coef;
P-> data. EXPN = E. EXPN;
Return OK;
}

Link locateelem (linklist L, elemtype E, status (* compare) (elemtype, elemtype ))
{
// Return the first position in the linear linked list that matches the compare () determination relationship with E. If no value exists, null is returned.
Link P;
P = L. Head-> next;
While (P! = NULL &&! (* Compare) (p-> data, e ))
P = p-> next;
If (P! = NULL)
Return P;
Else
Return NULL;
}

Status initlist (linklist & L)
{
L. Head = L. Tail = (Link) malloc (sizeof (lnode ));
If (! L. Head)
Exit (-2 );
L. Len = 0; // The length is not the header node.
L. Head-> next = NULL;
Return OK;
}

Status ecqual (elemtype A, elemtype B)
{
If (A. EXPN = B. EXPN)
Return OK;
Return Error;
}

Void insfirst (link H, link S)
{
// Insert a node (header)
S-> next = H-> next;
H-> next = s;
}

Void getcurelem (link P, elemtype &)
{
// Assign the value of P to
A. coef = p-> data. coef;
A. EXPN = p-> data. EXPN;
}

Link nextpos (linklist L, link P)
{
If (! P-> next)
// Cout <"the end! "<Endl;
Return NULL;
Return p-> next;
}

Status CMP (elemtype A, elemtype B)
{
If (A. EXPN> B. EXPN)
Return 1;
Else if (A. EXPN = B. EXPN)
Return 0;
Else
Return-1;
}

Status listempty (linklist L)
{
// Null, return true
If (L. Head-> next = NULL)
Return true;
Else
Return false;
}

Status delfirst (link H, link & P)
{
// Delete the first node in the table and return it as P
If (H-> next = NULL)
{
Cout <"this is empty! "<Endl;
P = H-> next;
Return 0;
}
P = H-> next;
H-> next = p-> next;
P-> next = NULL;
Return OK;
}

Void freenode (link & P)
{
Free (P );
}

Status clearlist (linklist & L)
{
Link P, h;
H = L. head;
P = H-> next;
While (P! = NULL)
{
H = p-> next;
Freenode (P );
P = h;
}
 
If (! P)
{
L. Head-> next = NULL;
L. Tail = L. head;
L. Len = 0;
Return OK;
}
Else
Return Error;
}

Void append (linklist & L, link S)
{
// Link a string of nodes referred to by S to the end of the L table
Int I = 0;
While (L. Tail-> next)
{
L. Tail = L. Tail-> next;
}
L. Tail-> next = s;
While (L. Tail-> next)
{
L. Tail = L. Tail-> next;
I ++;
}
L. Len + = I;
}

Void destroypolyn (Polynomial & P)
{
// Destroy Polynomials
If (P. Head)
{
Freenode (P. Head );
P. Head = P. Head-> next;
}
}

Void printpolyn (polynomial P)
{
// Print
Link h;
H = gethead (P );
H = H-> next;
If (H-> data. EXPN = 0)
Printf ("% F", H-> data. coef );
Printf ("% FX ^ % d", H-> data. coef, H-> data. EXPN );
H = H-> next;
While (h)
{
If (H-> data. EXPN = 0)
Printf ("% F", H-> data. coef );
Else
{
If (H-> data. coef> 0)
Printf ("+ ");
Printf ("% FX ^ % d", H-> data. coef, H-> data. EXPN );
}
H = H-> next;
}
Printf ("/N ");
}

Int polynlength (polynomial P)
{
// Returns the number of polynomials.
Int I;
I = 0;
While (P. Head)
{
I ++;
P. Head = P. Head-> next;
}
Return I;
}

Void createpolyn (Polynomial & P, int m)
{
// Input the index and coefficient of M, and establish an ordered linked list p that represents a polynomial.
Link H, S;
Term E;
Int I;
Initlist (P );
H = gethead (P );
E. coef = 0.0;
E. EXPN =-1; // set the header Node
Setcurelem (H, e );
// Cout Cout <"Please input coef and EXPN" <"(" <m <", descending)" <":" <Endl;
For (I = 1; I <= m; I ++)
{
// CIN> E. coef> E. EXPN;
Scanf ("% F", & E. coef );
Scanf ("% d", & E. EXPN );
If (! Locateelem (p, E, ecqual ))
If (makenode (S, e ))
Insfirst (H, S );
}
}

Void addpolyn (Polynomial & PA, Polynomial & Pb, status (* CMP) (elemtype, elemtype ))
{
Link ha, Hb, QA, QB;
Elemtype A, B;
// Int T;
Float sum;
Ha = gethead (PA );
HB = gethead (PB );
QA = nextpos (Pa, ha );
QB = nextpos (Pb, Hb );
While (QA & QB)
{
Getcurelem (QA, );
Getcurelem (QB, B );
// T = (* CMP) (A, B );
Switch (* CMP) (a, B ))
{
Case-1: // the current node value in polynomial PA is small.
Ha = QA;
QA = nextpos (Pa, QA );
Break;
Case 0:
Sum = A. coef + B. coef;
If (sum! = 0)
{
QA-> data. coef = sum; // modify the Coefficient
Ha = QA;
}
Else
{
Delfirst (HA, QA );
Freenode (QA );
}
Delfirst (Hb, QB );
Freenode (QB );
QB = nextpos (Pb, Hb );
QA = nextpos (Pa, ha );
Break;
Case 1:
Delfirst (Hb, QB );
Insfirst (HA, QB );
QB = nextpos (Pb, Hb );
Ha = nextpos (Pa, ha );
Break;
}
}
If (! Listempty (PB ))
Append (Pa, QB );
Freenode (HB );
}

Void subtractpolyn (Polynomial & PA, Polynomial & Pb)
{
// Polynomial subtraction and destruction of Pb
Link ha, Hb, QA, QB;
Elemtype A, B;
Float sub;
Ha = gethead (PA );
HB = gethead (PB );
QA = nextpos (Pa, ha );
QB = nextpos (Pb, Hb );
While (QA & QB)
{
Getcurelem (QA, );
Getcurelem (QB, B );
Switch (* CMP) (a, B ))
{
Case-1:
Ha = QA;
QA = nextpos (Pa, QA );
Break;
Case 0:
Sub = A. coef-b.coef;
If (sub! = 0.0)
{
QA-> data. coef = sub;
Ha = QA;
}
Else
{
Delfirst (HA, QA );
Freenode (QA );
}
Delfirst (Hb, QB );
Freenode (QB );
QB = nextpos (Pb, Hb );
QA = nextpos (Pa, ha );
Break;

Case 1:
Delfirst (Hb, QB );
QB-> data. coef * =-1;
Insfirst (HA, QB );
QB = nextpos (Pb, Hb );
Ha = nextpos (Pa, ha );
Break;
}
}
If (! Listempty (PB ))
Append (Pa, QB );
Freenode (HB );
}

/* Void contrary (Polynomial & Q)
{
// Reverse Order
Link P, C, N;
P = NULL; // previous Node
C = Q. Head; // current node
N = Q. Head-> next; // next node
While (n) {// if it does not reach the end of the linked list
C-> next = P; // the current node in reverse order
P = C; // forward the node
C = N; // forward the node
N = N-> next; // node forward
}
C-> next = P;
Q. Head = C;
}*/

Void multiplypolyn (Polynomial & PA, Polynomial & Pb)
{
// Polynomial Multiplication
Polynomial sum, T; // sum saves the addition result, and t saves the result of multiplication in each step.
Link HS, HT, ha, Hb, QA, QB, S, T;
Term E;
// Elemtype A, B;
// Int m, n
/* Int la, Lb;
La = polynlength (PA );
Lb = polynlength (PB );*/
Initlist (SUM );
Initlist (t );
Cout <1 <Endl;
E. coef = 0.0;
E. EXPN =-1; // set the header Node
Ht = gethead (t );
HS = gethead (SUM );
Setcurelem (HS, e );
Setcurelem (HT, e );
Ha = gethead (PA );
If (Ha-> next = NULL)
Cout <"re" <Endl;
QA = nextpos (Pa, ha );
Cout <2 <Endl;
HB = gethead (PB );
While (QA)
{
QB = nextpos (Pb, Hb );
Cout <3 <Endl;
While (QB)
{

E. coef = QA-> data. coef * QB-> data. coef;
E. EXPN = QA-> data. EXPN + QB-> data. EXPN;
If (makenode (S, e ))
{
If (HT-> next = NULL)
{
Ht-> next = s;
T = s;
}
Else
{
T-> next = s;
S-> next = NULL;
T = s;
}
}
QB = QB-> next;
}
QA = QA-> next;
Addpolyn (sum, T, CMP );
Printpolyn (SUM );
Initlist (t );
Ht = gethead (t );
Cout <4 <Endl;
}
Destroypolyn (PB );
HS = gethead (SUM );
HS = nextpos (sum, HS );
Clearlist (PA );
Append (Pa, HS );
}

Void main ()
{
Polynomial A, B;
Createpolyn (A, 2 );
Createpolyn (B, 2 );
Printpolyn ();
Printpolyn (B );
// Addpolyn (a, B, CMP );
// Multiplypolyn (A, B );
Subtractpolyn (A, B );
Printpolyn ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.