The multiplication of a unary polynomial requires that the exponent of each item of a polynomial be multiplied by the exponent of each of the other polynomial.
Code implementation:
header files and function declarations:
#ifndef _polyn_h #define _polyn_h #include <iostream> #include <malloc.h> #include < stdio.h>using namespace std; #define _crt_secure_no_deprecate#define NULL 0typedef struct NODE {float coef; coefficient int expn; exponential struct NODE *next;} NODE; node *creat (int n); void print (node *head); Node *addpolyn (node *head1, node *head2); Node *delfirst (node *head, node *q); void InsertBefore (Node *p1, node *p2); int
function definition:
#include "polyn.h" #include <iostream> void menu () {cout << << endl;cout << ****************** "<< endl;cout <<" * [0] Quit_system * "<< endl;cout <<" * [1] Poly Nadd [2] Polynmul * "<< endl;cout <<" ********************************** "<< endl;cout <<" pl ESE chose:> ";} int inputnum (int n) {while (n <= 0) {cout<< "The value entered is wrong, please confirm that the value entered is an integer greater than 0!" : "; cin>>n;} return n;} /* Create linked list */node *creat (int n) {node *current, *previous, *head;int I;head = (node *) malloc (sizeof (node));/* Create header node */previous = Head;for (i = 0; i < n; i++) {current = (node *) malloc (sizeof (node));cout<< "Please enter coefficients and exponents:"; Cin >> Current-> ; coef;cin>>current->expn;previous->next = current;previous = current;} Previous->next = Null;return Head;} /* Sort */bool sortdown (NODE *head) {node* p = NULL; node* q = null;for (p = head->next; P! = null; p = p->next) {for (q = p->next; Q! = null; q = q->next) {if (P->expn < Q->EXPN) {p->expn = p->expn ^ q->expn;q->expn = p->expn ^ q-> EXPN;P->EXPN = p->expn ^ Q->expn;p->coef = p->coef + Q->coef;q->coef = p->coef-q- >coef;p->coef = P->coef-q->coef;}}} return true;} BOOL Sortup (NODE *head) {node* p = NULL; node* q = null;for (p = head->next; P! = null; p = p->next) {for (q = p->next; Q! = null; q = q->next) {if (P-&G T;EXPN > q->expn) {p->expn = p->expn ^ q->expn;q->expn = p->expn ^ q->expn;p->expn = P ->expn ^ Q->expn;p->coef = p->coef + Q->coef;q->coef = P->coef-q->coef;p->coef = P->c Oef-q->coef;}}} return true;} /* Compare */int compare (int a, int b) {if (a < b) Return-1;else if (a > B) return 1;elsereturn 0;} /* Delete node Q*/node *delfirst (node *p1, node *q) {p1->next = Q->next;return (q);} /* Insert node, introduce node p, allows p to be inserted between P2 and P1 */void insertbefore (node *p1, node *p2) {node *p;p = p1->next;p1->next = P2;p2->next = P;} /* Print, */void print (NODE *head) {Sortdown (head) separately for aesthetic programs; NODE *cur;cur = Head->next;while (cur->next! = NULL) {cout<<cur->coef<< "*x^" <<cur->expn << "+"; cur = cur->next;} cout << cur->coef << "*x^" << cur->expn; cout << Endl;} /* The sum of the unary polynomial, overall consideration, can be divided into the QA index smaller than QB, or equal to PB (if the coefficients are added equal to 0 and not equal to 0), or greater than PB inside by InsertBefore and Delfirst two small modules part */node *addpolyn (NODE * HEAD1, node *head2) {node *ha, *HB, *qa, *qb;int A, b;float sum;ha = head1; /*ha and HB point to the head node */HB = head2;if (ha! = null)//head1 determine if NULL {if (HB = = null) {return head1;} Else{qa = ha->next; /*qa and QB points to the next node of the head node */QB = hb->next;while (QA && QB)/*QA and QB are NOT null */{a = Qa->expn;b = Qb->expn;swi TCH (Compare (A, B)) {case-1:/*qa->expn < qb->expn*/ha = Qa;qa = Qa->next;break;case 0:sum = qa- >coef + qb->coef; /* coefficients and */if (sum! = 0.0) {/* If not 0.0*/QA->coef = sum; /* Change factor */ha = QA;} else {free (Delfirst (ha, QA));} Free (Delfirst (HB, QB)); QA = HA->NEXT;QB = hb->next; /*QB after release to re-assign value */break;case 1:/* If qa-> expn > QB-Expn*/delfirst (HB, QB); InsertBefore (Ha, QB); /* Insert QB before ha next node */qb = Hb->next;ha = Ha->next;break;}}} if (QB) Ha->next = QB; /* Insert the remaining pb*/free (head2); return head1;} Else{return head2;}} /* Implement add and create multiple unary polynomial */node *creatlinklist (int n) {int i, A;char x; NODE *head[100], *headsum; An array of length 100, long enough. Headsum = null;for (i = 1; I <= n; i++) {cout << \ n Please enter the number of "<< I <<" polynomial: ";//sca nf_s ("%d", &a), cin >> a;a = Inputnum (a), cout << "(enter coefficients and exponents by index size)" << endl;head[i] = creat (a);//Create Head[i] As the head node of the linked list cout << "The next type is what you entered the" << I << "polynomial:" << endl;print (Head[i]); cout << "Please confirm that the input is Correct, if wrong please input n re-enter the type, correct please enter y confirm (lowercase): "; Confirm Input cin >> x;if (x = = ' n ')//if wrong correct {if (i = = 1)//If the first item is wrong I do not subtract, will first oneMeta-polynomial empty {head[i] = NULL;} else{//If it is not the first item, the polynomial that has been entered is emptied and I minus 1 head[i] = null;//i--;}} Headsum = Addpolyn (Headsum, head[i]);//Add each linked list if (x = = ' n ')///If the first item is wrong i--{i--;}} return headsum;} /* The multiplication of a unary polynomial requires that the exponent of each item of one polynomial be added to the exponent of each of the other polynomial and multiply the coefficients */node *mulpolyn (node *a, node *b) {node *pa, *PB, *pc, *u, *head;int k = 0;int Maxexp = 0;//maxexp is the maximum value of two linked list indices and float Coef = 0.0;head = (node *) malloc (sizeof (node)); if (!head) return Null;head-> ; coef = 0.0;HEAD->EXPN = 0;head->next = NULL; Sortdown (A); Sortdown (B); if (a->next! = NULL && B->next! = null) Maxexp = (A->NEXT->EXPN) + (B->NEXT->EXPN); e Lsereturn head;pc = head; Sortup (B); for (k = maxexp; k >= 0; k--)/* Determines the exponential range of a polynomial product of 0 to Maxexp*/{pa = a->next;while (PA! = NULL && Pa-> ; expn > K)/* Find PA position */pa = PA->NEXT;PB = B->next;while (PB! = NULL && PA! = null && (pa->expn + pb->expn) < K)/* if and less than K,PB move to the next node */PB = Pb->next;coef = 0.0;while (PA! = null && PB! = NULL) {if (PA->EXPN) + (pb->expn) = = k) {Coef + = (PA->COEF) * (PB->COEF);p a = PA->NEXT;PB = Pb->next;} else if ((PA->EXPN) + (PB->EXPN) > K)/* if and greater than k,pa move to the next node */pa = PA->NEXT;ELSEPB = pb->next;/* if and less than K,PB move to the next A node */}if (coef! = 0.0) {/* If the coefficients are not 0, a new node is generated and the coefficients and exponents are assigned to the new node and inserted into the list */u = (node*) malloc (sizeof (NODE)); u->coef = Coef; U->EXPN = K;u->next = Pc->next;pc->next = u;pc = U;}} Sortdown (B); return head;}
Main function:
#include "polyn.h" void main (int n) {char y; NODE *headsum; NODE *headmul;int select = 1; NODE *a; NODE *b;int NumA = 0;int NumB = 0;menu (); Cin >> Select;switch (SELECT) {case 1:/* add */cout << ***************** Welcome to the unary polynomial add-on program ****************** "<< endl;cout <<" Enter the number of the polynomial (integers greater than 0): "; cin >> n;n = Inputnum (n); headsum = Creatlinklist (n); cout << endl;cout << "added polynomial display:" << endl;print (headsum); cout << "**************************** Thank you for using *********************************** << endl;cout <<" continue? Please enter (y/n): "; Cin >> y;if (y = = ' Y ') {main (1);} Break;case 2:/* Multiply */cout << "**************************** Welcome to two unary polynomial multiplication program ******************" << endl;cout << "Please enter the number of the 1th polynomial:"; Cin >> numa;cout << "(Please enter coefficients and indices by index size) << Endl; A = creat (NumA); cout << "The following is the 1th polynomial you entered:" << endl;print (A); cout << "Please enter the number of the 2nd polynomial:"; CIN >> Numb;c Out << "(Please enter coefficients and indices by index size)" << Endl; B = creat (NumB); cout << "The next formula is the 1th polynomial you entered:" << endl;print (b); headsum = Mulpolyn (A, b); cout << endl;cout << "multiply Item-type display: "<< endl;print (headsum); cout <<" **************************** Thank you for using ****************************** "<< endl;cout <<" Continue please enter (y/n): "; Cin >> y;if (y = = ' Y ') {main (1);} Break;break;default:break;}}
The results of the operation are as follows:
"Application of Chain list" the addition and multiplication of one-element polynomial and analysis of problems