Multiplication and addition of a polynomial (C), polynomial Multiplication
Input Format:
The input is divided into two rows. Each row first gives the number of non-zero polynomial items, and then enters a non-zero polynomial coefficient and exponent (the absolute value is an integer not greater than 1000) in the exponential descent mode ). Numbers are separated by spaces.
Output Format:
The output is divided into two rows. The product polynomials and non-zero coefficients and exponent of polynomials are output in the exponential descent mode respectively. Numbers are separated by spaces, but no extra space is allowed at the end. Zero polynomial output0 0
.
Input example:
4 3 4 -5 2 6 1 -2 03 5 20 -7 4 3 1
Output example:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 15 20 -4 4 -5 2 9 1 -2 0
#include <stdio.h>#include <stdlib.h>//#include <dos.h>typedef struct polyNode{ int coef; int exp; struct polyNode *next;}polyNode, *polyList;void DestroyList(polyList L);void printList(polyList L);polyList creatList(int n);polyList add(polyList a, polyList b);polyList mul(polyList a, polyList b);int main(){ int n1, n2, i; polyList a, b, L1, L2; scanf("%d", &n1); a = creatList(n1); scanf("%d", &n2); b = creatList(n2); L1 = mul(a, b); L2 = add(a, b); printList(L1); printf("\n"); printList(L2); DestroyList(L1); DestroyList(L2); //system("pause"); return 0;}void DestroyList(polyList L){ polyNode * tmp; while (L) { tmp = L->next; free(L); L = tmp; }}polyList creatList(int n){ polyNode *head, *r, *p; int coef, exp; head = (polyNode *)malloc(sizeof(polyNode)); r = head; while (n--) { scanf("%d%d", &coef, &exp); p = (polyNode *)malloc(sizeof(polyNode)); p->coef = coef; p->exp = exp; r->next = p; r = p; } r->next = NULL; return head;}polyList add(polyList a, polyList b){ polyNode *ha, *hb, *p, *r, *h; int temp; ha = a->next; hb = b->next; h = (polyNode *)malloc(sizeof(polyNode)); r = h; while (ha != NULL && hb != NULL) { p = (polyNode *)malloc(sizeof(polyNode)); if (ha->exp < hb->exp) { p->exp = hb->exp; p->coef = hb->coef; hb = hb->next; r->next = p; r = p; } else if (ha->exp > hb->exp) { p->exp = ha->exp; p->coef = ha->coef; ha = ha->next; r->next = p; r = p; } else { temp = ha->coef + hb->coef; if (temp != 0) { p->exp = ha->exp; p->coef = temp; r->next = p; r = p; } ha = ha->next; hb = hb->next; } } while ( hb != NULL ) { p = (polyNode *)malloc(sizeof(polyNode)); p->exp = hb->exp; p->coef = hb->coef; hb = hb->next; r->next = p; r = p; } while ( ha != NULL ) { p = (polyNode *)malloc(sizeof(polyNode)); p->exp = ha->exp; p->coef = ha->coef; ha = ha->next; r->next = p; r = p; } r->next = NULL; DestroyList(a); DestroyList(b); return h;}polyList mul(polyList a, polyList b){ polyNode *ha, *hb , *r, *p; polyList c, tempc; ha = a->next; hb = b->next; c = (polyNode *)malloc(sizeof(polyNode)); c->next = NULL; if (ha == NULL || hb == NULL) { return c; } while (ha != NULL) { tempc = (polyNode *)malloc(sizeof(polyNode)); r = tempc; hb = b->next; while (hb != NULL) { p = (polyNode *)malloc(sizeof(polyNode)); p->exp = ha->exp + hb->exp; p->coef = ha->coef * hb->coef; hb = hb->next; r->next = p; r = p; } r->next = NULL; c = add(c,tempc); ha = ha->next; } return c;}void printList(polyList L){ polyNode * x; x = L->next; if (x == NULL) { printf("0 0"); } while (x != NULL) { if (x->next == NULL) { printf("%d %d", x->coef, x->exp); } else printf("%d %d ", x->coef, x->exp); x = x->next; }}