The following is an experiment report we made earlier: (Data Structure C Edition)
[Design question]
Algebraic operation of Polynomial
[Problem description]
Calculate the addition, subtraction, and multiplication of any two unary polynomials.
[Basic requirements]
Input the exponent and coefficient of each of the two polynomials by the keyboard, and output the sum and product of the two polynomials.
[Implementation description]
You can store linear tables by link.
The following is the code I wrote. The language is C, and the running environment is
Window XP
Under
Turbo C 2.0
Implementation
:
# Include "stdio. H" # include "alloc. H" typedef struct node {int C, E;/* C
Is the coefficient, e is the index */struct node * Next;} Polytype; Polytype * Create () /* input and establish polynomial */{Polytype * head = malloc (sizeof (Polytype), * P; int N, E, C;/* n
Is the number of items, e is the index, C is the coefficient */P = head; printf ("/N:"); scanf ("d %", & N ); while (n) {printf ("/n C, E:"); scanf ("% d, % d", & C, & E); n --; p-> next = malloc (sizeof (Polytype ));/*
Create an empty single-chain table */P = p-> next; P-> C = C; P-> E = E;} p-> next = NULL; return head ;/*
Returns the pointer */} void printpoly (Polytype * head)/* output polynomial */{Polytype * P = head-> next; while (p) {printf ("(% d, % d), ", p-> C, p-> E); P = p-> next;} printf ("/N ");} polytype * polyadd (Polytype * Ha, Polytype * Hb) {/* polynomial addition */Polytype * HC = malloc (sizeof (Polytype), * Pc = HC, * pA = ha-> next, * pb = HB-> next; int e, C;/* C
Is the coefficient, e is the index */while (PA | Pb) {If (PA & (PB = NULL | (Pa-> E <Pb-> E) {c = pa-> C; E = pa-> E; Pa = pa-> next ;} else if (Pb & (Pa = NULL | (Pa-> E> Pb-> E) {c = Pb-> C; E = Pb-> E; Pb = Pb-> next;} else {c = pa-> C + Pb-> C; E = pa-> E; /* or E = Pb-> E */Pa = pa-> next; Pb = Pb-> next;} If (c) {PC-> next = malloc (sizeof (Polytype); Pc = pc-> next; PC-> C = C; PC-> E = E ;}} PC-> next = NULL; return HC;} Polytype * polyminus (Polytype * Ha, Polytype * Hb) {/* polynomial subtraction */Polytype * HC = malloc (sizeof (Polytype), * Pc = HC, * pA = ha-> next, * pb = HB-> next; int E, C;/* C
Is the coefficient, e is the index */while (PA | Pb) {If (PA & (PB = NULL | (Pa-> E <Pb-> E) {c = pa-> C; E = pa-> E; Pa = pa-> next ;} else if (Pb & (Pa = NULL | (Pa-> E> Pb-> E) {c = Pb-> C; E = Pb-> E; Pb = Pb-> next;} else {c = pa-> C-Pb-> C; E = pa-> E; /* or E = Pb-> E */Pa = pa-> next; Pb = Pb-> next;} If (c) {PC-> next = malloc (sizeof (Polytype); Pc = pc-> next; PC-> C = C; PC-> E = E ;}} PC-> next = NULL; return HC;} Polytype * onexmul (Polytype * pA, Polytype * Hb) {/* single multiplication multiple */ Polytype * pb = HB-> next, * PC, * HC = malloc (sizeof (Polytype); Pc = HC; while (PB) {PC-> next = malloc (sizeof (Polytype); Pc = pc-> next; PC-> E = pa-> E + Pb-> E; PC-> C = pa-> C * pb-> C; Pb = Pb-> next;} PC-> next = NULL; return HC ;} polytype * mulxmul (Polytype * Ha, Polytype * Hb) {/* multiplication of multiple numbers */Polytype * t, * HC = malloc (sizeof (Polytype), * pA; pa = ha-> next; HC-> next = NULL; while (PA) {T = onexmul (Pa, Hb); Hc = polyadd (HC, t ); freepoly (t); Pa = pa-> Ne XT;} return HC;} void freepoly (Polytype * head) {Polytype * P = head; while (p) {head = head-> next; free (P ); P = head ;}} main () {Polytype * Ha, * Hb, * HC; HA = create (); printpoly (HA); HB = create (); printpoly (HB); Hc = polyadd (HA, Hb); printf ("A + B:"); printpoly (HC); freepoly (HC); Hc = polyminus (HA, HB); printf ("A-B:"); printpoly (HC); freepoly (HC); Hc = mulxmul (HA, Hb); printf ("A * B: "); printpoly (HC); freepoly (HC); freepo Ly (HA); freepoly (HB);} the above Code can meet the requirements of the question, but I think it is not very good ,. however, it is helpful for beginners .. so it is for reference only... if you want to learn the technology well, I suggest you write it by yourself ..........