#include<stdio.h> #include<malloc.h> #include<conio.h> struct link{ int coeff; int pow; struct link *next; }; struct link *poly1=NULL,*poly2=NULL,*poly=NULL; void create(struct link *node) { char ch; do { // clrscr(); printf("\n Enter coeff : "); scanf("%d",&node->coeff); printf(" Enter power : "); scanf("%d",&node->pow); node->next=(struct link*)malloc(sizeof(struct link)); node=node->next; node->next=NULL; printf("\n Continue(y/n):"); ch=getch(); } while(ch=='y' || ch=='Y'); } void show(struct link *node) { while(node->next!=NULL) { printf("%dx^%d",node->coeff,node->pow); node=node->next; if(node->next!=NULL) printf(" + "); } } void polyadd(struct link *poly1,struct link *poly2,struct link *poly) { while(poly1->next && poly2->next) { if(poly1->pow>poly2->pow) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } else if(poly1->powpow) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } else { poly->pow=poly1->pow; poly->coeff=poly1->coeff+poly2->coeff; poly1=poly1->next; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } while(poly1->next || poly2->next) { if(poly1->next) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } if(poly2->next) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } } main() { char ch; do{ clrscr(); poly1=(struct link *)malloc(sizeof(struct link)); poly2=(struct link *)malloc(sizeof(struct link)); poly=(struct link *)malloc(sizeof(struct link)); printf("Enter 1st polinomial : "); create(poly1); clrscr(); printf("Enter 2nd polinomial : "); create(poly2); clrscr(); printf("1st polinomial : "); show(poly1); printf("\n2nd polinomial : "); show(poly2); polyadd(poly1,poly2,poly); printf("\n\n Add of Two polynomial : "); show(poly); ch=getch(); } while(ch=='y' || ch=='Y'); }
OUTPUT
Enter 1st polinomial :
Enter coeff : 10
Enter Power : 7
Continue(y/n): y
Enter coeff : 5
Enter Power : 4
Continue(y/n): y
Enter coeff : 3
Enter Power : 2
Continue(y/n): y
Enter coeff : 2
Enter Power : 0
Continue(y/n): n
Enter 2nd polinomial :
Enter coeff : 8
Enter Power : 6
Continue(y/n): y
Enter coeff : 4
Enter Power : 4
Continue(y/n): y
Enter coeff : 3
Enter Power : 3
Continue(y/n): y
Enter coeff : 6
Enter Power : 0
Continue(y/n): n
1st polinomial : 10x^7 + 5x^4 + 3x^3 + 2
2nd polinomial : 8x^6 + 4x^4 + 3x^2 + 6
Add of two polimanial: 10x^7 + 8x^6 + 9x^4 + 3x^3 + 3x^2 + 8