Data structure second on-machine experiment "chain list to achieve the addition and multiplication of polynomial"

Source: Internet
Author: User
Tags define null

The addition and multiplication of unary polynomial using linked list

#define NULL 0#include "stdio.h" #include "stdlib.h" #include "math.h" int op;typedef struct{float coef;//coefficient int E xpn;//index}term;typedef struct lnode{term data; Lnode *next;} *link,*linklist;int CMP (term a,term B)//index from small to large sort {if (A.EXPN==B.EXPN) return 0;else return (A.EXPN-B.EXPN)/abs (A. EXPN-B.EXPN);} void OrderInsert (linklist &l,term e,int (*comp))//Enter the number of entries and exponents directly into the linked list (no items that can be merged) {link term,term Next;while (P&&comp (p->data,e) <0) {Q=p;p=p->next;} O= (Lnode) (sizeof); o->data=e;q->next=o;o->next=p;}    void Chengfa (linklist &l1,linklist &l2,linklist &l3) {L3 = (Link) malloc (sizeof (Lnode));    L3->next = null;    Link S = l1->next; term s, e;while (s) {s.coef=s->data.coef;s.expn=s->data.expn;            Link Q=l2->next;while (Q) {e.coef = S.coef * q->data.coef;            E.EXPN = s.expn + q->data.expn;            OrderInsert (L3,E,CMP); Q=q->next;} S=s->next;}} int Locateelem (linklist l,term E,link &s,link &q,int (*comp) (term,term)//Determine if there are items that can be merged {Link p;s=l;p=s->next;while (P&&comp (P- >data,e)!=0) {S=p;p=p->next;} if (!p) {S=q=null;return 0;} else {Q=p;return 1;}} void Delnext (linklist &l,link s) {Link q=s->next; s->next=q->next; free (q);} void Orderinsertmerge (linklist &l,term e,int (*compara))//merge the same items in the same polynomial {Link term,term (q,s;if,    S,q,compara)) {q->data.coef+=e.coef;if (!Q-&GT;DATA.COEF) {delnext (l,s); }}else OrderInsert (L,e,compara);} void Creatpolyn (linklist &p,int m)//coefficient of input polynomial and exponent {term e; int i; p= (link) malloc (sizeof (Lnode))//Open space for list p->next= Null printf ("\ n Please enter%d coefficients and exponents with a space character interval: \ n", m);  for (i=1;i<=m;i++) {scanf ("%f%d", &AMP;E.COEF,&AMP;E.EXPN); Orderinsertmerge (P,E,CMP);//merge the entries that can be merged in the input polynomial}}void add (linklist &la,linklist Lb)//merge two polynomial {Link qb;term b; qb=lb- >next; while (QB) {b=qb->data; Orderinsertmerge (LA,B,CMP); qb=qb->next; }//Destroy linked list lb}void Fuzhi (linklist &lc,linklist La) {lc= (Link) malloc (sizeof (Lnode)); lc->next=null; Link s=la->next;term E;while (S) {e.coef=s->data.coef;e.expn=s->data.expn;orderinsertmerge (Lc,e,cmp); S=s->next;}} void Printpolyn (linklist p)//print out the added polynomial {Link q;q=p->next;int t=0;while (q) {if (t==0) {if (q->data.coef==1) printf    ("x^%d", Q-&GT;DATA.EXPN); else if (q->data.coef==-1) printf ("-x^%d", q->data.expn), else if (q->data.coef>0) printf ("%.2f*x^%d", Q- &GT;DATA.COEF,Q-&GT;DATA.EXPN); else if (q->data.coef<0) printf ("%.2f*x^%d", Q-&GT;DATA.COEF,Q-&GT;DATA.EXPN) ;}    Else{if (q->data.coef==1) printf ("+x^%d", Q-&GT;DATA.EXPN); else if (q->data.coef==-1) printf ("-x^%d", q->data.expn), else if (q->data.coef>0) printf ("+%.2f*x^%d", Q -&GT;DATA.COEF,Q-&GT;DATA.EXPN); else if (q->data.coef<0) printf ("%.2f*x^%d", Q-&GT;DATA.COEF,Q-&GT;DATA.EXPN );} q=q->next; t++;}} int main () {int x; linklist l1,l2,l3;printf ("\ n Please enter the number of entries for the first unary polynomial:"); scanf ("%d", &x); Creatpolyn (l1,x);p rintf ("\ n Please enter a second unary moreItem Number of items: "); scanf ("%d ", &x); Creatpolyn (l2,x); Fuzhi (L3,L1); Add (L3,L2);p rintf ("\ n");p Rintpolyn (L3);p rintf ("The unary polynomial after multiplying: \ n");    CHENGFA (L1,L2,L3);p Rintpolyn (L3); return 0;}

  

Data structure second on-machine experiment "chain list to achieve the addition and multiplication of 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.