联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2018-06-18 05:42

#include <conio.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define szWORD 10

#define szSTRN 10

#define szITEM sizeof(struct TItem)

char fileDict[szSTRN];

typedef struct TItem

{

char word[szWORD];

char mean[szSTRN];

} Item;

typedef struct TItemG

{

char word[szWORD];

char mean[szSTRN];

} ItemG;

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("输入新释义,按回车结束(输入esc放弃修改):");

fflush(stdin);

gets(i.mean );

if(strstr(i.mean ,"esc"))

{

printf("放弃修改!\n");

return ;

}

f = fopen(fileDict, "rb+");

fsetpos(f, &p);

fwrite(&i, szITEM, 1, f);

fclose(f);

printf("词条已保存\n");

}

void display()

{

int j,t,e;

Item i;

char temp1,temp2;

e = sizeof(i.word);

for(t=0;t<szWORD;t++)

for(j=t+1;j<szWORD;j++)

{

if(i.word[j]>i.word[j+1])

{

temp1=i.word[j];

i.word[j]=i.word[j+1];

i.word[j+1]=temp1;

temp2=i.mean[j];

i.mean[j]=i.mean[j+1];

i.mean[j+1]=temp2;

}

}

//while(*i.word!= '\0'){putchar(*i.word);i.word[t++];}

for(t = 0;t < szWORD;t++){printf("【词条】%c【释义】%s\n",i.word, i.mean);}

}

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, "dict.txt");

/*end if*/

for(;;) {

printf("\n\

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

**       英汉小词典    **\n\

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

**0*******************0**\n\

**    1 - 查询词条     **\n\

**    2 - 新增词条     **\n\

**    3 - 编辑词条     **\n\

**    4 - 删除词条     **\n\

**    5 - 所有词条     **\n\

**    6 - 退出字典     **\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;

 case 5: display(); break;


 default: return 0;

 }

}

system("pause");

return 0;

}


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp