Thursday, August 27, 2009

Polynominal Addition



#include<stdio.h>



#include<conio.h>



#include<alloc.h>




struct

poly



{



int coef,exp;



struct
poly *next;



};



typedef
struct poly
padd;




void

getnode();




void

display();




void

main()



{



int
x;



char
s;



padd *eq1,*temp,*eq2,*t;



st:



clrscr();



printf("\n\t\t\tPOLYNOMINAL
ADDITION"
);



printf("\n\t\t\t--------------------");



printf("\n\n
1st expression:-"
);



temp=malloc(sizeof(padd));



getnode(temp);



eq1=temp;



clrscr();



printf("\n
2nd expression:-"
);



t=malloc(sizeof(padd));



getnode(t);



eq2=t;



clrscr();



printf("\nResult
is:"
);



printf("\n\n\t\t\t
Equation 1 : "
);



while(temp!=0)



{



if(temp->exp==0)



printf("%d",temp->coef);



else



printf("%dx^%d",temp->coef,temp->exp);



temp=temp->next;








if(temp!=0)



{



if(temp->coef>0)



printf("
+ "
);



}



}



printf("\n\n\t\t\t
equation 2 : "
);



while(t!=0)



{



if(t->exp==0)



printf("%d",t->coef);



else



printf("%dx^%d",t->coef,t->exp);



t=t->next;



if(t!=0)



{



if(t->coef>0)



printf("
+ "
);



}



}



printf("\n\n\t\t\t
The result : "
);



while((eq1!=0)&&(eq2!=0))



{



if((eq1->exp)==(eq2->exp))



{



x=(eq1->coef)+(eq2->coef);



if(x<0)



{








display(x,eq1->exp);



}



if(x>0)



{








printf("
+ "
);



display(x,eq1->exp);



}



eq1=eq1->next;



eq2=eq2->next;




continue
;



}



if((eq1->exp)<(eq2->exp))



{



if(eq2->coef<0)



{



printf("-");



display(eq2->coef,eq2->exp);




}



else



{



printf("+");



display(eq2->coef,eq2->exp);



}



eq2=eq2->next;



continue;



}



if((eq1->exp)>(eq2->exp))



{




if
(eq1->coef<0)



display(eq1->coef,eq1->exp);



else



{



printf("+");



display(eq1->coef,eq1->exp);



}



eq1=eq1->next;



}



}



while(eq1!=0)



{



if(eq1->coef>0)



{



printf("
+ "
);



display(eq1->coef,eq1->exp);



}



else



display(eq1->coef,eq1->exp);



eq1=eq1->next;



}



while(eq2!=0)



{



if(eq2->coef<0)



display(eq2->coef,eq2->exp);



else



{



printf("+");



display(eq2->coef,eq2->exp);



}



eq2=eq2->next;



}



getch();



printf("\n\n\n\t\tDo
You Want Continue(Y/N):"
);



scanf("%s",&s);



if(s=='y' || s=='Y')



goto st;








}














void

getnode(padd *list)



{



char ch;



printf("\n\n\t\t\t
Enter the coefficient : "
);



scanf("%d",&list->coef);



printf("\n\t\t\t
Enter the exponent : "
);



scanf("%d",&list->exp);



printf("\n\n\t\t\t
Do You Want to extend(Y/N) : "
);



ch=getch();



if((ch=='Y')||(ch=='y'))



{



list->next=malloc(sizeof(padd));



clrscr();



printf("\n\n
Extending expression:- "
);



getnode(list->next);



}



else



list->next='\0';



}









void

display(int y,int
z)



{



if(z==0)



printf("%d",y);



else



printf("%dx^%d",y,z);



}





No comments:

Post a Comment