Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
# define MAX_WORD_SIZE 15
# define NUMRECORDS 23
# define TRUE 1
# define FALSE 0
enum columns { WORD, GRAMMAR, QUANTITY };
typedef struct
{
char word[MAX_WORD_SIZE];
enum { noun, verb, adverb, article, pronoun, adjective } type;
int numTimesAppear;
} record;
record wordlist[NUMRECORDS] =
{
{ "the", article, 0 }
, { "their", pronoun, 0 }
, { "a", article, 0 }
, { "difficult", adjective, 0 }
, { "run", verb, 0 }
, { "you", pronoun, 0 }
, { "game", noun, 0 }
, { "quickly", adverb, 0 }
, { "name", noun, 0 }
, { "he", pronoun, 0 }
, { "team", noun, 0 }
, { "quietly", adverb, 0 }
, { "president", noun, 0 }
, { "it", pronoun, 0 }
, { "interest", noun, 0 }
, { "promptly", adverb, 0 }
, { "whom", pronoun, 0 }
, { "ball", noun, 0 }
, { "decide", verb, 0 }
, { "she", pronoun, 0 }
, { "land", noun, 0 }
, { "is", verb, 0 }
, { "of", article, 0 }
};
int processFile( char fn[ ] );
void printRecords( );
void sortRecords( enum columns which );
int main( int argc, char *argv[ ] )
{
if ( argc != 2 )
{
printf( "Usage: %s filename\n", argv[0] );
return -1;
}
if ( processFile( argv[1] ) < 0 )
{
printf( "Error on file: %s \n", argv[1] );
return -1;
}
printRecords( );
sortRecords( WORD );
printf( "\n====================================\n" );
printf( "\n========Sorted by word==============\n" );
printf( "\n====================================\n" );
printRecords( );
sortRecords( GRAMMAR );
printf( "\n====================================\n" );
printf( "\n========Sorted by type==============\n" );
printf( "\n====================================\n" );
printRecords( );
sortRecords( QUANTITY );
printf( "\n====================================\n" );
printf( "\n=======Sorted by quantity===========\n" );
printf( "\n====================================\n" );
printRecords( );
}
void printRecords( )
{
printf( "\n WORD\t TYPE\t# APPEAR\n" );
for( int i=0; i<NUMRECORDS; i++ )
{
switch(wordlist[i].type)
{
case 0:
printf( "%15s\t noun \t%3d\n", wordlist[i].word, wordlist[i].numTimesAppear );
break;
case 1:
printf( "%15s\t verb \t%3d\n", wordlist[i].word, wordlist[i].numTimesAppear );
break;
case 2:
printf( "%15s\t adverb \t%3d\n", wordlist[i].word, wordlist[i].numTimesAppear );
break;
case 3:
printf( "%15s\t article \t%3d\n", wordlist[i].word, wordlist[i].numTimesAppear );
break;
case 4:
printf( "%15s\t pronoun \t%3d\n", wordlist[i].word, wordlist[i].numTimesAppear );
break;
case 5:
printf( "%15s\t adjective \t%3d\n", wordlist[i].word, wordlist[i].numTimesAppear );
}
//printf( "%15s\t%10s\t%3d\n", wordlist[i].word, grammar, wordlist[i].numTimesAppear );
}
}
void sortRecords( enum columns which )
{
record temp;
int wordcount=0;
record newwordlist[NUMRECORDS];
switch(which)
{
case 0:
for(int i=0; i<NUMRECORDS; i++)
{
}
break;
case 1:
for(int j=0; j<6; j++)
{
for(int i=0; i<NUMRECORDS; i++)
{
if(wordlist[i].type==j)
{
newwordlist[wordcount]=wordlist[i];
wordcount++;
}
}
}
for(int i=0; i<NUMRECORDS; i++)
{
wordlist[i]=newwordlist[i];
}
break;
case 2:
for(int i=0; i<NUMRECORDS; i++)
{
if(wordlist[i].numTimesAppear<wordlist[i+1].numTimesAppear)
{
temp=wordlist[i];
wordlist[i]=wordlist[i+1];
wordlist[i+1]=wordlist[i];
}
}
}
}
int processFile( char fn[ ] )
{
FILE *file;
char line[81];
char query[MAX_WORD_SIZE];
if ( (file = fopen( fn, "r" )) == NULL )
{
return -2;
}
while( fgets(line, sizeof(line), file ) != NULL )
{
for(int i=0; i<NUMRECORDS; i++)
{
for(int j=0; i<=strlen(line); )
{
while(line[j] != ' ')
{
query[j]=line[j];
j++;
}
if(strcmp(query, wordlist[i].word) == 0)
{
wordlist[i].numTimesAppear++;
}
j++;
}
}
}
fclose(file);
return 0;
}