Data structure course design summation and multiplication of unary polynomial

Source: Internet
Author: User
Tags printf

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct POLYNODE
{
int Coef; coefficients of the polynomial
int exp; Index
struct Polynode *next;
}node;

Node *create ()//using the tail interpolation method to establish a linked list of unary polynomial
{
Node *h,*r,*s;
int c,e;
H= (node*) malloc (sizeof (node));
R=h;
printf ("Coef:");
scanf ("%d", &c);
printf ("Exp:");
scanf ("%d", &e);
while (c!=0)//input factor is 0 o'clock, the input of the polynomial ends
{
S= (node*) malloc (sizeof (node));
s->coef=c;
s->exp=e;
r->next=s;
R=s;
printf ("Coef:");
scanf ("%d", &c);
printf ("Exp:");
scanf ("%d", &e);
}
r->next=null;
return (h);
}

void Print (node *p)//Output function, printing out a unary polynomial
{
while (P->next!=null)
{
p=p->next;
printf ("%d*x^%d", p->coef,p->exp);

}
}

Void Polyadd (node *ha, node *HB)//Unary polynomial addition function for adding two polynomial, then storing and polynomial in the polynomial ha, and deleting the polynomial HB
{
Node *p,*q,*pre,*temp;
int sum;
p=ha->next;
q=hb->next;
Pre=ha;
while (p!=null&&q!=null)
{
if (p->exp<q->exp)
{
pre->next=p;
pre=pre->next;
p=p->next;
}
Else if (p->exp==q->exp)
{
sum=p->coef+q->coef;
if (sum!=0)
{
p->coef=sum;
pre->next=p;pre=pre->next;p=p->next;
Temp=q;q=q->next;free (temp);
}
Else/If the coefficients and zeros are removed, the node p and Q are deleted, and the pointer points to the next node
{
Temp=p->next;free (p);p =temp;
Temp=q->next;free (q); q=temp;
}
}
Else
{
pre->next=q;
pre=pre->next;
q=q->next;
}
}
if (p!=null)//Add the remaining nodes in the polynomial A to the and polynomial
pre->next=p;
Else
pre->next=q;
}

void Multipoly (node *ha,node *HB)
{node *p,*q,*n,*m;
p=ha->next;
N= (node*) malloc (sizeof (node));
n->next=null;
while (P!=null)
{m= (node*) malloc (sizeof (node));
for (Q=hb->next;q;q=q->next)
{m->coef=p->coef*q->coef;
m->exp=p->exp+q->exp;
m->next=null;
}
p=p->next;
Polyadd (N,M);
}
printf (the product of the polynomial is:/n);
printf ("/n");
}


void Main ()
{
Node *HA,*HB;
printf ("Please enter the coefficients of the polynomial ha with the exponent:/n");
Ha=create ();
Print (HA);
printf ("/n");
printf ("Please enter the coefficient of the polynomial HB with the exponent:/n");
Hb=create ();
Print (HB);
printf ("/n");
printf ("and is:/n" for polynomial);
Polyadd (HA,HB);

Print (HA);
printf ("/n");

Multipoly (HA,HB);
}

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.