The operation of a unary polynomial

Source: Internet
Author: User

Take a unary polynomial addition operation as an example:

A/b can be expressed as a linear linked list:

The "and polynomial" list is as follows (the rectangular box in the figure represents the node that has been released):

#include <stdio.h>#include<stdlib.h>typedefstructpolyn{intdata; intindex; structPolyn *Next;} Polyn,*polynlist;voidCreatpolyn (Polynlist &p,intM//enter the coefficients and exponents of M-term to establish a unary polynomial P{        //There's a head knot.Polyn *q=p;  for(intI=0; i<m;i++) {Q->next= (polyn*)malloc(sizeof(Polyn)); Q=q->Next;//printf ("%d factor, exponent:", i+1);scanf"%d%d",&q->data,&q->index); } q->next=0;}voidPrintpolyn (Polynlist &p)//printing a unary polynomial P{Polyn*q=p->Next; //(x^0) and (x^1) to do special treatment    if(Q->index = =0) {printf ("%d",q->data); //Maybe the second index is X .Q=q->Next; if(q && Q->index = =1)        {            if(Q->data! =1) printf ("%+DX", Q->data);//positive number is preceded by plus +, otherwise no            Elseprintf ("+x"); Q= q->Next; }    }    Else if(Q->index = =1){        if(Q->data! =1) printf ("%DX",q->data); Elseprintf ("x"); Q= q->Next; }    Else{printf ("%dx^%d",q->data,q->index); Q= q->Next; }     while(q) {printf ("%+dx^%d",q->data,q->index); Q=q->Next; } printf ("\ n");}voidCopypolyn (polynlist &q,polyn *r)//NULL pointers must be referenced to create, new nodes linked list and copied (data fields and pointer fields){Q= (Polyn *)malloc(sizeof(Polyn)); Polyn*q1 = Q,*R1 = r->Next;  while(R1) {Q1->next = (Polyn *)malloc(sizeof(Polyn)); Q1= q1->Next; Q1->data = r1->data; Q1->index = r1->index; R1= r1->Next; } Q1->next =NULL;}voidShowMenu () {printf ("\t\t\t1. polynomial addition \ n"); printf ("\t\t\t2. Polynomial subtraction \ n"); printf ("\t\t\t3. multiplication by polynomial \ n"); printf ("\t\t\t4. Exit \ n");}voidAddpolyn (Polyn *p,polyn *p1)//Complete polynomial addition, P=P+P1, and destroy P1{Polyn*qa,*qb,*prior,*r;//set prior to insert a list of nodes in the B-linked listPrior =p; QA= p->Next; QB= p1->Next;  while(QA &&QB) {        if(Qa->index < qb->index) {Prior=QA; QA= qa->Next; }        Else if(Qa->index > qb->index) {R=QB; QB= qb->Next; //remove QB to "and polynomial" linked listR->next =QA; Prior->next =R; //Update PriorPrior =R; }        Else{            if(Qa->data + qb->data! =0)            {                //Modify the value of the coefficients of the points that the QA refers to, while releasing the nodes that the QB refers toQa->data = Qa->data + qb->data; R=QB; QB= qb->Next;  Free(R); }            Else{                //release pointer QA and QB-referred nodesR =QA; QA= qa->Next; Prior->next = QA;//Bridging                 Free(R); R=QB; QB= qb->Next;  Free(R); }        }    }    //and the B-linked list has nodes.    if(QB) {prior->next = QB;//The QA is now null pointer    }     Free(p1);//releasing the B-head node.}voidReverse (Polyn *p) {Polyn*q = p->Next;  while(q) {q->data =-(q->data); Q= q->Next; }}voidDestroypolyn (Polyn *R) {Polyn*Q;  Do{Q=R; R= r->Next;  Free(q); } while(R);}voidAdjustpolyn (Polyn *r,intDataintIndex//Adjust each item{Polyn*q = r->Next;  while(q) {q->data *=data; Q->index + =index; Q= q->Next; }}voidSubtractpolyn (polynlist &p,polynlist &p1)//complete the polynomial subtraction operation, P=P-P1, and destroy the P1{reverse (p1);//resets the data domain of the B-linked list toAddpolyn (P,P1);}voidMultiplypolyn (polynlist &p,polyn *p1)//completes the polynomial multiplication operation, p=p*p1, and destroys P1{Polyn*q,*r = NULL,*R1 =NULL; Q= p1->Next; //A (x)--"bi*a (x) *x^eiCopypolyn (r,p); Adjustpolyn (R,q->data,q->index); Q= q->Next;  while(q) {Copypolyn (r1,p); Adjustpolyn (R1,q->data,q->index); Addpolyn (R,R1);//R1 will be destroyed .R1 = NULL;//The R1 must be placed emptyQ = q->Next; }    //R-->p, simultaneous release of the R-linked listDestroypolyn (P); P=NULL;    Copypolyn (P,R); Destroypolyn (R);}intMain () {intm; Polyn*p= (polyn*)malloc(sizeof(Polyn)); Polyn*p1= (polyn*)malloc(sizeof(Polyn)); Polyn*P2 = NULL,*P3 =NULL; printf ("Press Ascending to enter polynomial a (x), B (x) coefficients, exponent \ n"); printf ("Number of items A (x):"); scanf ("%d",&m);    Creatpolyn (P,M); printf ("A (x) =");    Printpolyn (P); printf ("number of items in B (x):"); scanf ("%d",&m);    Creatpolyn (P1,M); printf ("B (x) =");    Printpolyn (p1); //Save A, b original polynomial, where replication is the new open node listCopypolyn (p2,p);    Copypolyn (P3,P1); System ("Pause"); System ("CLS"); printf ("A (x) =");    Printpolyn (P); printf ("B (x) =");    Printpolyn (p1); printf ("Select one of the following actions: \ n");    ShowMenu ();  Do{printf ("Please enter your choice:"); scanf ("%d",&m); Switch(m) { Case 1: Addpolyn (P,P1); printf ("A (x) +b (x) =");            Printpolyn (P);  Break;  Case 2: Subtractpolyn (P,P1); printf ("A (x)-B (x) =");            Printpolyn (P);  Break;  Case 3: Multiplypolyn (P,P1); printf ("A (x) *b (x) =");            Printpolyn (P);  Break; default: Exit (0);        } fflush (stdin);        Destroypolyn (P); P1= P = NULL;//at this point the P, P1 is a free pointer, must be emptyCopypolyn (P,P2);    Copypolyn (P1,P3); } while(1); return 0;}

Operation Result:

After pressing the ENTER key

The operation of a unary polynomial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.