C-Language polynomial addition

Source: Internet
Author: User

#include <stdio.h>

#include <stdlib.h>

#define ERROR-1

typedef struct list{

Double Coef;

int expn;

struct list *next;

}polyn;


polyn* Creatpolyn (polyn*p,int m)

{

p= (polyn*) malloc (sizeof (Polyn));

p->next=null;

while (m--) {

Polyn *temp= (polyn*) malloc (sizeof (Polyn));

scanf ("%lf%d", &TEMP->COEF,&TEMP->EXPN);

temp->next=p->next;

p->next=temp;

}

return p;

}


int cmp (int a,int b) {

if (a>b) return 1;

else if (a==b) return 0;

else return-1;

}



void Delfirst (POLYN*P,POLYN*Q)

{

p->next=q->next;

}


void Insfirst (POLYN*P,POLYN*Q)

{

q->next=p->next;

p->next=q;

}


void Append (Polyn*p,polyn*q)

{

Polyn *temp=p;

while (1) {

temp=temp->next;

if (!temp->next) {

temp->next=q;

Break

}

}

}


void Addpolyn (POLYN*PA,POLYN*PB)

{int A, b;

Double sum=0;

Polyn *ha=pa;

Polyn *HB=PB;

Polyn *qa=ha->next;

Polyn *qb=hb->next;

while (QA&&QB) {

a=qa->expn;

b=qb->expn;

Switch (CMP (A, B)) {

Case-1:ha=qa;

qa=qa->next;

Break

Case 0:sum=qa->coef+qb->coef;

if (sum!=0.0) {

qa->coef=sum;

Ha=qa;

}

else{

Delfirst (HA,QA);

Free (QA);

}

Delfirst (HB,QB);

Free (QB);

qb=hb->next;

qa=ha->next;

Break

Case 1:delfirst (HB,QB);

Insfirst (HA,QB);

qb=hb->next;

ha=ha->next;

Break

}

}

if (!pb->next) append (PA,QB);

Free (HB);

}


int main (void) {

int NUMBEROFPA,NUMBEROFPB;

Polyn *pa=null,*pb=null,*p=null;

scanf ("%d%d", &NUMBEROFPA,&NUMBEROFPB);

Pa=creatpolyn (PA,NUMBEROFPA);

printf ("List A is complete \ n");

p=pa->next;

while (P!=null) {

printf ("%.2f%d\n", P->COEF,P->EXPN);

p=p->next;

}

Pb=creatpolyn (PB,NUMBEROFPB);

printf ("List B is set \ n");

Addpolyn (PA,PB);

printf ("addition complete \ n");

while ((Pa->next)!=null) {

pa=pa->next;

printf ("%.2f%d\t", PA->COEF,PA->EXPN);

}

return 0;

}

Pointer passing is also a value pass, in which changes to the pointer are returned.

C-Language polynomial addition

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.