Tuesday, March 31, 2015

Addition of two polynomials

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct poly
{
 float constant ;
float expo;
struct poly * next;
};
void append(struct poly **q)
{
   struct poly *temp;
    temp=*q;
   if(temp==NULL)
   {
     *q=(struct poly *)malloc(sizeof(struct poly));
     printf("\nenter the constant");
     scanf("%d",&(*q)->constant);
     printf("\nenter the expo");
     scanf("%d",&(*q)->expo);
     (*q)->next=NULL;
    }
else
{
 while(temp->next!=NULL)
   temp=temp->next;

   temp->next=(struct poly *)malloc(sizeof(struct poly));
    temp=temp->next;

   printf("\nenter the data");
     scanf("%d",&temp->data);
   printf("\nenter the expo");
    scanf("%d",&temp->expo);
  temp->next=NULL;
 }
}


void add_poly(struct node **q,struct poly **q2,struct poly *q3)
{
 struct poly *temp,*t,*t1;
temp=*q;
t=*q2;
t1=*q3;

 while(temp->next!=NULL||t->next!=NULL)
 {
   if(temp->expo=t->expo)
    {
       t1->data=temp->constant+t->constant;
       t1->expo=temp->expo;
     }
  }
}

void display(struct node *q)
{
  struct node *temp;
  temp=q;
    while(temp!=NULL)
    {
      printf(" %d^%d",temp->data);
       if(temp->expo!=0)
        printf("+");
      temp=temp->next;
     }


}

void main()
{
 struct poly *start,*start2;
 start=NULL; //empty list1
start2=NULL; //empty list 2
start3=NULL; //empty list 3

 printf("\nenter theequation : ")
 append(&start);
 append(&start);
 append(&start);

printf("\now the equationis :");
display(start);

printf("\nenter the second equation :");
append(&start2);
 append(&start2);
 append(&start2);

printf('\now the list is :')
display(start2);

printf("\nenter the Third equation :");
append(&start3);
 append(&start3);
 append(&start3);


add_poly(&start,%start2,&start3);



printf('\now the equation is :')

display(start3);


}

No comments:

Post a Comment