#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int count;
struct node * leaves[1<<4];
};
void addnode(struct node * root,char * str,unsigned char * collation)
{
struct node *p=NULL;
p=root;
unsigned char * s=(unsigned char *)str;
if(*s==0)return;
while(*s != 0)
{
unsigned char c=collation[*s];
if(p->leaves[c >> 4] == NULL)
p->leaves[c >> 4]=calloc(1,sizeof(struct node));
p=p->leaves[c >> 4];
if(p->leaves[c & 15] == NULL)
p->leaves[c & 15]=calloc(1,sizeof(struct node));
p=p->leaves[c & 15];
++s;
}
++p->count;
}
void printnodes(struct node * n,unsigned char * buffer,int bp,unsigned char * collation)
{
int k=0,t=0,l=0;
if(n->count>0)
{
l=strlen(buffer);
unsigned char *cc=calloc(1,l+1);
for(t=0;t<l;t++)cc[t]=collation[buffer[t]];
for(k=0;k<n->count;k++)
printf("%s\n",cc);
free(cc);
}
for(k=0;k<16;k++)
if(n->leaves[k]!=NULL)
{
buffer[bp>>1]&=(bp&1)?((1<<4) -1)<<4:0;
buffer[bp>>1]|=(bp&1)?k:(k<<4);
printnodes(n->leaves[k],buffer,bp+1,collation);
}
buffer[bp>>1]&=(bp&1)?((1<<4) -1)<<4:0;
}
int main()
{
unsigned char cf[256];
unsigned char cr[256];
int a=0,b=0;
while(a<'A')
{
cf[a]=a;
cr[a]=a;
++a;
}
for(b='A';b<='Z';b++)
{
cf[a]=b;
cr[b]=a;
++a;
cf[a]=b+('a'-'A');
cr[b+('a'-'A')]=a;
++a;
}
for(;b<'a';b++)
{
cf[a]=b;
cr[b]=a;
++a;
}
for(b='z'+1;b<256;b++)
{
cf[a]=b;
cr[b]=a;
++a;
}
unsigned char *buffer=calloc(1,1024);
struct node * root=calloc(1,sizeof(struct node));
while(!feof(stdin))
{
if(fscanf(stdin,"%1023s",buffer)==0)break;
addnode(root,&buffer[0],&cf[0]);
memset(buffer,0,1024);
}
printnodes(root,buffer,0,&cr[0]);
return 0;
}
$ gcc -o t t.c
$ echo 'Afkjdhfkj daakh akjhkjh Dnkhkj Afkjdhfkj'|./t
Afkjdhfkj
Afkjdhfkj
akjhkjh
Dnkhkj
daakh
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
/* Structures */
/* Tree node, half a byte to reduce memory usage.*/
struct node
{
int count;
struct node * leaves[1<<4];
};
/* Functions */
/* Add a word to a tree. Adds 2 node per 1 character.*/
struct node * current=NULL;
void addword(struct node * root,void *str,int len,unsigned char * collation)
{
if(current==NULL)current=root;
unsigned char * s=(unsigned char *)str;
unsigned char * end=s+len;
if(s==end)return;
while(s < end)
{
/*Translate to sortable form*/
if((*s == 13) || (*s==10))
{
if(current != root)++current->count;
current=root;
}else{
unsigned char c=collation[*s];
if(current->leaves[c >> 4] == NULL)
current->leaves[c >> 4]=calloc(1,sizeof(struct node));
if(current->leaves[c >> 4] == NULL)
{
perror("Failed to allocate node. ");
exit(1);
}
current=current->leaves[c >> 4];
if(current->leaves[c & 15] == NULL)
current->leaves[c & 15]=calloc(1,sizeof(struct node));
if(current->leaves[c & 15] == NULL)
{
perror("Failed to allocate node. ");
exit(1);
}
current=current->leaves[c & 15];
}
++s;
}
}
/* Walk the tree from lower to higher and print words found.*/
void printwords(struct node * n,unsigned char * buffer,int bp,unsigned char * collation)
{
int k=0,t=0,l=0;
if(n->count>0)
{
l=strlen(buffer);
unsigned char *cc=calloc(1,l+1);
if(cc==NULL)
{
perror("Failed to allocate translation buffer. ");
exit(1);
}
/*Translate to printable form*/
for(t=0;t<l;t++)cc[t]=collation[buffer[t]];
/*Print words found*/
for(k=0;k<n->count;k++)
printf("%s\n",cc);
free(cc);
}
/*Check all leaves*/
for(k=0;k<16;k++)
if(n->leaves[k]!=NULL)
{
buffer[bp>>1]&=(bp&1)?((1<<4) -1)<<4:0;
buffer[bp>>1]|=(bp&1)?k:(k<<4);
/*Change to next position in a buffer and reenter*/
printwords(n->leaves[k],buffer,bp+1,collation);
}
/*clear last bufer position from remaining garbage (Is it really needed???)*/
buffer[bp>>1]&=(bp&1)?((1<<4) -1)<<4:0;
}
/*Entry point*/
int main(int argc,char **argv)
{
/*collation tables*/
unsigned char cf[256];
unsigned char cr[256];
/*Set up collation tables so small letters are sorted after corresponding capital ones*/
int a=0,b=0;
while(a<'A')
{
cf[a]=a;
cr[a]=a;
++a;
}
for(b='A';b<='Z';b++)
{
cf[a]=b;
cr[b]=a;
++a;
cf[a]=b+('a'-'A');
cr[b+('a'-'A')]=a;
++a;
}
for(;b<'a';b++)
{
cf[a]=b;
cr[b]=a;
++a;
}
for(b='z'+1;b<256;b++)
{
cf[a]=b;
cr[b]=a;
++a;
}
unsigned char *buffer=calloc(1,4096);
if(buffer==NULL)
{
perror("Failed to allocate buffer. ");
exit(1);
}
int got=0;
/*tree root node*/
struct node * root=calloc(1,sizeof(struct node));
if(root==NULL)
{
perror("Failed to allocate root node. ");
exit(1);
}
/*Check argument count*/
if(argc!=2)
{
fprintf(stderr,"Exactly 1 file expected.\n");
exit(1);
}
/*Open input file*/
FILE * fd=fopen(argv[1],"r");
if(fd==NULL)
{
perror("Failed to open file: ");
exit(1);
};
/*Read words*/
while(!feof(fd))
{
got=fread(buffer,1,4096,fd);
fprintf(stderr,"%d\n",got);
if(got==0)break;
//buffer[got]=0;
addword(root,buffer,got,&cr[0]);
if(got<4096)break;
}
if((got==0)||((buffer[got-1]!=13)&&(buffer[got-1]!=10)))
{
buffer[0]=13;
addword(root,buffer,1,&cr[0]);
}
if(errno != 0)perror("Railed to read word from file: ");
/*close file*/
if(fclose(fd)!=0)
{
perror("Error while trying to close input file: ");
};
/*Clear buffer*/
memset(buffer,0,4096);
/*Show results*/
printwords(root,buffer,0,&cf[0]);
return 0;
}
#include <stdio.h>
#include <string.h>
int main(void)
{
enum {MAX_LINE_SIZE=100, MAX_VEC_SIZE=1000};
char line[MAX_LINE_SIZE], tmp[MAX_LINE_SIZE];
char v[MAX_VEC_SIZE][MAX_LINE_SIZE];
char fname[40];
int i, j, vec_size;
FILE *f;
printf("Файл " );
scanf("%s", &fname);
// Проверка открытия
if ((f = fopen(fname, "r")) == NULL)
{
printf("Файл не найден\n");
system("pause");
return 1;
}
// Заполняем массив строк
vec_size=0;
while( fgets(line, MAX_LINE_SIZE, f) != NULL )
{
strcpy(v[vec_size++], line);
}
// Проверка закрытия
if (fclose(f)!=0)
{
printf("Ошибка закрытия файла\n");
system("pause");
return 1;
}
// Сортируем массив строк
// Правильней было бы сортировать массив указателей
for (i=0; i<vec_size-1; i++)
for (j=vec_size-1; j>i; j--)
if (strcmp(v[j], v[j-1])<0)
{
strcpy(tmp, v[j]);
strcpy(v[j], v[j-1]);
strcpy(v[j-1], tmp);
}
printf("\nПосле сортировки\n");
for (i=0; i<vec_size; i++)
printf(v[i]);
system("pause");
return 0;
}
Richard Bach
Jonathan Livingston Seagull
To the real Jonathan Seagull,
who lives within us all.
Part One
It was morning, and the new sun sparkled gold across the ripples of a
gentle sea. A mile from shore a fishing boat chummed the water. and the
word for Breakfast Flock flashed through the air, till a crowd of a
thousand seagulls came to dodge and fight for bits of food. It was another
busy day beginning.
But way off alone, out by himself beyond boat and shore, Jonathan
Livingston Seagull was practicing. A hundred feet in the sky he lowered
his webbed feet, lifted his beak, and strained to hold a painful hard
twisting curve through his wings. The curve meant that he would fly
slowly, and now he slowed until the wind was a whisper in his face, until
the ocean stood still beneath him. He narrowed his eyes in fierce
concentration, held his breath, forced one... single... more... inch...
of... curve... Then his featliers ruffled, he stalled and fell.
Seagulls, as you know, never falter, never stall. To stall in the air
is for them disgrace and it is dishonor.
But Jonathan Livingston Seagull, unashamed, stretching his wings
again in that trembling hard curve - slowing, slowing, and stalling once
more - was no ordinary bird.
Most gulls don't bother to learn more than the simplest facts of
flight - how to get from shore to food and back again. For most gulls, it
is not flying that matters, but eating. For this gull, though, it was not
eating that mattered, but flight. More than anything else. Jonathan
Livingston Seagull loved to fly.
Файл test.txt
После сортировки
But Jonathan Livingston Seagull, unashamed, stretching his wings
But way off alone, out by himself beyond boat and shore, Jonathan
It was morning, and the new sun sparkled gold across the ripples of a
Jonathan Livingston Seagull
Livingston Seagull loved to fly.
Livingston Seagull was practicing. A hundred feet in the sky he lowered
Most gulls don't bother to learn more than the simplest facts of
Part One
Richard Bach
Seagulls, as you know, never falter, never stall. To stall in the air
To the real Jonathan Seagull,
again in that trembling hard curve - slowing, slowing, and stalling once
busy day beginning.
concentration, held his breath, forced one... single... more... inch...
eating that mattered, but flight. More than anything else. Jonathan
flight - how to get from shore to food and back again. For most gulls, it
gentle sea. A mile from shore a fishing boat chummed the water. and the
his webbed feet, lifted his beak, and strained to hold a painful hard
is for them disgrace and it is dishonor.
is not flying that matters, but eating. For this gull, though, it was not
more - was no ordinary bird.
of... curve... Then his featliers ruffled, he stalled and fell.
slowly, and now he slowed until the wind was a whisper in his face, until
the ocean stood still beneath him. He narrowed his eyes in fierce
thousand seagulls came to dodge and fight for bits of food. It was another
twisting curve through his wings. The curve meant that he would fly
who lives within us all.
word for Breakfast Flock flashed through the air, till a crowd of a
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.