Implementation of the addition of a polynomial under the sequential structure and a dynamic linked list structure.

Source: Internet
Author: User

 

# Include <stdio. h>
# Include <stdlib. h>
Typedef struct
{
Float coef;
Int EXPN;
} Term;
Typedef struct lnode
{
Term data;
Struct lnode * next;
} Lnode, * linklist;
Typedef linklist polynomail;
Int CMP (term a, term B)
{
If (A. EXPN> B. EXPN) return 1;
If (A. EXPN = B. EXPN) return 0;
If (A. EXPN <B. EXPN) Return-1;
Else exit (-2 );
}
Void arrange1 (polynomail PA)
{
Polynomail H = PA, P, Q, R;
If (Pa = NULL) Exit (-2 );
For (P = PA; P-> next! = NULL; P = p-> next); r = P;
For (H = PA; H-> next! = R & P! = R; P = p-> next)
{
If (CMP (p-> next-> data, p-> next-> data) = 1)
{
Q = p-> next;
P-> next = Q-> next;
Q-> next = p-> next;
P-> next = Q;
}
R = P;
}
}
Void arrange2 (polynomail PA)
{
Polynomail H = PA, P, Q, R;
If (Pa = NULL) Exit (-2 );
For (P = PA; P-> next! = NULL; P = p-> next); r = P;
For (H = PA; H-> next! = R ;)
{
For (P = H; P-> next! = R & P! = R; P = p-> next)
If (CMP (p-> next-> data, p-> next-> data) = 1)
{Q = p-> next;
P-> next = Q-> next;
Q-> next = p-> next;
P-> next = Q;
}
R = P;
}
}
Int printpolyn (polynomail P)
{
Int I;
Polynomail Q;
If (P = NULL) printf ("no item! \ N ");
Else if (p-> next = NULL) printf ("Y = 0 \ n ");
Else
{
Printf ("the polynomial is y ="); q = p-> next; I = 1;
If (Q-> data. coef! = 0 & Q-> data. EXPN! = 0)
{Printf ("%. 2fx ^ % d", Q-> data. coef, Q-> data. EXPN); I ++ ;}
If (Q-> data. EXPN = 0 & Q-> data. coef! = 0)
Printf ("%. 2f", Q-> data. coef );
Q = Q-> next;
If (q = NULL)
{
Printf ("\ n ");
Return 1;
}
While (1)
{
If (Q-> data. coef! = 0 & Q-> data. EXPN! = 0)
{
If (Q-> data. coef> 0) printf ("+ ");
Printf ("%. 2f ^ % d", Q-> data. coef, Q-> data. EXPN );
I ++;
}
If (Q-> data. EXPN = 0 & Q-> data. coef! = 0)
{
If (Q-> data. coef> 0) printf ("+ ");
Printf ("% F", Q-> data. coef );
}
Q = Q-> next;
If (q = NULL)
{
Printf ("\ n ");
Break;
}
}
}
Return 1;
}
Polynomail creatpolyn (polynomail P, int m)
{
Polynomail R, Q, P, S, Q;
Int I;
P = (lnode *) malloc (sizeof (lnode ));
R = P; // P is the header pointer! R points to the P header pointer!
For (I = 0; I <m; I ++)
{
S = (lnode *) malloc (sizeof (lnode ));
Printf ("Enter the coefficient and index of item % d:", I + 1 );
Scanf ("% F % d", & S-> data. coef, & S-> data. EXPN );
R-> next = s; // insert s nodes
R = s; // R points to the node S.
}
R-> next = NULL; // R points to the coefficient and index of the last item!
If (p-> next! = NULL)
{For (q = p-> next; Q! = NULL; q = Q-> next)
For (P = Q-> next, r = Q; P! = NULL;) // exit if p is a null pointer;
If (Q-> data. EXPN = p-> data. EXPN)
{
Q-> data. coef = Q-> data. coef + P-> data. coef; // Add the same exponent coefficient;
R-> next = p-> next;
Q = P;
P = p-> next;
Free (Q); // you can add one item to release another item;
}
Else
{
R = r-> next;
P = p-> next;
}
}
Return P;
}
Polynomail addpolyn (polynomail Pa, polynomail Pb)
{
Polynomail S, newp, Q, P, R; Int J;
P = pa-> next;
Q = Pb-> next;
Newp = (lnode *) malloc (sizeof (lnode ));
R = newp;
While (P & Q)
{
S = (lnode *) malloc (sizeof (lnode ));
Switch (CMP (p-> data, Q-> data ))
{
Case-1:
S-> data. coef = p-> data. coef;
S-> data. EXPN = p-> data. EXPN;
R-> next = s;
R = s;
P = p-> next;
Break;
Case 0:
S-> data. coef = p-> data. coef + q-> data. coef;
If (S-> data. coef! = 0.0)
{
S-> data. EXPN = p-> data. EXPN;
R-> next = s;
R = s;
}
P = p-> next;
Q = Q-> next;
Break;
Case 1:
S-> data. coef = Q-> data. coef;
S-> data. EXPN = Q-> data. EXPN;
R-> next = s;
R = s;
Q = Q-> next;
Break;
}
}
While (P)
{
S = (lnode *) malloc (sizeof (lnode ));
S-> data. coef = p-> data. coef;
S-> data. EXPN = p-> data. EXPN;
R-> next = s; r = s;
P = p-> next;
}
While (q)
{
S = (lnode *) malloc (sizeof (lnode ));
S-> data. coef = Q-> data. coef;
S-> data. EXPN = Q-> data. EXPN;
R-> next = s; r = s;
Q = Q-> next;
}
R-> next = NULL;
For (q = newp-> next; q-> next! = NULL; q = Q-> next)
For (P = Q; P! = NULL & P-> next! = NULL; P = p-> next)
If (Q-> data. EXPN = p-> next-> data. EXPN)
{
Q-> data. coef = Q-> data. coef + P-> next-> data. coef;
R = p-> next;
P-> next = p-> next;
Free (R );
}
Printf ("ascending 1, descending 2 \ n ");
Printf ("select ");
Scanf ("% d", & J );
If (j = 1)
Arrange1 (newp );
Else
Arrange2 (newp );
Return newp;
}
 
