Addition and multiplication of unary polynomial

Source: Internet
Author: User

#include <iostream> #include <stdlib.h>using namespace std;typedef struct{int coef;//coefficient entry int exp;//index Item} Elemtype; typedef struct NODE{ELEMTYPE data;struct Node *next;} Lnode,*linklist;void initlist (linklist &l); void Polycreate (linklist l); void Output (linklist l); Linklist Polyadd (linklist la,linklist Lb); Linklist Multiply (linklist l,int coef,int exp);    Linklist polymultiply (linklist la,linklist lb); void ShowMenu () {cout<< "1, demonstrating the addition of unary polynomial:" <<endl;    cout<< "2, demonstration of multiplication of unary polynomial:" <<endl; cout<< "0, Exit" &LT;&LT;ENDL;}    int main () {ShowMenu ();    int x;        while (1) {cout<< "Please enter the action you want to take:";        cin>>x;                Switch (x) {case 1:{linklist l;                Initlist (l);                Polycreate (l);                                 Output (l);                Linklist L;                Initlist (L);                Polycreate (L);                Output (L);               Lnode *p = Polyadd (l,l); cout<< "Add-on polynomial:" <<endl;                Output (P);            Break                } case 2:{linklist L1;                Initlist (L1);                Polycreate (L1);                 Output (L1);                Linklist L2;                Initlist (L2);                Polycreate (L2);                 Output (L2);                Lnode *temp = polymultiply (L1,L2);                cout<< "The polynomial after multiplying is:" <<endl;                Output (temp);            Break                } Case 0:{exit (0);            Break }}} return 0;} void Initlist (linklist &l) {L = new Lnode; L->next = NULL;} void Polycreate (linklist l) {int c,e; Lnode *p = l;cout<< "Please establish this unary polynomial in the order of exponential increments:" <<endl; while (1) {cout<< "input the coef,exp of a term, exp=-1 'll end:" <<endl;cin>>c;cin>>e;if (E==-1) Break Lnode *s = new Lnode;s->data.coef = C;s->data.exp = E;p->next = S;p = s;} P->next = NULL;} void Output (linklist l) {cout<< "The Poly is:" <<endl; Lnode *p;p=l->next;while (P) {cout<<p->data.coef;if (p->data.exp) cout<< "X^" <<p-> Data.exp;if (P->next && p->next->data.coef>0) cout<< "+";p = P->next;} cout<< "\ n";} Linklist Polyadd (linklist la,linklist Lb) {lnode *p,*q,*tail,*temp;int sum;p = la->next;q = Lb->next;tail = La;while (P && Q) {if (p->data.exp<q->data.exp) {tail->next = P;tail = P;p = P->next;} else if (p->data.exp = = q->data.exp) {sum = p->data.coef+q->data.coef;if (sum!=0) {p->data.coef = sum; Tail->next = P;tail = P;p = P->next;temp = Q;q = Q->next;delete temp;} else//if the coefficient is 0, delete the node p,q and point the pointer to the next node {temp = P;p = p->next; Delete temp;temp = Q;q = Q->next;delete temp;}} Else{tail->next = Q;tail = Q;q = Q->next;}} if (p) tail->next = P;elsetail->next = Q;delete Lb;return La;}   The polynomial is multiplied by the single-item, which is Coef x^explinklist Multiply (linklist l,int coef,int exp) { Lnode *p,*q;    linklist Poly = new Lnode;    p = Poly;    Q = l->next;        while (q) {p->next = new Lnode;        p = p->next;        P->data.coef = (Q-&GT;DATA.COEF*COEF);        P->data.exp = (Q-&GT;DATA.EXP+EXP);    Q = q->next;    } p->next = NULL; return Poly;}    Polynomial and polynomial multiply linklist polymultiply (linklist la,linklist lb) {linklist Poly;    Initlist (Poly);    Lnode *p = lb->next;        while (p) {Polyadd (poly,multiply (la,p->data.coef,p->data.exp));    p = p->next; } return Poly;}

Addition and multiplication of 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.