Two unary polynomial add-ons

Source: Internet
Author: User

The results of the program run as follows:
<span style= "font-family:arial, Helvetica, Sans-serif;" ><span style= "FONT-SIZE:18PX;" ># include<stdio.h></span>
# include<stdlib.h>struct Node{int exp;float coef;struct node *next;/* point to struct pointer */};typedef struct node ListNode;  ListNode *createpoly ()//create a polynomial linked list {ListNode *h=null,*p,*q=null;  int e;  float C;  printf ("Please enter coefficients and indices:");  scanf ("%f,%d", &c,&e); while (e!=0| |  c!=0) {p= (listnode*) malloc (sizeof (ListNode));  p->coef=c;  p->exp=e;  p->next=null;  if (h==null) h=p;  else q->next=p;  Q=p;  printf ("Please enter coefficients and indices:");  scanf ("%f,%d", &c,&e); } return h;} void Disppoly (ListNode *h)/* Output polynomial */{listnode *p;p=h;while (p!=null) {if (p->exp==0) printf ("%.2f", P->coef); elseprintf ("%fx^%d", P->coef,p->exp);p =p->next;if (p!=null) printf ("+");} printf ("\ n");  } ListNode *addpoly (ListNode *h1,listnode *h2)/* Add two polynomial */{ListNode *p,*r=null,*s1,*s2,*s=null; float c; int e; s1=h1; S2=H2; while (S1!=null&&s2!=null) {if (s1->exp==s2->exp) {c=s1->coef+s2->coef; e=s1->exp; s1=s1-> Next s2=s2->next; } else if (s1->exp>s2->exp) {c=s1->coef; E=s1->exp; s1=s1->next; } else {c=s2->coef; e=s2->exp; s2=s2->next;} if (c!=0) {p= (listnode*) malloc (sizeof (ListNode)); p->coef=c; p->exp=e;p->next=null; if (s==null) s=p; else r->next=p; R=p; }} while (S1!=null) {c=s1->coef; e=s1->exp; s1=s1->next; if (c!=0) {p= (listnode*) malloc (sizeof (ListNode)); p-& Gt;coef=c; p->exp=e;p->next=null; if (s==null) s=p; else r->next=p; R=p; }} while (S2!=null) {c=s2->coef; e=s2->exp; s2=s2->next; if (c!=0) {p= (listnode*) malloc (sizeof (ListNode)); p-& Gt;coef=c; p->exp=e;p->next=null; if (s==null) s=p; else r->next=p; R=p;  }} return S; } void Deletepoly (ListNode *h)//release polynomial occupies memory unit {listnode *p,*r=h;while (r!=null) {p=r->next;free (r); r=p;}} int main () {ListNode *head1,*head2,*head;printf ("Create first polynomial: \ n"), Head1=createpoly ();p rintf ("Create a second polynomial: \ n"); head2=  Createpoly ();p rintf ("add two polynomial: \ n"), Head=addpoly (head1,head2);d Isppoly (head);d Eletepoly (head);} </span>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Two unary polynomial add-ons

Related Article

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.