Categories
- 4th semester (27)
- 5th semester (3)
- ADA (13)
- Assembly Level Language (12)
- BE (45)
- C Language Programming (5)
- C language (20)
- C++ Language (5)
- CCP Lab programing (3)
- Computer Programming Lab (3)
- DAA Lab Programming (13)
- Data Structure and C++ laboratory Program (6)
- Data Structure and C++ labotary Program (5)
- Design and Analysis of algorithm (14)
- First Year (5)
- MASM (12)
- Microprocessor (12)
- Microprocessor lab program (12)
- System Software & OS Laboratory (5)
- Unix program (4)
- bachelor of engineering (30)
- basic (1)
- basic mathematics (2)
- beginners (10)
- c++ program (9)
- calculations (7)
- computer science (30)
- downloadable (5)
- engineering syllabus (4)
- simple program (6)
Trend Posts
Blogger news
Author
Followers
Blog Archive
Friday, October 25, 2013
//Courtesy : Avinash G, Computer Science Dept.,Srinivas Institute of Technology,Mangalore
(avinashganiga94@gmail.com)
----------
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#define MALLOC(p,n,type)\
p=(type*) malloc(n*sizeof(type));\
if(p==NULL)\
{\
printf("Insufficient memory");\
exit(0);\
}
#define COMPARE(x,y) (((x)==(y))?0:((x)>(y))?1:-1)
struct node
{
int coeff;
int expon;
struct node *link;
};
typedef struct node *NODE;
void display(NODE head)
{
NODE temp;
if(head->link==head)
{
printf("polynomial does not exit\n");
return;
}
temp=head->link;
while(temp!=head)
{
if(temp->coeff<0)
printf("%dX^%d",temp->coeff,temp->expon);
else
printf("+%dX^%d",temp->coeff,temp->expon);
temp=temp->link;
}
}
NODE attach(int coeff,int expon,NODE head)
{
NODE temp,cur;
MALLOC(temp,1,struct node);
temp->coeff=coeff;
temp->expon=expon;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
NODE read_poly(NODE head)
{
int i=1,coeff,expon;
printf("Enter the coefficient -999to end the polynomial\n");
while(1)
{
printf("Enter the %d term \n",i++);
printf("coeff=");
scanf("%d",&coeff);
if(coeff==-999)
break;
printf("pow X=");
scanf("%d",&expon);
head=attach(coeff,expon,head);
}
return head;
}
NODE poly_add(NODE head1,NODE head2,NODE head3)
{
NODE a,b;
int coeff;
a=head1->link;
b=head2->link;
while(a!=head1 && b!=head2)
{
switch(COMPARE(a->expon,b->expon))
{
case 0:coeff=a->coeff+b->coeff;
if(coeff!=0)
head3=attach(a->coeff,a->expon,head3);
a=a->link;
b=b->link;
break;
case 1: head3:attach(a->coeff,a->expon,head3);
a=a->link;
break;
default: head3=attach(b->coeff,b->expon,head3);
b=b->link;
}
}
while(a!=head1)
{
head3=attach(a->coeff,a->expon,head3);
a=a->link;
}
while(b!=head2)
{
head3=attach(b->coeff,b->expon,head3);
b=b->link;
}
return head3;
}
void main()
{
clrscr();
NODE head1,head2,head3;
MALLOC(head1,1,struct node);
MALLOC(head3,1,struct node);
MALLOC(head3,1,struct node);
head1->link=head1;
head2->link=head2;
head3->link=head3;
printf("\nEnter the first polynomial\n");
head1=read_poly(head1);
printf("\nEnter the second polynomial\n");
head2=read_poly(head2);
printf("\npolynomial 1:");
display(head1);
printf("\npolynomial 2:");
display(head2);
head3=poly_add(head1,head2,head3);
printf("\npolynomial 3:");
display(head3);
getch();
}
__
Program Ends
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment
You are very Important to Us...
STAY TUNE...