Data Structures - f

OPEN ENDED DESIGN BASED PROJECT
Imagine an effective dynamic structure for storing polynomials. Write operations for addition,
subtraction, and multiplication of polynomials. I/O description: Input:a. p1=3x^7+5x^6+22.5x^5+0.35x-2
b. p2=0.25x^3+.33x^2-.01
c. p1+p2
d. p1-p2
e. p1*p2
SOLUTION
PROBLEM:Performing mathematical operations on polynomials is a complex task as it includes taking
into consideration both the power and coefficient of the terms simultaneously. We have to
develop a program that can accept two polynomials of single variable and perform addition,
subtraction and multiplication on them, thus giving third polynomial as a result.
SOLUTION:The above stated problem can be accomplished by implementing linked list. Linked list as a
data structure can be implemented through C programming language. The two operand
polynomials and the resultant polynomial can be easily generated through linked list.
PROPOSED MODEL:In our solution, we have tried to achieve the target by creating multiple functions and using
the concept of linked list. They are summarized as:


Linked list structures:a. Term:- This is used to store the “power” and “coefficient” of a particular term.
b. Polynomial:- It contains the structure “term” and another variable “n” for
generating the polynomial expression.
Functions:1. init( ):- To initialize a linked list.
2. read( ):- To input the values provided by user for power and coefficient.
3. print( ):- To display the resultant linked list.
4. add( ):- To take two polynomial as its arguments and generate their sum.
5. subtr( ):- To take two polynomial as its arguments and generate their
difference.
6. multiply( ):- To take two polynomial as its arguments and generate their
product.
7. insert( ):- To add terms to the specified linked list.
8. main( ):- To generate the menu-driven interface for the user to enter his choice
to perform any operation and thus carrying out the specified operation using
switch case.
SOFTWARE USED:Turbo C++
SOURCE CODE:#include<math.h>
#include<stdio.h>
#include<conio.h>
typedef struct term
{
int power;
float coeff;
}term;
typedef struct polynomial
{
term a[20];
int n;
}polynomial;
void init(polynomial *ptr);
void read(polynomial *ptr);
void print(polynomial *ptr);
polynomial add(polynomial *p1,polynomial *p2);
polynomial subtr(polynomial *p1,polynomial *p2);
polynomial multiply(polynomial *p1,polynomial *p2);
void insert(polynomial *,term);
void main()
{
polynomial P1,P2,P3;
int option;
float x,value;
do
{
clrscr();
printf("\n1 : Create 1'st polynomial(p1)");
printf("\n2 : Create 2'nd polynomial(p2)");
printf("\n3 : Add polynomials");
printf("\n4 : Multiply polynomials");
printf("\n5 : Subtract polynomials");
printf("\n6 : Quit");
printf("\nEnter your choice :");
scanf("%d",&option);
switch(option)
{
case 1:read(&P1);
getch();
break;
case 2:read(&P2);
getch();
break;
case 3:P3=add(&P1,&P2);
printf("\n\n1'st polynomial : ");
print(&P1);
printf("\n\n2'nd polynomial : ");
print(&P2);
printf("\n\n Sum : ");
print(&P3);
getch();
break;
case 4:P3=multiply(&P1,&P2);
printf("\n\n1'st polynomial : ");
print(&P1);
printf("\n\n2'nd polynomial : ");
print(&P2);
printf("\n\n Product : ");
print(&P3);
getch();
break;
case 5:P3=subtr(&P1,&P2);
printf("\n\n1'st polynomial : ");
print(&P1);
printf("\n\n2'nd polynomial : ");
print(&P2);
printf("\n\n Difference : ");
print(&P3);
getch();
break;
}
}while(option!=6);
}
void read(polynomial *Ptr)
{
int n, i, power;
float coeff;
term t;
init(Ptr);
printf("\n Enter number of terms :");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("\nenter a term(power coeff.): ");
scanf("%d%f",&power,&coeff);
t.power=power;
t.coeff=coeff;
insert(Ptr,t);
}
}
void print(polynomial *Ptr)
{
int i;
printf("%5.2fX^%d ",(Ptr->a[0]).coeff,(Ptr->a[0]).power);
for(i=1;i<Ptr->n;i++)
printf(" + %5.2fX^%d ",(Ptr->a[i]).coeff,(Ptr->a[i]).power);
}
polynomial add(polynomial *P1, polynomial *P2)
{
polynomial P3;
term t;
int i,j;
i=j=0;
init(&P3);
while(i<P1->n && j<P2->n)
{
if(P1->a[i].power==P2->a[j].power)
{
t.power=P1->a[i].power;
t.coeff=P1->a[i].coeff+P2->a[j].coeff;
insert(&P3,t);
i++;j++;
}
else
if(P1->a[i].power < P2->a[j].power)
{
insert(&P3,P1->a[i]);
i++;
}
else
{
insert(&P3,P2->a[j]);
j++;
}
}
while(i<P1->n)
{
insert(&P3,P1->a[i]);
i++;
}
while(j<P2->n)
{
insert(&P3,P2->a[j]);
j++;
}
return(P3);
}
polynomial subtr(polynomial *P1, polynomial *P2)
{
polynomial P3;
term t;
int i,j;
i=j=0;
init(&P3);
while(i<P1->n && j<P2->n)
{
if(P1->a[i].power==P2->a[j].power)
{
t.power=P1->a[i].power;
t.coeff=P1->a[i].coeff-P2->a[j].coeff;
insert(&P3,t);
i++;j++;
}
else
if(P1->a[i].power < P2->a[j].power)
{
insert(&P3,P1->a[i]);
i++;
}
else
{
insert(&P3,P2->a[j]);
j++;
}
}
while(i<P1->n)
{
insert(&P3,P1->a[i]);
i++;
}
while(j<P2->n)
{
insert(&P3,P2->a[j]);
j++;
}
return(P3);
}
polynomial multiply(polynomial *P1,polynomial *P2)
{
polynomial P3, Ptemp;
term t;
int i,j;
init(&P3);
for(i=0;i<P2->n;i++)
{
init(&Ptemp);
for(j=0;j<P1->n;j++)
{
t.power=P1->a[j].power+P2->a[i].power;
t.coeff=P1->a[j].coeff*P2->a[i].coeff;
insert (&Ptemp, t);
}
P3=add(&P3,&Ptemp);
}
return(P3);
}
void init(polynomial *Ptr)
{
Ptr->n=0;
}
void insert(polynomial *Ptr,term t)
{
int i;
for(i=Ptr->n-1; (Ptr->a[i]).power > t.power && i>=0;i--)
Ptr->a[i+1]=Ptr->a[i];
Ptr->a[i+1]=t;
(Ptr->n)++;
}
OUTPUTS:-