Realize multiplicity by chain list

xiaoxiao2021-03-06  62

/ ************************************************** ***** The polynomial is multiplied by ************************************************************* ************* / # include #include #include

// Structure of one of the polynomials

Typedef struct term {double Coef; int expn; struct term * next;} PolyNode, * PPOLYNODE

/ / Create a list of saved multiplicithels, return pointers that point to the header node. Multi-class, order-descending PPOLYNODE CREATEPOLY () PPOLYNODE CREATEPOLY () PPOLYNODE * P, * Q, * S, * Head = NULL; Double Coef; Int Exp

HEAD = (PPOLYNODE) ​​Malloc (hEAD == NULL) {Printf ("Allocable Memory Fail! / N"); return null;} head-> Coef = 0.0; head-> evgn = 0 Head-> next = NULL;

Printf ("PLEASE INPUT COEFFICIENT: / N"); // Scanf ("% LF% D", & Coef, & Expn); Printf ("please input coefficient:"; scanf ("% LF ", & COEF); Printf (" "" ",", "% 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 && express 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;

}

// A polynomial is reversed, and the boom is arranged.

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;} ​​// two polynomial multiplogen 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); if (head == null) {printf ("Allocable Memory Fail! / N" Return NULL;

Head-> Coef = 0.0; head-> evgn = 0; head-> next = null; if (a-> next! = null && b-> next! = null) {maxexp = (a-> next) -> EXPN (B-> Next) -> EXPN;} else {return;} pc = head; b = reverse (b);

For (k = maxexp; k> = 0; k -) {pa = a-> next; while (pa! = null && pa-> evN> k) PA = PA-> Next; PB = B-> Next WHILE (PB! = NULL && PA! = NULL && (PA-> EXPN PB-> EXPN) 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 (A); Printf (" / n ");

B = createPoly (); Printf ("B (x) ="); PrintPoly (b); Printf ("/ n");

C = Multiply (a, b);

// c = creagepoly (); Printf ("c (x) ="); PrintPoly (c); Printf ("/ n");

// Getch ();

Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-118439.html

New Post(0)