Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #counts the number of words, source text write in count.txt file, has a bug
- #link https://yadi.sk/d/tJGgiAghdsp5WQ
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <malloc.h>
- #include <sys/time.h>
- #include <time.h>
- struct timeval t;
- typedef struct word {
- int count;
- char *Word;
- } WORD;
- int cmpword_alpha(WORD *, WORD *);
- int cmpword_quant(WORD *, WORD *);
- WORD *words[100000];
- char g[10000][100000],*gg[100000];
- int c = 0,b=0,u=1, kk=0, qqq=0;
- int totalw = 0;
- char *gw;
- char *getword(FILE *);
- int main(int ac, char *av[])
- {
- long int T1,T2,T3,T4,T5,T6;
- gettimeofday(&t,NULL);
- T1=t.tv_sec;
- T2=t.tv_usec;
- FILE *OUT1, *OUT2, *IN;
- char *pword;
- totalw = 0;
- int k = 0, l=0, i=0;
- OUT1 = fopen("qWord.txt","w");
- OUT2 = fopen("qcount.txt", "w");
- struct word *word = (struct word*)malloc(sizeof(struct word));
- IN = fopen("count.txt", "r");
- while ((pword = getword(IN))!=NULL)
- {
- totalw++;
- for (i=1;i<l;i++)
- if (strcmp(pword,word[i].Word)==0)
- {
- k=1;
- word[i].count++;
- }
- if ((k == 0) && (c>1))
- {
- l++;
- word[l].Word = pword;
- word[l].count=1;
- }
- k = 0;
- }
- qsort(word, l+1, sizeof(WORD),
- (int(*)(const void *, const void *))cmpword_alpha);
- for (i = 1; i < l; i++){
- fprintf(OUT1,"%d - %s \n", word[i].count, word[i].Word);
- }
- qsort(word, l+1, sizeof(WORD),
- (int(*)(const void *, const void *))cmpword_quant);
- for (i = 1; i < l; i++){
- fprintf(OUT2,"%d - %s \n", word[i].count, word[i].Word);
- }
- fclose(OUT1);
- fclose(OUT2);
- gettimeofday(&t,NULL);
- T3=t.tv_sec;
- T4=t.tv_usec;
- T5=T3-T1;
- T6=T4-T2;
- printf("Zatracheno %ld sec, %ld microsec \n", T5,T6);
- /*
- for (i=0;i<l+1;i++)
- {
- printf("%s",word[i].Word);
- }
- */
- return 0;
- }
- char *getword(FILE *F)
- {
- char *s;
- if (c==0)
- s=NULL;
- if (b==0)
- while (!feof(F))
- {
- b++;
- fgets(g[b],10000,F);
- }
- if (c>1)
- s=strtok(0," .,");
- if ((s==NULL) && (kk<b))
- {
- kk++;
- s=strtok(g[kk]," .,");
- }
- c++;
- return s;
- }
- int cmpword_alpha(WORD *w1, WORD *w2)
- {
- return (strcmp(((w1)->Word),((w2)->Word)));
- }
- int cmpword_quant(WORD *w1, WORD *w2)
- {
- return (((w1)->count) - ((w2)->count));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement