/*************************************** ***************
Polynomial Multiplication
**************************************** ***************/
# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>
// Structure of a Polynomial
Typedef struct term {
Double coef;
Int EXPN;
Struct term * next;
} Polynode, * ppolynode;
// Create a linked list that stores polynomials and returns a pointer to the header node. Polynomials are sorted in descending order of Exponent
Ppolynode createpoly ()
{
Polynode * P, * q, * s, * head = NULL;
Double coef;
Int EXPN;
Head = (ppolynode) malloc (sizeof (polynode ));
If (Head = NULL)
{
Printf ("allocable memory fail! /N ");
Return NULL;
}
Head-> coef = 0.0;
Head-> EXPN = 0;
Head-> next = NULL;
Printf ("Please input coefficient and exponent (intput 0 0 end):/N ");
// Scanf ("% lf % d", & coef, & EXPN );
Printf ("Please input coefficient :");
Scanf ("% lf", & coef );
Printf ("Please input exponent :");
Scanf ("% d", & EXPN );
While (long) coef! = 0 & EXPN! = 0)
{
S = (ppolynode) malloc (sizeof (polynode ));
S-> coef = coef;
S-> EXPN = EXPN;
Q = head-> next;
P = head;
While (Q & EXPN <q-> EXPN)
{
P = Q;
Q = Q-> next;
}
If (q = NULL | EXPN> q-> EXPN)
{
P-> next = s;
S-> next = Q;
}
Else
{
Q-> coef + = coef;
}
// Read next number
Printf ("Please input coefficient :");
Scanf ("% lf", & coef );
Printf ("Please input exponent :");
Scanf ("% d", & EXPN );
}
Return head;
}
// Reverse the polynomial by ascending power
Ppolynode reverse (ppolynode head)
{
Polynode * P, * q, * t;
P = NULL;
Q = head-> next;
While (Q! = NULL)
{
T = Q-> next;
Q-> next = P;
P = Q;
Q = T;
}
Head-> next = P;
Return head;
}
// Multiply two Polynomials
Ppolynode multiply (ppolynode A, ppolynode B)
{
Polynode * pA, * pb, * PC, * u, * head;
Int K, maxexp;
Double coef;
// The head node of the multiply
Head = (ppolynode) malloc (sizeof (polynode ));
If (Head = NULL)
{
Printf ("allocable memory fail! /N ");
Return NULL;
}
Head-> coef = 0.0;
Head-> EXPN = 0;
Head-> next = NULL;
If (a-> next! = NULL & B-> next! = NULL)
{
Maxexp = (a-> next)-> EXPN + (B-> next)-> EXPN;
}
Else
{
Return head;
}
PC = head;
B = reverse (B );
For (k = maxexp; k> = 0; k --)
{
Pa = A-> next;
While (Pa! = NULL & pa-> EXPN> K)
Pa = pa-> next;
PB = B-> next;
While (PB! = NULL & pa! = NULL & (Pa-> EXPN + Pb-> EXPN) <K)
PB = Pb-> next;
Coef = 0.0;
While (Pa! = NULL & Pb! = NULL)
{
If (Pa-> EXPN + Pb-> EXPN) = K)
{
Coef + = pa-> coef * pb-> coef;
Pa = pa-> next;
PB = Pb-> next;
}
Else
{
If (Pa-> EXPN + Pb-> EXPN)> K)
{
Pa = pa-> next;
}
Else
{
PB = Pb-> next;
}
}
}
If (coef! = 0.0)
{
U = (ppolynode) malloc (sizeof (polynode ));
U-> coef = coef;
U-> EXPN = K;
U-> next = pc-> next;
PC-> next = u;
PC = u;
}
}
B = reverse (B );
Return head;
}
// Print poly
Void printpoly (ppolynode head)
{
Polynode * P = head-> next;
While (P)
{
Printf ("% 1.1f", p-> coef );
If (p-> EXPN)
Printf ("* x ^ % d", p-> EXPN );
If (p-> next & P-> next-> coef> 0)
Printf ("+ ");
P = p-> next;
}
}
Int main ()
{
Ppolynode A, B, C;
A = createpoly ();
Printf ("A (x) = ");
Printpoly ();
Printf ("/N ");
B = createpoly ();
Printf ("B (X) = ");
Printpoly (B );
Printf ("/N ");
C = multiply (A, B );
// C = createpoly ();
Printf ("C (x) = ");
Printpoly (C );
Printf ("/N ");
// Getch ();
Return 0;
}