One-element polynomial multiplication

Source: Internet
Author: User

/*** @brief the product of two unary polynomial P (x) and Q (x) p (x) * Q (x) for two unary polynomial P (x) and q (x) = P (x) (x) * q* @author hxm*@d Ate 2016/3/26** @note requirements: 1. Randomly input two polynomial p (x) and Q (x) through the keyboard 2. The output should have P (x) and Q (x) and their product p (x) *q (x) requireme Nts:1. The random input through the keyboard the polynomial P (x) and Q (x) 2. The output should has P and Q (x) (x) and their product P * Q (x) (x) */#include <stdio.h> #include <stdlib.h> #in    clude<windows.h> #include <malloc.h>typedef struct polynode{float coef;    int expon; struct Polynode *next;}    Pnode;void face () {System ("color E4");    printf ("-------------------------------------------------------------------\ n");    printf ("*********two polynomial Add, muilt, plus*******\n");    printf ("------chose one operation to start------\ n");    printf ("0.exit\n");    printf ("1.add-polynomial:\n");    printf ("2.sub-polynomial: \ n"); printf ("3.multiply-polynomial:\n");} void Insertpoly (Pnode *head, Pnode *input) {pnode *p, *q;    int flag=0;   P=head;    P is the prior link node if (p->next==null) {p->next=input;        } else{q=p->next; while (flag==0) {if (input->expon<q->expon) {if (q->next==null) {q-&                    Gt;next=input;                flag=1;                    } else{p=q;                q=p->next;                    }} else{if (input->expon>q->expon) {input->next=q;                    p->next=input;                flag=1;                    } else{q->coef=q->coef+input->coef;                    flag=1;                    Free (input);                        if (q->coef==0) {p->next=q->next;                    Free (q); }}}}}}pnode *creatpolY (char pch) {Pnode *head, *input;    float x;    int y;    Head= (Pnode *) malloc (sizeof (Pnode));    head->next=null; printf ("Please enter polynomial%c: (Style is:coef expon;    Input 0 0 to end) \ n ", PCH);    scanf ("%f%d", &x, &y);        while (x!=0) {input= (Pnode *) malloc (sizeof (Pnode));        input->coef=x;        input->expon=y;        input->next=null;        Insertpoly (head, input);        printf ("Please enter the next term of polynomial%c: (input 0 0 to end) \ n", PCH);    scanf ("%f%d", &x, &y); } return head;    Pnode *addpoly (Pnode *head, Pnode *prior) {Pnode *input;    int flag=0;        while (flag==0) {if (prior->next==null) flag=1;            else{prior=prior->next;            input= (Pnode *) malloc (sizeof (Pnode));            input->coef=prior->coef;            input->expon=prior->expon;            input->next=null;        Insertpoly (head, input); }} return head;}    Pnode *subpoly (Pnode *head, Pnode *prior) {Pnode *input;    int flag=0;        while (flag==0) {if (prior->next==null) {flag=1;            } else{prior=prior->next;            input= (Pnode *) malloc (sizeof (Pnode));            input->coef=0-prior->coef;            input->expon=prior->expon;            input->next=null;        Insertpoly (head, input); }} return head;    Pnode *multpoly (Pnode *head1, Pnode *head2) {Pnode *input, *r, *p;    int flag=0;    R= (Pnode *) malloc (sizeof (Pnode));    r->next=null;    head1=head1->next;    P=head2;            while (flag==0) {if (p->next==null) {p=head2;            head1=head1->next;        Continue            } if (head1==null) {flag=1;        Continue        } p=p->next;        input= (Pnode *) malloc (sizeof (Pnode));        INPUT-&GT;COEF=P-&GT;COEF * head1->coef;        Input->expon=p->expon + head1->expon; Input->next=null;    Insertpoly (R,input); } return R;}    void Displaypoly (Pnode *head) {Pnode *p;    int flag=0;    p=head->next;        if (head->next==null) {printf ("0\n");    return;         } while (flag==0) {if (p->coef>0 && head->next!=p) printf ("+");         if (p->coef==1);         else if (p->coef==-1) printf ("-");         else printf ("%f", P->coef);         if (p->expon!=0) printf ("x^%d", p->expon); else if ((p->coef==1) | | |            (P->coef==-1))         printf ("1");         if (p->next==null) flag=1;    else p=p->next; } printf ("\ n");}  int main () {Pnode *p, *q;  int choice=-1;  Face ();    while (choice!=0) {scanf ("%d", &choice);    Switch (choice) {case 0:break;            Case 1: {printf ("Your Choice Is:add-polynomial:\n");      P=creatpoly (' P '); Input polynomial printf("P (x) =");            Displaypoly (P);            Q=creatpoly (' Q ');            printf ("Q (X) =");            Displaypoly (q);            printf ("R (x) =p (x) +q (x) =");            P=addpoly (P, q);            Displaypoly (P);            Choice=-1;            Face ();        Break            } Case 2: {printf ("Your choice is:sub-polynomial:\n");      P=creatpoly (' P ');            Input polynomial printf ("P (x) =");            Displaypoly (P);            Q=creatpoly (' Q ');            printf ("Q (X) =");            Displaypoly (q);            printf ("R (x) =p (x)-Q (x) =");            P=subpoly (P, q);            Displaypoly (P);            Choice=-1;            Face ();        Break            } Case 3: {printf ("Your choice is:multiply-polynomial:\n");      P=creatpoly (' P ');            Input polynomial printf ("P (x) =");            Displaypoly (P);            Q=creatpoly (' Q ');            printf ("Q (X) =");           Displaypoly (q); printf ("R (x) =p (x) *q (x) =");            P=multpoly (P, q);            Displaypoly (P);            Choice=-1;            Face ();        Break } default: {printf ("Error input!!            Please choose again~~~\n ");            Face ();        Break }    }  }}

The results are as follows:

One-element polynomial multiplication

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.