當前位置:成語大全網 - 書法字典 - 設計對象字典

設計對象字典

/*基本庫函數*/

# include & ltconio.h & gt

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltstring.h & gt

#定義szWORD 32

#定義szSTRN 224

# define szITEM sizeof(struct TItem)

char fileDict[SZ strn];

typedef結構項{

char word[SZ word];

char mean[SZ strn];

}項;

fpos_t查找(char *word,char *mean)

{

文件* f = 0;第壹項;

int r = 0;fpos _ t p = 0;

如果(!word)返回0;

f = fopen(fileDict," Rb ");

如果(!f)返回0;

而(!feof(f)) {

fgetpos(f & amp;p);

r = fread(& amp;I,szITEM,1,f);

if(r & lt;1)破位;

if(i.word[0] == 0)繼續;

if(strcmp(i.word,word))繼續;

if(mean) strcpy(mean,I . mean);

fclose(f);

返回p+1;

}

fclose(f);

返回0;

}

void追加(void)

{

第壹項;文件* f = 0;fpos _ t p = 0;

memset(& amp;I,0,szITEM);

Printf("請輸入壹個詞:");scanf("%s ",I . word);

p = lookup(即word,0);

如果(p) {

Printf("這個詞已經被記錄在字典裏了!\ n ");

返回;

}

Printf("請輸入壹個定義,回車結束:");

fflush(stdin);

gets(I . mean);

f = fopen(fileDict," ab ");

fwrite(& amp;I,szITEM,1,f);

fclose(f);

Printf("條目已添加\ n ");

}

無效擦除(無效)

{

第壹項;文件* f = 0;fpos _ t p = 0;

memset(& amp;I,0,szITEM);

Printf("請輸入壹個詞:");scanf("%s ",I . word);

p = lookup(即word,0);

if(p==0) {

Printf("字典裏沒有這個詞的記錄!\ n ");

返回;

}

p-;

memset(& amp;I,0,szITEM);

f = fopen(fileDict," r b+ ");

fset pos(f & amp;p);

fwrite(& amp;I,szITEM,1,f);

fclose(f);

Printf("條目已刪除\ n ");

}

無效編輯(無效)

{

第壹項;文件* f = 0;fpos _ t p = 0;

memset(& amp;I,0,szITEM);

Printf("請輸入壹個詞:");scanf("%s ",I . word);

p = lookup(即word,0);

if(p==0) {

Printf("字典裏沒有這個詞的記錄!\ n ");

返回;

}

p-;

Printf("請輸入壹個定義,回車結束(回車中止放棄修改):");

fflush(stdin);

gets(I . mean);

if(strstr str(即“abort”){

Printf("修改中止!\ n ");

返回;

}

f = fopen(fileDict," r b+ ");

fset pos(f & amp;p);

fwrite(& amp;I,szITEM,1,f);

fclose(f);

Printf("條目已保存\ n ");

}

無效查詢(void)

{

第壹項;fpos _ t p = 0;

memset(& amp;I,0,szITEM);

Printf("請輸入壹個詞:");scanf("%s ",I . word);

p = lookup(i.word,I . mean);

if(p==0) {

Printf("字典裏沒有這個詞的記錄!\ n ");

返回;

}

printf(" Entry % s \ n interpretion % s ",i.word,I . mean);

}

無效集(無效)

{

int cmd = 0;

Printf("當前詞典是%s,需要更改嗎(選擇Y或Y更改)?",fileDict);

cmd = getch();

if(cmd == 'y' || cmd == 'Y') {

Printf("請輸入字典文件名(包括路徑):");

scanf("%s ",fileDict);

Printf("設置成功!\ n ");

}

}

int main(int argc,char * argv[])

{

int cmd = 0;

if(argc & gt;1)

strcpy(fileDict,argv[1]);

其他

strcpy(fileDict," c:\ \ dict . txt ");

/*end if*/

for(;;) {

printf("\n\

* * * * * * * * * * * * * * * * * * * * * * * * * \ n \

* *歡迎使用迷妳詞典!**\n\

* * * * * * * * * * * * * * * * * * * * * * * * * \ n \

* * 0-設置字典**\n\

* * 1-查詢術語**\n\

* * 2-新條目**\n\

* * 3-編輯條目**\n\

* * 4-刪除條目**\n\

* * 5-退出字典**\n\

* * * * * * * * * * * * * * * * * * * * * * * * * \ n ");

cmd = getch()-' 0 ';

開關(cmd) {

案例0:set();打破;

案例1:query();打破;

情況二:append();打破;

案例三:edit();打破;

情況四:erase();打破;

默認值:返回0;

}

}

返回0;

}