Demonstration of Polynomial multiplication and Addition

Source: Internet
Author: User
Tags mul

 
# Include <stdio. h>
# Include <stdlib. h>
Typedef struct node
{
Int p;
Int e;
Struct node * next;
} Polynomial, * P_Polynomial;
P_Polynomial Input ()
{// Input Polynomial
P_Polynomial p1, p2, head;
Head = p2 = (P_Polynomial) malloc (sizeof (Polynomial ));
P1 = (P_Polynomial) malloc (sizeof (Polynomial ));
Printf ("\ n enter the coefficient and power of the polynomial \ n (Note: end with a power value of 0) \ n ");
Scanf ("% d", & p1-> p, & p1-> e );
While (p1-> e! = 0)
{
P2-> next = p1;
P2 = p1;
P1 = (P_Polynomial) malloc (sizeof (Polynomial ));
Scanf ("% d", & p1-> p, & p1-> e );
}
P2-> next = NULL;
Free (p1 );
Return head;
}
Void Add_polynomial (P_Polynomial p1, P_Polynomial p2)
{// Polynomial Addition
P_Polynomial pre_p1, cur_p1, cur_p2;
Pre_p1 = p1; // The front pointer of p1
Cur_p1 = p1-> next;
Cur_p1 = p2-> next; // The current pointer of p1 and p2
While (cur_p1 & cur_p2)
{
If (cur_p1-> e = cur_p2-> e) // if the power of p1 and p2 is the same, it is added to p1.
{
Cur_p1-> p = cur_p1-> p + cur_p2-> p;
If (cur_p1-> p = 0) // if p = 0
{
Pre_p1-> next = cur_p1-> next;
Free (cur_p1 );
Cur_p1 = pre_p1-> next;
Cur_p2 = cur_p2-> next;
}
Else // if p! = 0
{
Pre_p1 = cur_p1;
Cur_p1 = cur_p1-> next;
Cur_p2 = cur_p2-> next;
}
}
Continue; // continue find...
If (cur_p1-> e <cur_p2-> e)
{
Pre_p1 = cur_p1;
Cur_p1 = cur_p1-> next;
}
Else
{
P_Polynomial temp;
If (! (Temp = (P_Polynomial) malloc (sizeof (Polynomial ))))
{
Printf ("insufficient memory... ");
Exit (0 );
}
Temp-> p = cur_p2-> p;
Temp-> e = cur_p2-> e;
Temp-> next = cur_p1;
Pre_p1-> next = temp;
Pre_p1 = pre_p1-> next;
Cur_p2 = cur_p2-> next; // insert p2 in p1
}
}
While (cur_p2)
{
P_Polynomial temp;
If (! (Temp = (P_Polynomial) malloc (sizeof (Polynomial ))))
{
Printf ("insufficient memory... ");
Exit (0 );
}
Temp-> p = cur_p2-> p;
Temp-> e = cur_p2-> e;
Temp-> next = NULL;
Pre_p1-> next = temp;
Pre_p1 = pre_p1-> next;
Cur_p2 = cur_p2-> next;
}
}
P_Polynomial Mul_polynomial (P_Polynomial * exp1, P_Polynomial * exp2)
{// Polynomial Multiplication
P_Polynomial head, p1, p2, last, pre, px, q;
Int flag = 1;
Head = p1 = * exp1;
P1 = p1-> next;
For (; p1-> next! = NULL; p1 = p1-> next );
Px = last = p1;
P1 = (* exp1)-> next;
While (p1! = Px-> next)
{
Pre = p1;
Flag = 1;
P2 = (* exp2)-> next;
While (p2)
{
If (flag)
{
P1-> p = p1-> p * p2-> p;
P1-> e = p1-> e + p2-> e;
P2 = p2-> next;
Flag = 0;
}
Else
{
Q = (P_Polynomial) malloc (sizeof (Polynomial ));
Q-> p = pre-> p * p2-> p;
Q-> e = pre-> e + p2-> e;
Last-> next = q;
Last = q;
P2 = p2-> next;
}
}
P1 = p1-> next;
Last-> next = NULL;
}
Return head;
}
P_Polynomial Sort_polynomial (P_Polynomial pol)
{// Sorts polynomials,
P_Polynomial head = pol, p, q, r, t;
If (head-> next = NULL)
Printf ("input error !!! ");
Else
{
P = head-> next;
Q = p-> next;
P-> next = NULL;
P = q;
}
While (p! = NULL)
{
R = head;
Q = r-> next;
While (q! = NULL & q-> e <p-> e)
{
R = q;
Q = q-> next;
}
T = p-> next;
P-> next = r-> next;
R-> next = p;
P = t;
}
Return (head );
}
P_Polynomial Combine_polynomial (P_Polynomial head)
{// Merge Polynomials with the same exponent ..
P_Polynomial p1, p2;
P1 = head-> next;
While (p1-> next! = NULL)
{
If (p1-> next-> e = p1-> e)
{
P2 = p1-> next;
P1-> e = p1-> e + p2-> e;
P1-> next = p1-> next;
Free (p2 );
}
Else
{
P1 = p1-> next;
}
}
Return head;
}
Void output (P_Polynomial head)
{
P_Polynomial p1;
P1 = head-> next;
While (p1! = NULL)
{
If (p1-> e <0 & p1! = NULL)
Printf ("\ B ");
Printf ("% dX (% d) +", p1-> p, p1-> e );
P1 = p1-> next;
}
Printf ("\ B ");
Printf ("\ n ");
}
Int main ()
{
P_Polynomial p1, p2, add, mul;
Printf ("please input p1: \ n ");
P1 = Input ();
Printf ("please input p2: \ n ");
P2 = Input ();
// Printf ("p1 + p2 = ");
// Add = Add_polynomial (p1, p2 );
// Output (add); n
Printf ("p1 * p2 = ");
Mul = Mul_polynomial (& p1, & p2 );
Sort_polynomial (mul );
Combine_polynomial (mul );
Output (mul );
Return 0;

}

Related Article

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.