Home > Back-end >  Algorithm C, o god
Algorithm C, o god

Time:02-06

Demand is probably:
In a text, there are tens of thousands of words, need to filter out the repeated words, are as follows:
1, hello,
Hello, 2
3, you
This time, only keep the key words "you", "hello" and "hello" to take out, because, these two words contain the keywords "you", strives for the bosses teach, how to handle the code

CodePudding user response:

1, all the keywords from short to long sort;
2, from the beginning of the shortest, loop through the comparison, each time you select a keyword, then traverse whether at the back of the word containing the keyword, if included, delete the corresponding key words;

CodePudding user response:

Fyi:
//to sort the contents of the file 1 and heavy, as a result, save to file 2 
#include
#include
#include
1024//# define MAXCHARS can deal with the biggest line width, including the end-of-line \ n and string tail \ 0
Int MAXLINES=10000, MAXLINES2;
Buf, char * * buf2;
Int c, n, hh, I, L;
The FILE * f;
Char ln [MAXCHARS];
Int ignore_case=0;
Int icompare (const void * arg1, const void * arg2) {
Return stricmp ((char *) arg1, arg2 (char *));
}
Int the compare (const void * arg1, const void * arg2) {
Return STRCMP ((char *) arg1, arg2 (char *));
}
Int main (int arg c, char * * argv) {
If (argc<3) {
Printf (" Unique line. Designed by [email protected]. The 2012-08-20 \ n ");
Printf (" SRC Usage: % s. TXT uniqued. TXT [-] I \ n ", argv [0]).
return 1;
}
If (argc> 3) ignore_case=1;//if there is a command line parameter 3, ignore case
F=fopen (argv [1], the "r");
If (NULL==f) {
Printf (" Can not find the file % s! \ n ", argv [1]);
return 1;
}
Buf=(char *) malloc (MAXLINES * MAXCHARS);
If (NULL==buf) {
The fclose (f);
Printf (" Can not malloc LINES * (% d % d CHARS)! The \ n ", MAXLINES, MAXCHARS);
Return 2;
}
N=0;
Hh=0;
i=0;
While (1) {
If (NULL==the fgets (ln, MAXCHARS, f)) break;//
Hh++;
L=strlen (ln) - 1;
If (' \ n '!=ln [L]) {//long row behind ignore content
Printf (" % s Line % d long (& gt; % d), spilth ignored. \ n ", argv [1], hh, MAXCHARS);
While (1) {
C=fgetc (f);
If (' \ n '==c | | EOF==c) break;//
}
}
While (1) {//removes white Spaces at the end of each line '\ n' and
If (=='\ n' ln [L] | | '==ln [L]) {
Ln [L]=0;
L -;
If (L<0) break;//
} the else break;//
}
If (L>=0) {
Strcpy (buf + I, ln); I +=MAXCHARS;
N++;
If (n>={MAXLINES)
MAXLINES2=MAXLINES * 2;
If (MAXLINES2==1280000) MAXLINES2=2500000;
Buf2=(char *) realloc (buf, MAXLINES2 * MAXCHARS);
If (NULL==buf2) {
Printf (" Can not malloc LINES * (% d % d CHARS)! The \ n ", MAXLINES2, MAXCHARS);
Printf (" WARNING: Lines & gt; % d ignored. \ n ", MAXLINES);
break;//
}
Buf=buf2;
MAXLINES=MAXLINES2;
}
}
}
The fclose (f);
If (n> 1) {
If (ignore_case) tree (buf, n, MAXCHARS, icompare);
The else tree (buf, n, MAXCHARS, compare);
}
F=fopen (argv [2], "w");
If (NULL==f) {
Free (buf);
Printf (" Can not create the file % s! \ n ", argv [2]);
Return 2;
}
Fprintf (f "% s \ n", buf);
If (n> 1) {
If (ignore_case) {
Hh=0;
L=MAXCHARS;
For (I=1; iBuf if (stricmp (const char *) + hh, buf (const char *) + L)) {
Fprintf (f "% s \ n", buf + L);
}
Hh=L;
L +=MAXCHARS;
}
} else {
Hh=0;
L=MAXCHARS;
For (I=1; iBuf if (STRCMP (const char *) + hh, buf (const char *) + L)) {
Fprintf (f "% s \ n", buf + L);
}
Hh=L;
L +=MAXCHARS;
}
}
}
The fclose (f);
Free (buf);
return 0;
}

CodePudding user response:

refer to the second floor 4 teacher zhao response:
are for reference only:
//to sort the contents of the file 1 and heavy, as a result, save to file 2 
#include
#include
#include
1024//# define MAXCHARS can deal with the biggest line width, including the end-of-line \ n and string tail \ 0
Int MAXLINES=10000, MAXLINES2;
Buf, char * * buf2;
Int c, n, hh, I, L;
The FILE * f;
Char ln [MAXCHARS];
Int ignore_case=0;
Int icompare (const void * arg1, const void * arg2) {
Return stricmp ((char *) arg1, arg2 (char *));
}
Int the compare (const void * arg1, const void * arg2) {
Return STRCMP ((char *) arg1, arg2 (char *));
}
Int main (int arg c, char * * argv) {
If (argc<3) {
Printf (" Unique line. Designed by [email protected]. The 2012-08-20 \ n ");
Printf (" SRC Usage: % s. TXT uniqued. TXT [-] I \ n ", argv [0]).
return 1;
}
If (argc> 3) ignore_case=1;//if there is a command line parameter 3, ignore case
F=fopen (argv [1], the "r");
If (NULL==f) {
Printf (" Can not find the file % s! \ n ", argv [1]);
return 1;
}
Buf=(char *) malloc (MAXLINES * MAXCHARS);
If (NULL==buf) {
The fclose (f);
Printf (" Can not malloc LINES * (% d % d CHARS)! The \ n ", MAXLINES, MAXCHARS);
Return 2;
}
N=0;
Hh=0;
i=0;
While (1) {
If (NULL==the fgets (ln, MAXCHARS, f)) break;//
Hh++;
L=strlen (ln) - 1;
If (' \ n '!=ln [L]) {//long row behind ignore content
Printf (" % s Line % d long (& gt; % d), spilth ignored. \ n ", argv [1], hh, MAXCHARS);
While (1) {
C=fgetc (f);
If (' \ n '==c | | EOF==c) break;//
}
}
While (1) {//removes white Spaces at the end of each line '\ n' and
If (=='\ n' ln [L] | | '==ln [L]) {
Ln [L]=0;
L -;
If (L<0) break;//
} the else break;//
}
If (L>=0) {
Strcpy (buf + I, ln); I +=MAXCHARS;
N++;
If (n>={MAXLINES)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related