- C/C++ code
#include <time.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* toupper() function and others */ #define MAX_WORDLEN 50 #define MAX_WORDS 30000 /* Don't change this or you might overflow your allotted stack space. */ #define true 1 #define false 0 /* Use the following Macros to print EVERYTHING while executing the menu function */ /* Menu print */ #define printMenu() printf("Choose: 'P'rint, 'S'earch, 'I'nsert, 'R'emove, 'C'ount, 'Q'uit :\n") #define optP() printf("You chose to 'P'rint:\n") #define optS() printf("Enter word to 'S'earch:\n") #define optI() printf("Enter word to 'I'nsert:\n") #define optR() printf("Enter word to 'R'emove:\n") #define optC() printf("You chose to 'C'ount:\n") #define optQ() printf("You chose to 'Q'uit:\n") /* Insert print */ #define printInserted(word) printf("%s was successfully inserted.\n", word) #define printFailedToInsert(word) printf("Duplicate found, %s was not inserted.\n", word) /* Remove print */ #define printRemove(word) printf("%s was successfully removed.\n", word) #define printFailedToRemove(word) printf("%s cannot be found, and was not removed.\n", word) /* Search print */ #define printFound(word) printf("%s was successfully found.\n", word) #define printNotFound(word) printf("%s was not found.\n", word) /* Count print */ #define printCount(count) printf("%d words currently in the dictionary.\n", count) /* Other than the printf calls above in the macros, and the ones we put into main, you should not call printf anywhere else. You DO need fprintf in your printDictionary */ void printDictionary( char dictionary[][MAX_WORDLEN], int num, FILE *stream ); void insertWord( char dictionary[][MAX_WORDLEN], int *count, char *word ); void removeWord( char dictionary[][MAX_WORDLEN], int *count, char *word ); void loadDictionary( char *infilname, char dictionary[][MAX_WORDLEN], int *count ); void searchWord(char get[MAX_WORDLEN],char dictionary[][MAX_WORDLEN],int *count); void doMenu( char *outfileName, char dictionary[MAX_WORDS][MAX_WORDLEN], int *count) { while (1) { char select; int i; printMenu(); scanf("%c", &select); if (select=='P'||select=='p' ) { optP(); printDictionary(dictionary,*count,stdout);//未完成..80 char words. } if(select=='R'||select=='r') { char forremove[MAX_WORDLEN]; optR(); fgets(forremove,MAX_WORDLEN,stdin); removeWord(dictionary,count,forremove); if(0) printRemove(forremove); if(!0) printFailedToRemove(forremove); } if(select=='S'||select=='s') { char buffer[MAX_WORDLEN]; optS(); fgets(buffer,MAX_WORDLEN,stdin); searchWord(buffer,dictionary,count); if(0) printFound(buffer); if(1) printNotFound(buffer); } if(select=='I'||select=='I') { char forinsert[MAX_WORDLEN]; optS(); fgets(forinsert,MAX_WORDLEN,stdin); insertWord(dictionary,count,forinsert); if(0) printInserted(forinsert); if(!0) printFailedToInsert(forinsert); } if(select=='c'||select=='C') { optC(); printCount((*count+1)); } if(select=='q'||select=='Q') { FILE*handlefile; handlefile=fopen(outfileName,"w+"); optQ(); printDictionary(dictionary,*count,handlefile); fclose(handlefile); } } } /*void wordSearch(char dictionary[][MAX_WORDLEN],int*count,char buffer[]) { int i; int totelldifferent; for(i=0;i<*count;i++) { totelldifferent=strcmp(dictionary[i],buffer); if (totelldifferent==0) { printFound(buffer); exit(0); } } printNotFound(buffer); exit(1); }*/ void printDictionary( char dictionary[][MAX_WORDLEN], int num, FILE *stream ) { int i; for( i=0 ; i<num ; ++i ) { fprintf( stream, "%s\n", dictionary[i] ); } } void insertWord( char dictionary[][MAX_WORDLEN], int *count, char *word ) { int sequence; int i; int j; char temp[MAX_WORDS][MAX_WORDLEN]; strcpy(dictionary[*count],word); for(i=0;i<*count;i++) { sequence=strcmp(dictionary[*count],dictionary[i]); if(sequence<0) { for(j=i;j<*count;j++) { strcpy(temp[j],dictionary[j]); } for(j=i+1;j<(*count-1);j++) { strcpy(dictionary[j-1],temp[j]); } strcpy(dictionary[i],temp[j+1]); } if(sequence==0) exit(1); } exit(0); } void removeWord( char dictionary[][MAX_WORDLEN], int *count, char *word ) { int i,j,k; int totelldifferent; char temp[MAX_WORDS][MAX_WORDLEN]; for(i=0;i<*count;i++) { totelldifferent=strcmp(dictionary[i],word); if(totelldifferent==0) { for(j=i;j<*count;j++) strcpy(temp[j],dictionary[j]); for(k=i;k<(*count-1);k++) strcpy(dictionary[k],temp[k+1]); exit (0); } } exit(1); } /* LOADDICTIONARY */ void loadDictionary( char *infilname, char dictionary[][MAX_WORDLEN], int *count ) { FILE*handlefile; char buffer[MAX_WORDLEN]; handlefile=fopen (infilname,"rt"); while(!handlefile) { printf("the input file error"); exit (1); } while(fgets(buffer,MAX_WORDLEN,handlefile)) { insertWord(dictionary,count,buffer);//count 不能用*count? *count+=1; } fclose(handlefile); } void searchWord(char get[MAX_WORDLEN],char dictionary[][MAX_WORDLEN],int *count) { int i=0; int tell; for(;i<*count;i++) { tell=strcmp(get,dictionary[i]); if(tell==0) exit(0); } exit(1); } /* M A I N F U N C T I O N */ int main( int argc, char *argv[] ) { time_t startTime, stopTime; clock_t startCPU, stopCPU; double elapsedCPUsecs; char dictionary[MAX_WORDS][MAX_WORDLEN]; int count=0; if (argc < 3 ) { printf("usage: ./%s <infileName> <outFilename>\n",argv[0]); /* you gotta put in & out file names on cmd line! */ return EXIT_FAILURE; } startTime = time( NULL ); printf("\nStarting load of %s at %s", argv[1], ctime( &startTime) ); startCPU = clock(); loadDictionary( argv[1], dictionary, &count); stopCPU = clock(); stopTime = time( NULL ); printf("Finished load of %s at %s", argv[1], ctime( &stopTime) ); elapsedCPUsecs = ((double)(stopCPU-startCPU)) / CLOCKS_PER_SEC; printf("Elapsed CPU seconds: %f\n", elapsedCPUsecs ); doMenu( argv[2], dictionary, &count); return EXIT_SUCCESS; /* EXIT_SUCCESS defined as 0 in stdlib.h */ }