Friday, October 29, 2010

POLYNOMIAL ADDITION - LINKED LIST APPLICATION



Polynomial arithmetic includes basic mathematical operations such as addition, subtraction, and multiplication. These operations are defined naturally as if the variable x was an element of S. Division is defined similarly, but requires that S be a field. Examples of fields include rational numbers, Zp for p prime, and real numbers. The set of all integers is not a field and does not support polynomial division.

ADDITION

Addition and subtraction are performed by adding or subtracting corresponding coefficients. If

f(x) = \sum_{i=0}^n a_ix^i; g(x) = \sum_{i=0}^m b_ix^i

then addition is defined as

f(x)+g(x)= \sum_{i=0}^m (a_i+b_i)x^i where m > n

----------------------------------------------------------------------------------------------

A C PROGRAM TO IMPLEMENT POLYNOMIAL ADDITION- LINKED LIST APPLICATION

COMPILER EMPLOYED: DEV C++ COMPILER-4.9.9.2

SOURCE FILE SIZE :2 kb

EXE FILE SIZE :22 kb

NOTE: PLEASE INCLUDE THE DESIRED HEADER FILE

---------------------------------------------------------------------------------------------

C PROGRAM SOURCE & EXE DOWNLOAD:

Click download button to download

DISCLAIMER: The following program cannot be ensured of perfection.so any flaws in the program can be notified in the comments section.

----------------------------------------------------------------------------------------------

CODE:

#include
#include


//DEFINING A STRUCTURE
struct node
{
signed int COE;
int POWER;
struct node *NEXT;
}*START1,*START2,*ROOT,*SUM,*mul_res;
typedef struct node NODE;


//DISPLAY FUNCTION

void DISPLAY(NODE *CUR)
{
while(CUR!=NULL)
{
printf("%d x %d ",CUR->COE,CUR->POWER);
CUR=CUR->NEXT;
}
}



//READING TWO POLYNOMIAL EQUATIONS -LINKED LIST IMPLEMENTATION

void ins(NODE **ROOT,int c,int p)
{
NODE *TEMP,*CUR;
TEMP=(NODE *)malloc(sizeof(NODE));
CUR=(NODE *)malloc(sizeof(NODE));
CUR=*ROOT;
TEMP->COE=c;
TEMP->POWER=p;
TEMP->NEXT=NULL;
int flag=0;

if(*ROOT==NULL)
{
*ROOT=TEMP;
}

else
{
//FLAG=0 FOR ORDINARY INSERTION

//FLAG=1 FOR MULTIPLICATION

//WITHOUT FLAG, THE RESULT WILL BE 2X2 + 3X2

// AND NOT 5X2(CO-EFF WONT GET ADDED FOR SAME POWER)

while(CUR->NEXT!=NULL && flag==0)

{

if(p==CUR->POWER)
{
CUR->COE=CUR->COE + c;
flag=1;
}
CUR=CUR->NEXT;
}

if(p==CUR->POWER && flag==0) // THIS IS USED FOR MULTIPLICATION
CUR->COE=CUR->COE + c; //ADDITION WONT PASS INTO THIS
else if(flag==0) //CONDITION
CUR->NEXT=TEMP;
}
}



//READING TWO POLYNOMIAL EQUATIONS
void create( )
{
char ch;
int c,p,i;
for(i=1;i<=2;i++)
{
printf("\n ENTER FOR POLYNOMIAL %d (DECREASING POWER ORDER)",i);
printf("\n INSTRUCTION: PRESS C AFTER EACH INPUT TERM \n\n PRESS S AFTER ENTERING EXPRESSION");

ch='c';
// PRESS S AFTER GIVING I/P
while(ch!='s')
{
printf("\nCO-EFFICIENT (space) POWER :");
scanf("%d %d",&c,&p);
if(i==1)
ins(&START1,c,p);

else if(i==2)
ins(&START2,c,p);
scanf("%s",&ch);
}
ROOT=NULL;
}

printf("\n POLYNOMIAL 1:\n");
DISPLAY(START1);

printf("\n POLYNOMIAL 2:\n");
DISPLAY(START2);

}


//POLYNOMIAL ADDITION

void ADD()
{
NODE *CUR1=START1;
NODE *CUR2=START2;
while(CUR1!=NULL && CUR2!=NULL)
{
int k;
if(CUR1->POWER == CUR2->POWER)
{
k=CUR1->COE + CUR2->COE;
ins(&SUM,k,CUR1->POWER);
CUR1=CUR1->NEXT;
CUR2=CUR2->NEXT;
}
else if(CUR1->POWER > CUR2->POWER)
{
ins(&SUM,CUR1->COE,CUR1->POWER);
CUR1=CUR1->NEXT;
}
else if(CUR2->POWER > CUR1->POWER)
{
ins(&SUM,CUR2->COE,CUR2->POWER);
CUR2=CUR2->NEXT;
}
}



// INSERT THE REMAINING ELEMENTS OF CUR2

if(CUR1==NULL)
{
while(CUR2!=NULL)
{
ins(&SUM,CUR2->COE,CUR2->POWER);

CUR2=CUR2->NEXT;
}
}




// INSERT THE REMAINING ELEMENTS OF CUR1
else
while(CUR1!=NULL)
{
ins(&SUM,CUR1->COE,CUR1->POWER);
CUR1=CUR1->NEXT;
}

printf("\n\nTHE SUM:");
DISPLAY(SUM);
}




//MAIN FUNCTION
int main()
{
int CH;
START1=NULL;
START2=NULL;
SUM=NULL;

do
{
printf("\n POLYNOMIAL ADDITION");

printf("\n POLYNOMIAL ADDITION PROCESS INVOKED…");
create();
ADD();
break;

printf("\n DO YOU WISH TO CONTINUE?1~0:");
scanf("%d",&CH);
}while(CH==1);
getch();
return 0;
}


----------------------------------------------------------------------------------------------
Your's friendly,

[MOHANRAM.G],
ADMIN...

0 comments:

Post a Comment