In: Computer Science
Please use C language and use link list to do this program.
This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it.
I need you please to test it to see if it work with these two fraction polynomials
1- Left Poly Pointer:
1/1x2 + 3/4x + 5/12
2-Right Poly Pointer:
1/1x4 – 3/7x2 + 4/9x + 2/11
AND AFTER ADDING
3- Resulting Poly Pointer:
1/1x4 + 4/7x2 + 43/36x + 79/132
And if the user choice to Multiple
3- Resulting Poly Pointer:
1/1x6 + 3/4x5 – 1/84x4 + 31/252x3 + 871/924x2 + 191/594x + 5/66
C program to Add and multiply two polynomials using Link List
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
int coeff_n;
int coeff_d;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\n\nenter coeff numerator :");
scanf("%d",&node->coeff_n);
printf("\n\nenter coeff denominator :");
scanf("%d",&node->coeff_d);
printf("\nenter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\ncontinue(y/n):");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("(%d/%d)x^%d",node->coeff_n,node->coeff_d,node->pow);
node=node->next;
if(node->next!=NULL)
printf(" + ");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link
*poly)
{
int a, b,c,d,x,y,i,gcd;
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff_n=poly1->coeff_n;
poly->coeff_d=poly1->coeff_d;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff_n=poly2->coeff_n;
poly->coeff_d=poly2->coeff_d;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
a=poly1->coeff_n;
b=poly1->coeff_d;
c=poly2->coeff_n;
d=poly2->coeff_d;
x=(a*d)+(b*c); //numerator
y=b*d; //denominator
// Trick part. Reduce it to the simplest form by using gcd.
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}
poly->coeff_n=x/gcd;
poly->coeff_d=y/gcd;
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_n=poly1->coeff_n;
poly->coeff_d=poly1->coeff_d;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff_n=poly2->coeff_n;
poly->coeff_d=poly2->coeff_d;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
void polymul(struct link *n1, struct link *n2, struct link
*n)
{
int x,y,i,gcd;
struct link * n2beg=n2;
while (n1)
{
struct link * temp=(struct link *)malloc(sizeof(struct
link));
temp->next=NULL;
n2=n2beg;
while (n2)
{
x= n1->coeff_n * n2->coeff_n;
y= n1->coeff_d * n2->coeff_d;
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}
temp->coeff_n = x/gcd;
temp->coeff_d = y/gcd;
temp->pow = n1->pow + n2->pow;
n2 = n2->next;
temp->next=(struct link *)malloc(sizeof(struct link));
temp=temp->next;
temp->next=NULL;
}
polyadd(temp,n,n);
n1 = n1->next;
free(temp);
}
}
int main()
{
int op;
char ch;
do{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\n\nWhat do you want to
do?\n1.Addition\n2.Multiplication\n0.Exit\nEnter your
choice:");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n\nenter 1st polynomial:");
create(poly1);
printf("\n\nenter 2nd polynomial:");
create(poly2);
printf("\n1st Polynomial:\t");
show(poly1);
printf("\n2nd Polynomial:\t");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\nAdded polynomial:\t");
show(poly);
break;
case 2:
printf("\n\nenter 1st polynomial:");
create(poly1);
printf("\n\nenter 2nd polynomial:");
create(poly2);
printf("\n\n1st Polynomial:\t");
show(poly1);
printf("\n\n2nd Polynomial:\t");
show(poly2);
polymul(poly1,poly2,poly);
printf("\n\nMultiplied polynomial:\t");
show(poly);
break;
}
}
while(op);
return 0;
}