You are on page 1of 9

//addition of two polynomials

#include <stdio.h>
#include <conio.h>
typedef struct
{ int coeff[10];
int power[10];
} input;
int main()
{ input poly[10];
int l,m=1;
char strA[2];
clrscr();
for(l=1;l<=2;l++)
{ printf("Enter polynomial %d : ",l);
m=1;
while(m!=0)
{ scanf("%d",&poly[l].coeff[m]);
if(poly[l].coeff[m]==0)
{ m=0;
poly[l].power[m]=0;
}
else
{ printf("x^");
scanf("%d",&poly[l].power[m]);
m++;
}
}
}

printf("To perform addition press '+'\n");


scanf("%s",&strA);
if(strA[0]=='+')
{ for(l=1;l<=1;l++)
{ m=1;
while(poly[l].coeff[m]!=0)
{ if(poly[l].power[m]==poly[l+1].power[m])
{ poly[3].coeff[m]=poly[l].coeff[m]+poly[l+1].coeff[m];
poly[3].power[m]=poly[l].power[m];
}
else
{ poly[3].coeff[m]=poly[l].coeff[m];
poly[3].power[m]=poly[l].power[m];
}
m++;
}
}
}

printf("\n\n\n\nInput was: \n\n");


for (l=1;l<=2;l++)
{ m=1;
printf("Polynomial %d : ",l);
while(poly[l].coeff[m]!=0)
{ if(poly[l].coeff[m]>0)
{printf("+%d",poly[l].coeff[m]);
}
else
{printf("%d",poly[l].coeff[m]);
}
printf("x^");
printf("%d",poly[l].power[m]);
m++;
}
printf("\n");
}
printf("\n\n\n\nOnput is: ");
m=1;
while(poly[3].coeff[m]!=0)
{ if(poly[3].coeff[m]>0)
{printf("+%d",poly[3].coeff[m]);
}
else
{printf("%d",poly[3].coeff[m]);
}
printf("x^");
printf("%d",poly[3].power[m]);
m++;
}

getch();
return 0;
}
================================================================================
=============
/* Representation of Polynomials using arrays */
/* Addition of two Polynomials */
#include <stdio.h>
#define MAX 10
int main(){
int poly1[MAX]={0},poly2[MAX]={0},poly3[MAX]={0};
int i,deg1,deg2,deg3;
printf( nEnter degree of first polynomial? );
scanf( %d ,&deg1);
printf( nEnter degree of second polynomial? );
scanf( %d ,&deg2);
printf( nFor first polynomial: );
for(i=0;i<deg1;i++){

printf( nEnter Coefficient for Exponent %d> ,i);


scanf( %d ,&poly1[i]);
}
printf( nFor second polynomial: );
for(i=0;i<deg2;i++){
printf( nEnter Coefficient for Exponent %d> ,i);
scanf( %d ,&poly2[i]);
}
printf( nGenerating sum ! );
printf( nPress any key to continue );
deg3 = (deg1>deg2)?deg1:deg2;
for(i=0;i<deg3;i++)
poly3[i] = poly1[i] + poly2[i];
printf( nnAddition Result:nn );
for(i=deg1-1;i>=0;i )
printf( (%dx^%d)+ ,poly1[i],i);
printf( b n );
for(i=deg2-1;i>=0;i )
printf( (%dx^%d)+ ,poly2[i],i);
printf( b n );
for(i=deg3-1;i>=0;i )
printf( (%dx^%d)+ ,poly3[i],i);
printf( b n );
}
================================================================================
=============
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
void main()
{
struct poly
{
int coff;
int expo;
struct poly *link;
}*temp1,*start1,*temp2,*start2,*start3,*temp3;
int n,i,z=1,num;
char c;

clrscr();
start1=NULL;
printf("\tPOLYNOMIAL CREATION IN LINK LIST AND ADDITION\n\n");
while(1)
{
if(start1==NULL)
{
temp1=(struct poly*)malloc(sizeof(struct poly));
printf("Enter Coefficient and Exponent for Node %d\n",z);
scanf("%d %d",&temp1->coff,&temp1->expo);
start1=temp1;
temp1->link=NULL;
}
else
{
temp1->link=(struct poly*)malloc(sizeof(struct poly));
temp1=temp1->link;
printf("Enter Coefficient and exponent for Node %d\n",z);
scanf("%d %d",&temp1->coff,&temp1->expo);
}
z++;
printf("Do you want to create another node\n");
fflush(stdin);
scanf("%c",&c);
if(c!='y')
{
temp1->link=NULL;
break;
}
}

start2=NULL;
temp2=NULL;
z=1;
while(1)
{
if(start2==NULL)
{
temp2=(struct poly*)malloc(sizeof(struct poly));
printf("Enter Coefficient and exponent for Node %d\n",z);
scanf("%d %d",&temp2->coff,&temp2->expo);
start2=temp2;
temp2->link=NULL;
}
else
{
temp2->link=(struct poly*)malloc(sizeof(struct poly));
temp2=temp2->link;
printf("Enter Coefficient and exponent for Node %d\n",z);
scanf("%d %d",&temp2->coff,&temp2->expo);
}
z++;
printf("Do you want to create another node\n");
fflush(stdin);
scanf("%c",&c);
if(c!='y')
{
temp2->link=NULL;
break;
}
}

// TARVERSING
temp1=NULL;
temp2=NULL;
temp1=start1;
temp2=start2;
printf("Traversal of Polynomial Linked List 1\n");
while(temp1!=NULL)
{
printf("%2dx^%d+",temp1->coff,temp1->expo);
temp1=temp1->link;
}
printf("\b\b");
printf("\nTraversal of Polynomial Linked List 2\n");
while(temp2!=NULL)
{
printf("%2dx^%d+",temp2->coff,temp2->expo);
temp2=temp2->link;
}
printf("\b\b");
// addition
temp1=NULL;
temp2=NULL;
temp1=start1;
temp2=start2;
temp3=NULL;
start3=NULL;
while(1)
{
if((temp1!=NULL)||(temp2!=NULL))
{

if(start3==NULL)
{
if((temp1->expo)==(temp2->expo))
{
temp3=(struct poly*)malloc(sizeof(struct poly));
temp3->coff= (temp1->coff) + (temp2->coff);
temp3->expo=temp1->expo;
start3=temp3;
temp1=temp1->link;
temp2=temp2->link;
temp3->link=NULL;
}
else if(temp1->expo>temp2->expo)
{
temp3=(struct poly*)malloc(sizeof(struct poly));
temp3->coff=temp1->coff;
temp3->expo=temp1->expo;
start3=temp3;
temp1=temp1->link;
temp3->link=NULL;
}
else
{
temp3=(struct poly*)malloc(sizeof(struct poly));
temp3->coff=temp2->coff;
temp3->expo=temp2->expo;
start3=temp3;
temp2=temp2->link;
temp3->link=NULL;
}

}
else
{
if(temp1->expo==temp2->expo)
{
temp3->link=(struct poly*)malloc(sizeof(struct poly));
temp3=temp3->link;
temp3->coff= (temp1->coff) + (temp2->coff);
temp3->expo=temp1->expo;
temp1=temp1->link;
temp2=temp2->link;
}
else if(temp1->expo>temp2->expo)
{
temp3->link=(struct poly*)malloc(sizeof(struct poly));
temp3=temp3->link;
temp3->coff=temp1->coff;
temp3->expo=temp1->expo;
temp1=temp1->link;
}
else
{
temp3->link=(struct poly*)malloc(sizeof(struct poly));
temp3=temp3->link;
temp3->coff=temp2->coff;
temp3->expo=temp2->expo;
temp2=temp2->link;
}
}
}

else
break;
}
temp3->link=NULL;
//traversing temp3
temp3=NULL;
temp3=start3;
printf("\nTraversal of Polynomial Linked List after adding Temp1 & Temp2\n");
while(temp3!=NULL)
{
printf("%2dx^%d + ",temp3->coff,temp3->expo);
temp3=temp3->link;
} printf("\b \b");
getch();
}
================================================================================
=============

You might also like