// 詞典.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include windows.h #include stdio.h #include malloc.h #include winbase.h #include process.h void Search() void InsertItem() int main(int argc, char* argv[]) while(index0||index5); /*選擇項不在0~4之間重輸*/ switch(index) return 0; } 輸出: **************MENU*************** 1 英譯漢 2 插入新的詞條 3 清空屏幕 4 退出 ********************************** 請選擇操作(1~4): 1 請輸入要查詢的單詞: bad adj. 劣質的,有害的,壞的,不利的,不健康的,嚴重的 **************MENU*************** 1 英譯漢 2 插入新的詞條 3 清空屏幕 4 退出 ********************************** 請選擇操作(1~4): 2 請輸入要插入的單詞: cat 請輸入單詞的解釋: 貓 插入成功。 **************MENU*************** 1 英譯漢 2 插入新的詞條 3 清空屏幕 4 退出 ********************************** 請選擇操作(1~4): 1 請輸入要查詢的單詞: cat 貓 **************MENU*************** 1 英譯漢 2 插入新的詞條 3 清空屏幕
成都創新互聯秉承實現全網價值營銷的理念,以專業定制企業官網,成都網站制作、網站建設,重慶小程序開發公司,網頁設計制作,手機網站開發,全網整合營銷推廣幫助傳統企業實現“互聯網+”轉型升級專業定制企業官網,公司注重人才、技術和管理,匯聚了一批優秀的互聯網技術人才,對客戶都以感恩的心態奉獻自己的專業和所長。
如果幫助到您,請記得采納為滿意答案哈,謝謝!祝您生活愉快! vae.la
/*基本的庫函數*/
#include conio.h
#include stdio.h
#include stdlib.h
#include string.h
#define szWORD 32
#define szSTRN 224
#define szITEM sizeof(struct TItem)
char fileDict[szSTRN];
typedef struct TItem {
char word[szWORD];
char mean[szSTRN];
} Item;
fpos_t lookup(char *word, char *mean)
{
FILE * f = 0; Item i;
int r = 0; fpos_t p = 0;
if(!word) return 0;
f = fopen(fileDict, "rb");
if (!f) return 0;
while(!feof(f)) {
fgetpos(f, p);
r = fread(i, szITEM, 1, f);
if(r 1) break;
if(i.word[0] == 0) continue;
if(strcmp(i.word , word)) continue;
if(mean) strcpy(mean, i.mean );
fclose(f);
return p+1;
}
fclose(f);
return 0;
}
void append(void)
{
Item i; FILE * f = 0; fpos_t p = 0;
memset(i, 0, szITEM);
printf("請輸入單詞:"); scanf("%s", i.word );
p = lookup(i.word, 0 );
if(p) {
printf("字典內已經有該單詞記錄!\n");
return;
}
printf("請輸入釋義,按回車結束:");
fflush(stdin);
gets(i.mean );
f = fopen(fileDict, "ab");
fwrite(i, szITEM, 1, f);
fclose(f);
printf("詞條已新增\n");
}
void erase(void)
{
Item i; FILE * f = 0; fpos_t p = 0;
memset(i, 0, szITEM);
printf("請輸入單詞:"); scanf("%s", i.word );
p = lookup(i.word, 0 );
if(p==0) {
printf("字典內沒有該單詞記錄!\n");
return;
}
p--;
memset(i, 0, szITEM);
f = fopen(fileDict, "rb+");
fsetpos(f, p);
fwrite(i, szITEM, 1, f);
fclose(f);
printf("詞條已刪除\n");
}
void edit(void)
{
Item i; FILE * f = 0; fpos_t p = 0;
memset(i, 0, szITEM);
printf("請輸入單詞:"); scanf("%s", i.word );
p = lookup(i.word, 0 );
if(p==0) {
printf("字典內沒有該單詞記錄!\n");
return;
}
p--;
printf("請輸入釋義,按回車結束(輸入abort放棄修改):");
fflush(stdin);
gets(i.mean );
if(strstr(i.mean ,"abort")) {
printf("已放棄修改!\n");
return ;
}
f = fopen(fileDict, "rb+");
fsetpos(f, p);
fwrite(i, szITEM, 1, f);
fclose(f);
printf("詞條已保存\n");
}
void query(void)
{
Item i; fpos_t p = 0;
memset(i, 0, szITEM);
printf("請輸入單詞:"); scanf("%s", i.word );
p = lookup(i.word, i.mean );
if(p==0) {
printf("字典內沒有該單詞記錄!\n");
return;
}
printf("【詞條】%s\n【釋義】%s", i.word , i.mean );
}
void set(void)
{
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 1)
strcpy(fileDict, argv[1]);
else
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';
switch(cmd) {
case 0: set(); break;
case 1: query(); break;
case 2: append(); break;
case 3: edit(); break;
case 4: erase(); break;
default: return 0;
}
}
return 0;
}
我用VC寫了一個
比較簡單的
// 詞典.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include windows.h
#include stdio.h
#include malloc.h
#include winbase.h
#include process.h
void Search()
{
char temp[100];
char buffer[2];
char result[100];
printf("請輸入要查詢的單詞:\n");
scanf("%s",temp);
buffer[0]=temp[0];
buffer[1]='\0';
int ret=GetPrivateProfileString(buffer,temp,0,result,100,"./date.ini");
if(ret0)
printf("%s\n\n\n\n\n",result);
else
printf("對不起,沒有您要查找的單詞.\n\n\n\n\n");
}
void InsertItem()
{
char temp[100];
char buffer[2];
char result[100];
printf("請輸入要插入的單詞:\n");
scanf("%s",temp);
printf("請輸入單詞的解釋:\n");
getchar();
scanf("%s",result);
buffer[0]=temp[0];
buffer[1]='\0';
int ret=WritePrivateProfileString(buffer,temp,result,"./date.ini");
if(ret=0)
printf("插入失敗。\n\n\n\n\n");
else
printf("插入成功。\n\n\n\n\n");
}
int main(int argc, char* argv[])
{
int index;
main:
printf("**************MENU***************\n\n");
printf(" 1 英譯漢\n");
// printf("2 漢譯英\n");
printf(" 2 插入新的詞條\n");
// printf(" 3 刪除已有詞條\n");
printf(" 3 清空屏幕\n");
printf(" 4 退出\n\n");
printf("**********************************\n");
do{
printf("請選擇操作(1~4):\n");
scanf("%d",index); /*輸入選擇項*/
}while(index0||index5); /*選擇項不在0~4之間重輸*/
switch(index)
{
case 1:
Search();
goto main;
break;
case 2:
InsertItem();
goto main;
break;
case 3:
system("cls");
goto main;
break;
case 4:
return 0;
break;
default:
break;
}
return 0;
}
輸出:
**************MENU***************
1 英譯漢
2 插入新的詞條
3 清空屏幕
4 退出
**********************************
請選擇操作(1~4):
1
請輸入要查詢的單詞:
bad
adj. 劣質的,有害的,壞的,不利的,不健康的,嚴重的
**************MENU***************
1 英譯漢
2 插入新的詞條
3 清空屏幕
4 退出
**********************************
請選擇操作(1~4):
2
請輸入要插入的單詞:
cat
請輸入單詞的解釋:
貓
插入成功。
**************MENU***************
1 英譯漢
2 插入新的詞條
3 清空屏幕
4 退出
**********************************
請選擇操作(1~4):
1
請輸入要查詢的單詞:
cat
貓
**************MENU***************
1 英譯漢
2 插入新的詞條
3 清空屏幕
4 退出
**********************************
請選擇操作(1~4):
4
Press any key to continue
名稱欄目:c語言英漢詞典維護函數 c語言英漢字典
本文來源:http://vcdvsql.cn/article14/ddoejde.html
成都網站建設公司_創新互聯,為您提供品牌網站制作、網站內鏈、全網營銷推廣、定制網站、微信小程序、自適應網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