Poeter.se logo icon
Redan medlem?   Logga in




 

permutations there it is

/*Hej. */

#include
#include
#define MAX_P 25

struct permutation
{
int length;
int number;
int perm[MAX_P];
struct permutation *next;
};

struct permutation *permutation_list;

int mfak(int);
int write_file(struct permutation *first,int n);
struct permutation *read_and_link(struct permutation*,int);
struct permutation *build_perm_list(struct permutation*);
void print_perm_list(struct permutation*, int);
void clear_kb(void);

int main(int argc, char *argv[])
{
int work,n;
struct permutation *ny;
ny=(struct permutation*)malloc(sizeof(struct permutation));
ny->length=1;
ny->next=NULL;
ny->number=1;
ny->perm[0]=1;
permutation_list=ny;

printf("size of permutation <= ");
scanf("%d",&n);
printf("-------------------------");
permutation_list=read_and_link(permutation_list,n);
print_perm_list(permutation_list,n);
work=write_file(permutation_list,n);
if(work==-1)
printf("nBetter luck next time");

printf("nThank you for listing Permutations");
system("PAUSE");
return 0;
}

struct permutation* read_and_link(struct permutation *first1, int n)
{
int length;
struct permutation *mark, *tmp1,*tmp2,*first2;
if(n==1)
return first1;
else if(n==2)
{
tmp1=first1;
first2=build_perm_list(tmp1);
return(first2);
}
else if (n>=3)
{
tmp1=first1;
first2=build_perm_list(tmp1);
tmp2=first2;
tmp2=tmp2->next;
mark=tmp2->next;
tmp2->next=first1;//länkar om
free(mark);//frigör kopian
first1=first2;
printf("nI'm ready to rock and roll!n");
while(first1->length < n)//här är length=2 så ja första gången om n>=3
{
length=first1->length;
tmp1=first1;
while(tmp1->length==length)//(tmp1!=NULL)&& do while ? ...while(tmp1->length==tmp1->next->length)
{
first2=build_perm_list(tmp1);
tmp2=first2;
while(tmp2->length==tmp2->next->length)
{
tmp2=tmp2->next;
}
mark=tmp2->next;//peka ut den överflödiga kopian som ska bort
tmp2->next=first1;//länka ihop med den tidigare listan
free(mark);
first1=first2;
tmp1=tmp1->next;
}
/*system("PAUSE");*/
//ska egentligen ligga ovanför
}
return first1;
}

}

struct permutation* build_perm_list(struct permutation *first)
{
struct permutation *current;
current=first;
printf("n----------------------------n");
printf("nbuild_perm_list is calledn");
struct permutation *ny;
int number,length,i,j;
length=first->length;
number=first->number;
/*i representerar platsen där vi ska placera ut first->length+1*/
for(i=0;i<=length;i++)
{ /*skapa en ny permutation av längd l
från den som first pekar på av längd l-1*/
/*system("PAUSE");*/
ny =(struct permutation*)malloc(sizeof(struct permutation));
ny->length=length+1;
number++;
ny->number=number;
ny->next=NULL;
j=0;
printf("nfor i=%d",i); printf(" ny->length=%dnn",ny->length);
while(j {
ny->perm[j]=current->perm[j];/*kopiering*/
printf(" perm[%d]=%d ",j,ny->perm[j]);
j=j+1;
}
ny->perm[j]=ny->length; /*==perm[i] det nya elementet*/
printf(" perm[%d]=%d ",j,ny->perm[j]);
j=j+1;
while(j<=length)
{
ny->perm[j]=current->perm[j-1];/*kopiering*/
printf(" perm[%d]=%d ",j,ny->perm[j]);
j=j+1;
}
printf("n");
/*inlänkningsdags*/
ny->next=first;
first=ny;
}
return first;
}

void print_perm_list(struct permutation* first, int n)
{
printf("nn--------------------------------nnPrint_perm_list calledn--------------------------------n");
int i,m;
struct permutation *tmp;
m=mfak(n);
tmp=first;
if(n==1)
printf("nperm[0]=%d",tmp->perm[0]);
else
{ while((tmp!=NULL)) //&&(tmp->length==tmp->next->length)
{
/*smart sann lögn*/ printf("n%d:",m);
/* printf("nNumber should be = %dn",m); */
m--;
/*printf("ntmp->length=%d tmp->number=%dnn",tmp->length,tmp->number); */
for(i=0;ilength;i++)
{
printf(" %d",tmp->perm[i]);
}
printf("n");
tmp=tmp->next;
if(m==0)
tmp=NULL;
/* system("PAUSE");*/
}
}
}

int mfak(int m)
{
if(m<=0)
return(1);
else
return(m*mfak(m-1));
}

void clear_kb(void)
{
char junk[80];
gets(junk);

}

/*write_file returns 0 if succesful otherwise -1*/
int write_file(struct permutation *first,int n)
{
struct permutation *tmp;
FILE *fp;
int i,m;
char filename[25];
m=mfak(n);
clear_kb();
puts("Enter filename: ");
gets(filename);
if((fp=fopen(filename,"w"))==NULL)
{ fprintf(stderr,"Error writing to file");
return(-1);
}
else
{
tmp=first;
while(tmp!=NULL)
{
m--;
fprintf(fp,"n[%d] ",m+1);
fprintf(stdout,"n[%d] ",m+1);
for(i=0;ilength;i++)
{
fprintf(fp,"%d",tmp->perm[i]);
fprintf(stdout,"%d",tmp->perm[i]);
}
tmp=tmp->next;
if(m==0)
tmp=NULL;
}
fclose(fp);
return(0);
}
}




Bunden vers (Haiku) av PolyMathWolverine
Läst 212 gånger
Publicerad 2007-02-26 17:16



Bookmark and Share

  > Nästa text
< Föregående

PolyMathWolverine
PolyMathWolverine