/**************************************************************************/ /* CSE2304 Prac3B Sample Source Code */ /**************************************************************************/ #include #include #include "routines.h" int process(int input) { if(((input>='A')&&(input<='Z'))||((input>='a')&&(input<='z'))) return 1; else return 0; } int main(int argc, char *argv[]) { int input; FILE *fp1, *fp2; struct Element *record; int size_array; int i,index,rank000; char buffer[LIMIT]; int count000=0; int count1,count2; if(argc<3) { printf("The Usage of the program is:\n"); printf("prac3b [text file to be parsed] [dictionary of words]\n"); exit(0); } if((fp2=fopen(argv[1],"r"))==NULL) { printf(" The program can't open the text file, please check the directory and the file name\n"); exit(0); } if((fp1=fopen(argv[2],"r"))==NULL) { printf(" The program can't open the dictionary file, please check the directory and the file name (normally it is /usr/share/dict/words)\n "); exit(0); } size_array = size(fp1) + 1; /* dictionary */ fclose(fp1); fp1=fopen(argv[2],"r"); record=(struct Element *)malloc(sizeof(struct Element)*size_array); for(i=0;i='A')&&(buffer[index]<='Z')) buffer[index]+=32; /* if uppercase convert to lower case */ } if((index=check(buffer,record,size_array-1))>=0) { count(record,index); for(index=0;index=0) count(record,index); else { record[size_array - 1].count++; count000++; } for(index=0;indexcount > b->count ) return -1; if( a->count < b->count ) return +1; /*else same count so... */ if( a->length < b->length ) return -1; if( a->length > b->length ) return +1; /*else same count & length so... */ return( strncmp( a->words, b->words, LIMIT ) ); } void sort_dict(struct Element * record, int n) { qsort( record, n, sizeof(struct Element), compare ); } /* library qsort */ /* ------------------------------------------------------------------------- */ void show_dict(struct Element *record, int n) /* useful for debugging */ { int i, c; printf("\n--- dictionary size=%i: \n", n); for(i = 0; i < n; i++) { c = record[i].count; if( c > 0 ) printf(" record[%i] = %i x %s\n", i, c, record[i].words); } } /* ------------------------------------------------------------------------- */