當前位置:成語大全網 - 英語詞典 - C語言簡單的英文詞典排版系統的實現

C語言簡單的英文詞典排版系統的實現

#include "stdio.h" #include "stdlib.h" #include "string.h" #include "ctype.h" #define ROWS 256 #define COLS 32 static FILE *fp; static char a[ROWS][COLS]; char get_option(void); int b(int count); void c(char *pt[], int count); int check(char arr[], int count); void storage(char *pt[], int count); int main(void) { int i,count; int start; char *pt[ROWS]; char ch, len; char input; if((fp=fopen("words.txt","a+"))==NULL) { fputs("不能打開或建立文件!\n",stderr); exit(1); } fseek(fp,0L,SEEK_END); start=(int)ftell(fp)/32; count=start; rewind(fp); if(fread(a,32*sizeof(char),start,fp)==0) { i=0; puts("請輸入單詞(每行壹個),在新行輸入END結束輸入:"); while(i<ROWS&&scanf("%s", a[i])==1) { fflush(stdin); if(strncmp(a[i],"END",3)==0) { count+=i; break; } if(check(a[i], i)) continue; i++; } } puts("您要做些什麽?"); puts("a. 顯示已有的單詞 b. 添加新單詞"); puts("c. 對已有的單詞進行排序 d. 退出"); while((input=get_option())!='d') { if(input=='a') { puts("已有的單詞:"); for(i=0;i<count;i++) { printf(" "); puts(a[i]); } } if(input=='b') { puts("請輸入新的單詞(每行壹個),在新行輸入END結束輸入: "); count=b(count); } if(input=='c') { puts("對單詞進行排序:"); c(pt, count); for(i=0;i<count;i++) { printf(" "); puts(pt[i]); } } puts("還要做些什麽?"); } storage(pt,count); fclose(fp); puts("再見!"); return 0; } char get_option(void) { char ch; while((ch=getchar())<'a'||ch>'d') { while((ch=getchar())!='\n') ; puts("請輸入a,b,c或者d."); } fflush(stdin); return ch; } int b(int count) { int i; i=count; while(i<ROWS&&scanf("%s", a[i])==1) { fflush(stdin); if(check(a[i], i)) continue; if(strncmp(a[i],"END",3)==0) { count=i; break; } i++; } return count; } void c(char *pt[], int count) { int i,j; char *temp; for(i=0;i<ROWS;i++) pt[i]=a[i]; for(i=0;i<count;i++) for(j=i+1;j<count;j++) { if(strcmp(pt[i],pt[j])>0) { temp=pt[i]; pt[i]=pt[j]; pt[j]=temp; } } } int check(char arr[], int count) { int i; int flag=0; for(i=0;i<strlen(arr);i++) if(isalpha(arr[i])==0) { printf("%s不是壹個單詞.\n",arr); flag=1; break; } for(i=0;i<count;i++) if(strncmp(a[i],a[count],strlen(a[count])+1)==0) { puts("重復的單詞!"); flag=1; } return flag; } void storage(char *pt[], int count) { int i,j; char ptr[ROWS][COLS]; c(pt, count); for(i=0;i<count;i++) for(j=0;pt[i][j]!='\0';j++) ptr[i][j]=pt[i][j]; fp=fopen("words.txt","w+"); rewind(fp); fwrite(ptr,32*sizeof(char),count,fp); }