C小项目 简易英汉词典

简易英汉词典

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

#include<stdio.h>
#include<string.h>
#include<stdlib.h>			//文件处理的头文件
#include<windows.h>         //会用到清屏功能
#include<conio.h>			//getch()函数暂停而不立即返回

struct diction
{
	char a[20];		//存放英文单词
	char b[40];		//存放中文解释
};
struct diction dict[10000];
void init()
{
	int i;
	FILE *fp;
	fp=fopen("data.txt","r");
	if(fp==NULL)
	{
		printf("there isn't data.txt\n data.txt is bulding...");
		fp=fopen("data.txt","w");
		getch();
	}
	for(i=0;i<10000;i++)
		fscanf(fp,"%s %s\n",dict[i].a,dict[i].b);
	fclose(fp);
}
void add()
{
	int i;
	char c[20];
	printf("plese enter the word you want to add:");
	scanf("%s",c);
	for(i=0;i<10000;i++)
	{
		if(strcmp(dict[i].a,c)==0)
		{
			printf("\nthere has existed this word,its Chinese mean is %s\n",dict[i].b);
			printf("\npress any key to return!");
			getch();
			break;
		}
		if(!*dict[i].a)	//如果循环到的数组为空,那么刚好是最后一个单词的下一个位置,在这里添加单词
		{
			printf("please input its Chinese mean: ");
			scanf("%s",dict[i].b);
			strcpy(dict[i].a,c);
			printf("\nsuccessfully add word!");
			getch();
			return ; //直接退出来,也可以用break;
		}
		if(i>=10000)
		{
			printf("\nthere's no capacity to save!");
			printf("press any key to return");
			getch();
			return;
		}
	}
}
void look()
{
	int i;
	char d[20];
	printf("please input a word:");
	scanf("%s",d);
	for(i=0;i<10000;i++)
	{
		if(strcmp(dict[i].a,d)==0)
		{
			printf("%s's Chinese mean is %s",dict[i].a,dict[i].b);
			printf("\nPress any key to return!");
			getch();
			return;
		}
		if(!*dict[i].a)	     //到这里就可以确定没有这个单词了,因为后面都是空的,可以直接返回了,没必要再继续后续循环
		{
			printf("No such word!");
			printf("\npress any key to return");
			getch();
			return;
		}
	}
}
void dele()
{
	int i,j;
	char e[20];
	printf("please input the word you want to delete:");
	scanf("%s",e);
	for(i=0;i<10000;i++)
	{
		if(strcmp(e,dict[i].a)==0)
		{
			do{
				strcpy(dict[i++].a,dict[i+++1].a);	//删除位置后的单词整体向前移动一位
				strcpy(dict[i++].a,dict[i+++1].a);
			}while(*dict[i].a);

			printf("\n successfully delete!");
			printf("\n press any key to return!");
			getch();
			return;
		}
	}
	printf("\n No such word!");
	getch();
}

void change()
{
	int i;
	char f[20];
	printf("please enter the word you want to modify:");
	scanf("%s",f);
	for(i=0;i<10000;i++)
	{
		if(strcmp(f,dict[i].a)==0)
		{
			printf("The meaning of the word before is:%s",dict[i].b);
			printf("\nPlease enter the meaning after modify:");
			scanf("%s",dict[i].b);
			printf("\nsuccessfully modify!");
			printf("\npress any key to return");
			getch();
			return;
		}
	}
	printf("No such word!");
	getch();
}
void all()
{
	int i,j;
	system("cls");
	for(j=0;(*dict[j].a)&&(j<10000);j++);//这个分号就很关键
	printf("The number of words recorded is %d\n",j);
	printf("\nAll words in the thesaurus are as follow:\n\n");
	for(i=0;i<10000;i++)
	{
		printf("%s:%s\n",dict[i].a,dict[i].b);
		if((i+1)%20==0)
		{
			getch();
			system("cls");
			printf("The number of words recorded is %d\n\n",j);
			printf("Currently showing the %d word\n\n",i+2);
		}
	}
	getch();
}
void store()
{
	int i;
	FILE *fp;
	fp=fopen("data.txt","w");
	if(fp==NULL)
	{
		printf("\n the data.txt can't be opened\n!");
		getch();
		return;
	}
	for(i=0;i<10000;i++)
		fprintf(fp,"%s %s\n",dict[i].a,dict[i].b);
	printf("\n successfully stored!\n press any key to return");
	getch();
}
void menu()
{
	printf("\n\n            Electronic English-Chineses Dictionary           \n\n");
	printf("        ******************* 1.Add     ********************        \n\n");
	printf("        ******************* 2.Inquire ********************         \n\n");
	printf("        ******************* 3.Delete  **********************         \n\n");
	printf("        ******************* 4.Modify  **********************          \n\n");
	printf("        ******************* 5.Total   **********************         \n\n");
	printf("        ******************* 6.Store   *********************          \n\n");
	printf("        ******************* 0.Exit   *********************          \n\n");
}
void main()
{
	char x;
	init();
	while(1)
	{
		system("cls");
		menu();
		x=getchar();
		switch(x)
		{
		case '0':exit(0);
		case '1':add();break;
		case '2':look();break;
		case '3':dele();break;
		case '4':change();break;
		case '5':all();break;
		case '6':store();break;	
		}
	}
	exit(0);
}

猜你喜欢

转载自blog.csdn.net/AnalogElectronic/article/details/88932614