#include<stdio.h>
#include<stdlib.h>
////structure //////////
struct Poly
{
struct Poly *next;
int exp;
int coeff;
};
/////////////Functions declaration ///////////
struct Poly* CreatePoly();
struct Poly* AddPoly(struct Poly* ,struct Poly*);
void DisplayPoly(struct Poly*);
/////Main /////////////
int main()
{
while(1)
{
system("clear");
struct Poly *P1,*P2,*P3;
int x;
char s[5];
printf("\n\n\n-------------------Polynomial Addition ----------------\n\n");
printf(" 1) Enter First Polynomial Details\n");
printf(" 2) Enter Second Polynomial Details\n");
printf(" 3) Exit \n ");
printf("--------------------------------------------------------\n");
printf("Enter the chioce :");
scanf("%d",&x);
switch(x)
{
case 1:
{
P1 = CreatePoly();
break;
}
case 2:
{
P2 = CreatePoly();
P3 = AddPoly(P1,P2);
system("clear");
printf("\n\n\n\n\nFirst Polynomial : ");
DisplayPoly(P1);
printf("\n\nSecond Polynomial : ");
DisplayPoly(P2);
printf("\n--------------------------------------------------");
printf("\n\nResult : ");
DisplayPoly(P3);
printf("\n\n\n\n\n\n\nPress Any Key to return :");
gets(s);
gets(s);
break;
}
case 3:
{
return 0;
}
default:
{
printf("Enter valid choice\n");
printf("\n\n\n\n\n\n\nPress Any Key to return :");
gets(s);
gets(s);
}
}
}
}
////////Function Definition /////////////////
void DisplayPoly(struct Poly* P)
{
while(P != NULL)
{
printf("+(%d)x^%d ",P->coeff,P->exp);
P = P->next;
}
}
struct Poly* CreatePoly()
{
int count;
struct Poly* start=NULL,*new=NULL;
struct Poly* temp=NULL;
printf("\nEnter the number of Polynomial terms: ");
scanf("%d",&count);
while(count--)
{
new = malloc( sizeof(struct Poly) );
printf("\nTerm %d:\nCoefficient : ",count+1);
scanf("%d",&new->coeff);
printf("Exponent ");
scanf("%d",&new->exp);
if(start == NULL || start->exp < new->exp)
{
new->next = start;
start = new;
}
else
{
temp = start;
while(temp->next != NULL && temp->next->exp > new->exp)
temp = temp->next;
new->next = temp->next;
temp->next = new;
}
}
return start;
}
struct Poly* AddPoly( struct Poly* P1, struct Poly* P2)
{
struct Poly* P3_start = NULL;
struct Poly* P3,*temp;
while(P1 != NULL && P2 != NULL )
{
temp = malloc( sizeof(struct Poly) );
temp->next=NULL;
if(P3_start == NULL)
{
P3_start = temp;
P3 = temp;
}
else
{
P3->next = temp;
P3 = P3->next;
}
if( P1->exp > P2->exp)
{
temp->exp = P1->exp;
temp->coeff = P1->coeff;
P1 = P1->next;
}
else if(P1->exp < P2->exp)
{
temp->exp = P2->exp;
temp->coeff = P2->coeff;
P2 = P2->next;
}
else if(P1->exp == P2->exp)
{
temp->exp = P1->exp;
temp->coeff = P1->coeff + P2->coeff;
P1 = P1->next;
P2 = P2->next;
}
}
while(P1 != NULL) {
temp = (struct Poly*)malloc( sizeof(struct Poly) );
temp->coeff = P1->coeff;
temp->exp = P1->exp;
P1 = P1->next;
P3->next = temp;
P3 = P3->next;
}
while(P2 != NULL) {
temp = (struct Poly*)malloc( sizeof(struct Poly) );
temp->coeff = P2->coeff;
temp->exp = P2->exp;
P2 = P2->next;
P3->next = temp;
P3 = P3->next;
}
return P3_start;
}
#include<stdlib.h>
////structure //////////
struct Poly
{
struct Poly *next;
int exp;
int coeff;
};
/////////////Functions declaration ///////////
struct Poly* CreatePoly();
struct Poly* AddPoly(struct Poly* ,struct Poly*);
void DisplayPoly(struct Poly*);
/////Main /////////////
int main()
{
while(1)
{
system("clear");
struct Poly *P1,*P2,*P3;
int x;
char s[5];
printf("\n\n\n-------------------Polynomial Addition ----------------\n\n");
printf(" 1) Enter First Polynomial Details\n");
printf(" 2) Enter Second Polynomial Details\n");
printf(" 3) Exit \n ");
printf("--------------------------------------------------------\n");
printf("Enter the chioce :");
scanf("%d",&x);
switch(x)
{
case 1:
{
P1 = CreatePoly();
break;
}
case 2:
{
P2 = CreatePoly();
P3 = AddPoly(P1,P2);
system("clear");
printf("\n\n\n\n\nFirst Polynomial : ");
DisplayPoly(P1);
printf("\n\nSecond Polynomial : ");
DisplayPoly(P2);
printf("\n--------------------------------------------------");
printf("\n\nResult : ");
DisplayPoly(P3);
printf("\n\n\n\n\n\n\nPress Any Key to return :");
gets(s);
gets(s);
break;
}
case 3:
{
return 0;
}
default:
{
printf("Enter valid choice\n");
printf("\n\n\n\n\n\n\nPress Any Key to return :");
gets(s);
gets(s);
}
}
}
}
////////Function Definition /////////////////
void DisplayPoly(struct Poly* P)
{
while(P != NULL)
{
printf("+(%d)x^%d ",P->coeff,P->exp);
P = P->next;
}
}
struct Poly* CreatePoly()
{
int count;
struct Poly* start=NULL,*new=NULL;
struct Poly* temp=NULL;
printf("\nEnter the number of Polynomial terms: ");
scanf("%d",&count);
while(count--)
{
new = malloc( sizeof(struct Poly) );
printf("\nTerm %d:\nCoefficient : ",count+1);
scanf("%d",&new->coeff);
printf("Exponent ");
scanf("%d",&new->exp);
if(start == NULL || start->exp < new->exp)
{
new->next = start;
start = new;
}
else
{
temp = start;
while(temp->next != NULL && temp->next->exp > new->exp)
temp = temp->next;
new->next = temp->next;
temp->next = new;
}
}
return start;
}
struct Poly* AddPoly( struct Poly* P1, struct Poly* P2)
{
struct Poly* P3_start = NULL;
struct Poly* P3,*temp;
while(P1 != NULL && P2 != NULL )
{
temp = malloc( sizeof(struct Poly) );
temp->next=NULL;
if(P3_start == NULL)
{
P3_start = temp;
P3 = temp;
}
else
{
P3->next = temp;
P3 = P3->next;
}
if( P1->exp > P2->exp)
{
temp->exp = P1->exp;
temp->coeff = P1->coeff;
P1 = P1->next;
}
else if(P1->exp < P2->exp)
{
temp->exp = P2->exp;
temp->coeff = P2->coeff;
P2 = P2->next;
}
else if(P1->exp == P2->exp)
{
temp->exp = P1->exp;
temp->coeff = P1->coeff + P2->coeff;
P1 = P1->next;
P2 = P2->next;
}
}
while(P1 != NULL) {
temp = (struct Poly*)malloc( sizeof(struct Poly) );
temp->coeff = P1->coeff;
temp->exp = P1->exp;
P1 = P1->next;
P3->next = temp;
P3 = P3->next;
}
while(P2 != NULL) {
temp = (struct Poly*)malloc( sizeof(struct Poly) );
temp->coeff = P2->coeff;
temp->exp = P2->exp;
P2 = P2->next;
P3->next = temp;
P3 = P3->next;
}
return P3_start;
}
No comments:
Post a Comment