Realize every functions of following ADT Polynomial. ADT Polynomial Object: P(x)
ID: 3816143 • Letter: R
Question
Realize every functions of following ADT Polynomial. ADT Polynomial Object: P(x) = a_i x^e1 + ... + a_n x^e0: set with ordered pair. Here, a_i is Coefficient and e_i is Exponent e_i is integer that is move than or equals with O. Polynomial Zero():: = return polynomial, P(x) = O. Boolean Is Zero (Poly):: = if (poly) return FALS else return TRUE. Coefficient Coef (poly, expon):: = if (expon elementof Poly) return coefficient else return O Exponent Lead Exp (poly):: = the biggest exponent in return poly Polynomial Attach (poly, coef, expon):: if (expon elementof poly) return error else return polynomial poly that is inserted. Polynomial Remove (poly, expon):: = if (expon elementof poly) return poly that the term whose exponent is expon is deleted, else return error. Plolynomial Add (poly 1, poly 2):: = return polynomial poly 1 + poly 2 Polynomial Mult (poly 1, poly 2):: = return polynomial poly 1 poly 2Explanation / Answer
Polynomial Adt can be implemented in c language using structures and linked lists as data structures.We create a c program to display and add two user generated polynomials and then we move on to the function declarations separately.
#include<stdio.h>
#include<malloc.h>
struct Llist
{
int Coef;
int PWR;
struct Llist *next;
};
struct Llist *P1=NULL,*P2=NULL,*P=NULL;
void generate(struct Llist *node)
{
char C1;
do
{
printf(" Enter a Coefficient value:");
scanf("%d",&node->Coef);
printf(" Please enter a PWR value:");
scanf("%d", &node->PWR);
node->next=(struct Llist*)malloc(sizeof(struct Llist));
node=node->next;
node->next=NULL;
printf(" Continue? [Y/N]:");
C1=getch();
}
while(C1=='y' || C1=='Y');
}
void Display(struct Llist *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->Coef,node->PWR);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void Padd(struct Llist *P1,struct Llist *P2,struct Llist *P)
{
while(P1->next && P2->next)
{
If(P1->PWR >P2-> PWR)
{
P -> PWR = P1-> PWR;
P ->Coef= P1-> Coef;
P1= P1->next;
}
else if(P1->PWR<P2->PWR)
{
P->PWR=P2->PWR;
P->Coef=P2->Coef;
P2=P2->next;
}
else
{
P->PWR=P1->PWR;
P->Coef=P1->Coef+P2->Coef;
P1=P1->next;
P2=P2->next;
}
P->next=(struct Llist *)malloc(sizeof(struct Llist));
P=P->next;
P->next=NULL;
}
while(P1->next || P2->next)
{
if(P1->next)
{
P->PWR=P1->PWR;
P->Coef=P1->Coef;
P1=P1->next;
}
if(P2->next)
{
P->PWR=P2->PWR;
P->Coef=P2->Coef;
P2=P2->next;
}
P->next=(struct Llist *)malloc(sizeof(struct Llist));
P=P->next;
P->next=NULL;
}
}
main() // Main method
{
char C1;
do
{
P1=(struct Llist *)malloc(sizeof(struct Llist));
P2=(struct Llist *)malloc(sizeof(struct Llist));
P=(struct Llist *)malloc(sizeof(struct Llist));
printf(" Enter First No:");
generate (P1);
printf(" Enter second No:");
generate (P2);
printf(" First No is:");
Display(P1);
printf(" Second No is:");
Display(P2);
polyadd(P1,P2,P);
printf(" Addition of the Polynomial is:");
Display(P);
printf(" Add 2 more No’s:");
C1=getch();
}
while(C1=='y' || C1=='Y');
}
typedef struct
{
int CoeffArray[MaxDegree+1]; //array used to store coeficients of polynomial whose size is MaxDegree+1
int HighPower; //it is the MaxDegree of polynomial and the HighPower of the exponent in that polynomial
int expArray[HighPower]; //exponent values stored in expArray in order with coefficients
}*Polynomial;
//Next is the zero polynomial method
void Zero(Polynomial Poly)
{
int i;
for(i=0;i<=MaxDegree;i++)
Poly-->CoeffArray[i]=0;
Poly--> HighPower=0;
}
Boolean IsZero(Polynomial Poly)
{
int i;
for(i=0;i<=MaxDegree;i++)
if( (Poly-->CoeffArray[i]=0 ) && ( Poly--> HighPower=0) )
return true;
else
return false;
}
int LeadExponent(Polynomial Poly)
{
int maxpower;
maxpower=Poly--> HighPower;
return maxpower;
}
Polynomial Attach(const Polynomial poly1,int coeff,int expArray[])
{
int i,expchk,coeffchk;
for(i=0;i<=HighDegree;i++)
{
if (expchk=expArray[i] ) && coeffchk=CoeffArray[i])
return error;
else
printf("Polynomial coefficients are ", Poly-->CoeffArray[i] );
printf("Highest power of exponents is",Poly--> HighPower);
printf("<coef,exp> are already attached ");
}
void Add(const Polynomial poly1,const Polynomial poly2,Polynomial polysum)
{
int i;
ZeroPolynomial(PolySum);
PolySum-->HighPower=Max(Poly1-->HighPower,Poly2-->HighPower);
for(i=PolySum-->HighPower;i>=0;i--)
PolySum-->CoeffArray[i]=Poly1-->CoeffArray[i]+Poly2-->CoeffArray[i];
}
void Mult(const Polynomial poly1,const Polynomial poly2, Polynomial PolyProd)
{
int i,j;
ZeroPolynomial(PolyProd);
PolyProd-->HighPower=Poly1-->HighPower+Poly2-->HighPower;
if(PolyProd-->HighPower >MAxDegree)
printf("exceeding MaxSize of Array");
else
for(i=0;i<=Poly1-->HighPower;i++)
for(j=0;j<=Poly2-->HighPower;j++)
PolyProd-->CoeffArray[ i+j ] +=Poly1-->CoeffArray[i]*Poly2-->CoeffArray[j];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.