Void delpolyn (polynomail Pa, polynomail Pb)
{
Polynomail p, q;
P = PA;
While (P! = NULL)
{
Q = P;
P = p-> next;
Free (Q );
}
P = Pb;
While (P! = NULL)
{
Q = P;
P = p-> next;
Free (Q );
}
Printf ("two polynomials have been destroyed \ n ");
}

Void main ()
{
Polynomail Pa = NULL, Pb = NULL;
Polynomail p, q;
Polynomail ADDP = NULL;
Int n, m;
Int Sign = 'y ';
Printf ("1. Create two polynomial \ n ");
Printf ("2. A new polynomial \ n" after adding two polynomials ");
Printf ("3. Destroy two established polynomials \ n ");
Printf ("4. Exit \ n ");
Printf ("\ n ");
While (sign! = 'N ')
{
Printf ("select :");
Scanf ("% d", & N );
Switch (N)
{
Case 1:
If (Pa! = NULL)
{
Printf ("Two unary polynomials have been created. Please select another operation! ");
Break;
}
Printf ("Enter the first polynomial: \ n ");
Printf ("Enter several items :");
Scanf ("% d", & M );
While (M = 0)
{
Printf ("m cannot be 0. Enter M again :");
Scanf ("% d,", & M );
}
Pa = creatpolyn (Pa, M );
Printpolyn (PA );
Printf ("enter the second polynomial: \ n ");
Printf ("Enter several items :");
Scanf ("% d", & M );
PB = creatpolyn (Pb, M );
Printpolyn (PB );
Break;
Case 2:
If (Pa = NULL)
{Printf ("create two unidimensional polynomials first! \ N ");
Break;
}
ADDP = addpolyn (Pa, Pb );
Printpolyn (ADDP );
Break;
Case 3:
If (Pa = NULL)
{
Printf ("Please create two polynomial \ n first ");
Break;
}
Delpolyn (Pa, Pb );
Pa = Pb = NULL;
Break;
Case 4:
If (ADDP! = NULL)
{
P = ADDP;
While (P! = NULL)
{Q = P;
P = p-> next;
Free (Q );
}
}
Exit (-2 );
}
}
}

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.