Beispiel #1
0
void sortCount(int *p, int l, int r) {
	int i = l;
	int j = r;
	int mid = p[(l + r) / 2];
	while (i <= j) {
		while (p[i] < mid) i++;
		while (p[j] > mid) j--;
		if (i <= j) {
			int tmp = p[i];
			p[i] = p[j];
			p[j] = tmp;
			i++;
			j--;
		}
	}
	if (i < r) sortCount(p, i, r);
	if (l < j) sortCount(p, l, j);
}
Beispiel #2
0
int main() {

   FILE* inFile;
   char filename[101];
   WORD words[MAX];
   WORD topWords[TOPS];
   char tempWord[31];
   int i = 0;
   int count = 0;
   int strLen, structLen = 0;

   printf("Enter the name of the file: ");
   scanf("%s", filename);

   //Opens the file, and if doesn't exist, it displays an error
   //message and closes the program.
   inFile = fopen(filename, "r");
   if( inFile == NULL ) {
      printf("ERROR: File doesn't exist.\n");
      exit (-1);
   }
   
   //Goes through the whole file and adds them to the array of words
   while( fscanf(inFile, "%s", tempWord) != EOF ) {
      strLen = strlen(tempWord);
      i = 0;

      //Sees if it a valid word to keep     
      if( keepWord( tempWord ) != FALSE ) {

         //Looks through the whole struct to see if it repeats or not.
         for( i = 0; i < 500; i++ ){
            
           //Finds the end of the struct so we add a new one
            if( words[i].word[0] == '\0' ){
               strcpy(words[i].word,tempWord);
               words[i].count = 1;
               structLen++;
               break;
            } 

            //Finds a duplicate so it stops here and adds to it's  count
            if( strcmp( words[i].word, tempWord ) == FALSE ){
               words[i].count++;
               break;
            }

         }
 
      } 
   }

   //Sorts by count
   sortCount(words, structLen);
   
   //Pulls the tops out
   pullTops(words, topWords, TOPS);
   
   //Sorts the tops struct alphabetically
   sortTops(topWords, TOPS);

   //Output results
   printf("   WORD            COUNT\n");
   printf("   ----            -----\n");

   for(i=0;i<TOPS;i++){
      if( topWords[i].word[0] != '\0'){
         count++;
         printf("%2d %-11s %9d\n", count, topWords[i].word, topWords[i].count);
      }
   }

     
   return 0;
}