You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dic_001/电子英汉词典设计.cpp

266 lines
4.5 KiB

6 years ago
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define LEN sizeof(struct info)
struct info
{
char word[40];
char mean[40];
};
struct info inf[400];
int m=0;
void enter();
void add();
void Delete();
void Edit();
void enter()
{
int i,m=0;
char n;
char to[40];
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%s",to);
FILE *fp;
if((fp=fopen("data.txt","ab+"))==NULL)
{
printf("<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!\n");
exit(1);
}
while(!feof(fp))
{
if(fread(&inf[m],LEN,1,fp)==1)
{
if(strcmp(to,inf[m].word)==0)
{
printf("%s ",to);
printf("%s\n",inf[m].mean);
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?");
printf("<EFBFBD><EFBFBD>Y<EFBFBD><EFBFBD>y<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
fclose(fp);
return;
}
}
m++;
}
printf("<EFBFBD>޴˵<EFBFBD><EFBFBD><EFBFBD>!\n");
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?");
printf("<EFBFBD><EFBFBD>Y<EFBFBD><EFBFBD>y<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
fclose(fp);
return;
if(m==0)
printf("<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>޼<EFBFBD>¼!\n");
if((fp=fopen("data.txt","ab+"))==NULL)
{
printf("<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!\n");
return;
}
fclose(fp);
}
void add()
{
int i;
char n[20];
FILE *fp;
if((fp=fopen("data.txt","ab+"))==NULL)
{
printf("<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!\n");
exit(0);
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: \n");
scanf("%s",n);
while(!feof(fp))
{
if(fread(&inf[m],LEN,1,fp)==1)
{
if(strcmp(n,inf[m].word)==0)
{
printf("<EFBFBD><EFBFBD><EFBFBD>д˵<EFBFBD><EFBFBD>ʣ<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>?\n");
printf("1. <20><> 2. <20><>\n");
scanf("%d",&i);
switch(i)
{
case 1:
Edit();
case 2:
break;
}
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?");
printf("<EFBFBD><EFBFBD>Y<EFBFBD><EFBFBD>y<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
fclose(fp);
return;
}
}
m++;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
scanf("%s",inf[m].mean);
strcpy(inf[m].word,n);
if(fwrite(&inf[m],LEN,1,fp)!=1)
{
printf("<EFBFBD><EFBFBD><EFBFBD>ܱ<EFBFBD><EFBFBD><EFBFBD>");
}
else
{
printf("<EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD>\n<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>");
printf("<EFBFBD><EFBFBD>Y<EFBFBD><EFBFBD>y<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
fclose(fp);
}
void Delete()
{
FILE *fp,*ft;
char n[40],k[40];
int m=0,l=0,i;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫɾ<EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD>ʣ<EFBFBD>\n");
scanf("%s",n);
fp=fopen("data.txt","rb+");
ft=fopen("123.txt","wb+");
if(fp==NULL||ft==NULL)
{
printf("<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!\n");
exit(1);
}
printf("<EFBFBD>Ƿ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>ʣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>\n");
printf("\t1.<2E><>");
printf("\t\t2.<2E><>\n");
scanf("%d",&i);
switch(i)
{
case 1:
while(fread(k,sizeof(k),1,fp))
{
if(strcmp(k,n)!=0)
{
fwrite(k,sizeof(k),1,ft);
m++;
}
else
{
rewind(fp);
fseek(fp,sizeof(k)*(m+2),0);
}
}
fclose(fp);
fclose(ft);
remove("data.txt");
rename("123.txt","data.txt");
printf("ɾ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>");
break;
case 2:break;
}
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?\n");
printf("<EFBFBD><EFBFBD>Y<EFBFBD><EFBFBD>y<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
return;
}
void Edit()
{
int t,a,m=0;
char n[40],k[40];
FILE *fp;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>޸ĵĵ<EFBFBD><EFBFBD>ʣ<EFBFBD>");
scanf("%s",n);
if((fp=fopen("data.txt","rb+"))==NULL)
{
printf("<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!\n");
exit(1);
}
rewind(fp);
while(!feof(fp))
{
if(fread(&inf[m],LEN,1,fp)==1)
if(strcmp(n,inf[m].word)==0)
{
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD>޸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("\t1.<2E><>.");
printf("\t\t2.<2E><>.\n");
scanf("%d",&a);
switch(a)
{
case 1:printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%s",k);
strcpy(inf[m].mean,k);
t=sizeof(inf[m].mean);
fseek(fp,-t,1);
if(fwrite(&inf[m].mean,t,1,fp)==1)
printf("<EFBFBD>޸ijɹ<EFBFBD>!");
break;
case 2:
break;
}
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?\n");
printf("<EFBFBD><EFBFBD>Y<EFBFBD><EFBFBD>y<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
fclose(fp);
return;
}
m++;
}
printf("<EFBFBD>޴˵<EFBFBD><EFBFBD><EFBFBD>\n");
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>?\n");
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>y<EFBFBD><EFBFBD>Y<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ز˵<EFBFBD>\n");
fclose(fp);
return;
}
int main(void)
{
int choice;
char n[2];
do
{
printf("\n\t ==============================");
printf("\n\t *<2A><>ӭʹ<D3AD><CAB9><EFBFBD><EFBFBD><EFBFBD>޹<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>Ӣ<EFBFBD><D3A2><EFBFBD>ʵ<EFBFBD>*");
printf("\n\t ==============================\n");
printf("\n\t ***********<2A>˵<EFBFBD><CBB5><EFBFBD>ʾ***********\n");
printf("\n\t ********<2A><>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*********\n");
printf("\n\t ********<2A><>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*********\n");
printf("\n\t ********<2A><>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>*********\n");
printf("\n\t ********<2A><>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>*********\n");
printf("\n\t ********<2A><>0<EFBFBD><30><EFBFBD>˳<EFBFBD><CBB3>ʵ<EFBFBD>*********\n");
printf("<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>еĹ<EFBFBD><EFBFBD>ܣ<EFBFBD>");
scanf("%d",&choice);
switch(choice)
{
case 1:
enter();
scanf("%s",&n);
while(n[0]=='Y'||n[0]=='y')
{
enter();
scanf("%s",&n);
}
break;
case 2:
add();
m++;
scanf("%s",&n);
while(n[0]=='Y'||n[0]=='y')
{
add();
m++;
scanf("%s",&n);
}
break;
case 3:
Delete();
scanf("%s",&n);
while(n[0]=='Y'||n[0]=='y')
{
Delete();
scanf("%s",&n);
}
break;
case 4:
Edit();
scanf("%s",&n);
while(n[0]=='Y'||n[0]=='y')
{
Edit();
scanf("%s",&n);
}
break;
}
}while(choice!=0);
return 0;
}