* Id :120060023
*/
#include
#include
#include
struct linkedList_{
int num;
struct linkedList_ *nextPtr;
};
typedef struct linkedList_ linkedList;
void addLink();
void fib(int num);
int isEmpty(int *k,int num);
void addNum(linkedList **firstPtr,linkedList **secondPtr,int num);
int length(linkedList *ptr);
int takeNum(linkedList **firstPtr,int index);
void printLink(linkedList *header);
void printLinkedList();
linkedList *reverse(linkedList **num);
int *p,entry;
FILE *file;
void newLink(int num,linkedList **first,linkedList **last)
{
linkedList *header=NULL,*lastPtr=NULL;
linkedList *newNode;
newNode=(linkedList *)calloc(1,sizeof(linkedList));
newNode->num=num;
newNode->nextPtr=NULL;
if(!(*first))
{
*first=*last=newNode;
return ;
}
(*last)->nextPtr=newNode;
*last=newNode;
}
void instruction()
{
printf("\nWELCOME TO MY FIBANNOCI SERIES PROGRAM\n");
printf("Which number do you want to learn in fibannoci series?\n");
printf("Entry number :");
scanf("%d",&entry);
p=(int *)calloc(entry,sizeof(int));
fib(entry);
}
void fib(int num){
if(num==0|| num==1)
{
linkedList *first=NULL;
linkedList *second=NULL;
linkedList *firstEnd=NULL;
linkedList *secondEnd=NULL;
newLink(0,&first,&firstEnd);
newLink(1,&second,&secondEnd);
p[0]=(int)first;
p[1]=(int)second;
addLink();
}
else
{
if(isEmpty(p,num-1))
{
fib(num-1);
}
}
}
void addLink(){
int i=0,m=2;
linkedList *firstPtr,*secondPtr;
while(m!=entry)
{
firstPtr=(linkedList *)p[i];;
secondPtr=(linkedList *)p[++i];
addNum(&firstPtr,&secondPtr,m);
m++;
}
}
void addNum(linkedList **firstPtr,linkedList **secondPtr,int num)
{
int k,art,a,b,m=2;
linkedList *thirdEnd=NULL,*last=NULL,*first,*second,*third=NULL;
first=*firstPtr;
second=*secondPtr;
last=(linkedList *)p[num];
art=0;
a=length(first);
b=length(second);
while((a!=0)||(b!=0)||(art!=0))
{
k=takeNum(&first,a-1)+takeNum(&second,b-1);
newLink((k+art)%10,&third,&thirdEnd);
art=(k+art)/10;
if(a>0)
a--;
if(b>0)
b--;
}
third=reverse(&third);
p[num]=(int)third;
}
linkedList *reverse(linkedList **num)
{
int u=length(*num);
linkedList *f=NULL,*l=NULL,*k=NULL;
k=*num;
while(u>0)
{
newLink(takeNum(&k,u-1),&f,&l);
u--;
}
return f;
}
int length(linkedList *ptr){
int i=0;
while(ptr)
{
ptr=ptr->nextPtr;
i++;
}
return i;
}
int takeNum(linkedList **firstPtr,int index)
{
linkedList *first=*firstPtr;
linkedList *m=malloc(sizeof(linkedList));
m->num=0;
m->nextPtr=NULL;
if(index<0)
{
m->nextPtr=*firstPtr;
*firstPtr=m;
first=*firstPtr;
}
while(index>0)
{
first=(first)->nextPtr;
index--;
}
return (first)->num;
}
int isEmpty(int *k,int num){
if(k[num]==0)
return 1;
else
return 0;
}
void printLinkList()
{
int i;
file=fopen("fibannoci.txt","w");
for(i=0; i
fprintf(file,"%d ",i+1);
printLink((linkedList *)p[i]);
}
}
void printLink(linkedList *header)
{
int counter=0;
if(file==0){
printf("The file is not opened\n");
return ;
}
while(header)
{
fprintf(file,"%d",header->num);
header=header->nextPtr;
counter++;
}
fprintf(file,"\n");
}
int main(){
instruction();
printLinkList();
printf("The file is being written succesfully\n");
system("PAUSE");
return 1;
}
Hiç yorum yok:
Yorum Gönder