你的分享就是我们的动力 ---﹥

简单英汉小词典

时间:2013-05-24 15:48来源:www.chengxuyuans.com 点击:

代码简介

暂无

代码片段

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
#include "UserType.h"
#include "Dictionary.h"

int saveflag = 0; /*是否需要存盘的标志*/
void menu()  /*主菜单*/
{
	system("cls.exe");
	system("color 0A");
	printf("%s         Welcome to Use English-Chinese Dictionary        %s\n",__TIME__,__DATE__);
	printf("\n★**☆**☆**☆**☆**☆**☆**☆** Main  Menu **☆**☆**☆**☆**☆**☆**☆**☆**★\n");
	printf("☆          1.显示所有单词                   2.添加单词信息                   ☆\n");
	printf("☆          3.查询指定单词                   4.修改指定单词                   ☆\n");
	printf("☆          5.删除指定单词                   6.保存单词库                     ☆\n");
	printf("☆                             7.退出词典                                     ☆\n");
	printf("★**★**★**★**★**★**★**★**★*★*★**★**★**★**★**★**★**★**★**★**★\n");
}
void SelectMenu()
{
	Link lHead = NULL;      /*定义链表*/
	char ch;
	int select;
	lHead = (Node*)malloc(sizeof(Node));
	lHead->next = NULL;
	system("color 0E");
	LoadWordDB(lHead);
	while(1)
	{
		system("cls");
		fflush(stdin);
		menu();
		printf("\n              Please Enter your choice(0~8): ");    /*显示提示信息*/
		scanf("%d",&select);

		if(select==7)
		{
			if(saveflag==1) /*若对链表的数据有修改且未进行存盘操作,则此标志为1*/
			{ 
				fflush(stdin);
				printf("\n=====>Whether save the modified record to file?(y/n):");
				scanf("%c",&ch);
				if(ch=='y'||ch=='Y')
				{
					Save(lHead);
				}
			}
			printf("=====>Thanks for your using, Have a Good Day!\n");
			getchar();
			break;
		}
		switch(select)
		{
		case 1:ShowWords(lHead);break;			    /*显示所有单词信息*/
		case 2:AddNewWord(lHead);break;             /*添加单词信息*/
		case 3:QueryWord(lHead);break;				/*查询指定单词*/
		case 4:ModifyWord(lHead);break;				/*修改指定单词*/
		case 5:DelWord(lHead);break;				/*删除指定单词*/
		case 6:Save(lHead);break;					/*保存对文件的修改*/
		default: Wrong();break;           /*按键有误,必须为数值0-8*/
		}
	}
}
void Wrong()
{
	printf("\n\n\n------------------Input error,press any key to continue-------------------\n");
	system("pause");
}

代码片段

#include <stdio.h>
#include "Dictionary.h"

int main(void)
{
	SelectMenu();
	return 0;
}

代码片段

// "Dicionary.h"
#ifndef _DICTIONARY_H_INCLUDE_
#define _DICTIONARY_H_INCLUDE_
#include "UserType.h"
void menu();
void LoadWordDB(Link lHead);
void SelectMenu();
void Wrong();
void Save(Link lHead);
void ShowWords(Link l);
void AddNewWord(Link lHead);
void DelWord(Link lHead);
void QueryWord(Link lHead);
void ModifyWord(Link lHead);
Node* SeekWord(Link lHead,char* word);
void StringInput(char* word,int len,char* notice);
#endif //_DICTIONARY_H_INCLUDE_

代码片段

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "Dictionary.h"
#include "UserType.h"
extern int saveflag;
void ShowWords(Link lHead)
{
	Node* p;
	system("cls");
	system("color 0F");
	p = lHead->next; /*该链表有头结点,不存东西,数据存放在后续结点中*/
	if(!p)
	{
		printf("\n=====>单词库中还没有单词!\n");
		system("pause");
		return;
	}else
	{
		while(p)    /*逐条输出链表中存储的学生信息*/
		{
			printf("%s\n%s.\t%s\n",p->data.word,p->data.attribute,p->data.meaning);
			p=p->next;  /*移动直下一个结点*/
		}
		system("pause");
	}
}
void AddNewWord(Link lHead)
{
	Node *p,*r,*s;  /*实现添加操作的临时的结构体指针变量*/
	char record[127];
	int flag = 0;
	char ch;
	r=lHead;
	system("color 0F");
	while(r->next!=NULL)
	{
		 r=r->next; /*将指针移至于链表最末尾,准备添加记录*/
	}
	while(1)
	{    //用于进行循环添加新单词
		system("cls.exe");
		while(1)
		{	/*输入单词,保证数据库中没有重复添加,输入0则退出添加单词操作*/
			system("cls.exe");			
			StringInput(record,16,"Input a new word(Press '0' return menu):");/*格式化输入单词并检验*/	
			if(strcmp(record,"0") == 0)
			{
				return;
			}			
			s = lHead->next;
			while(s)
			{
				if(strcmp(s->data.word,record) == 0)
				{   /*判断单词是否已经存在*/
					flag = 1;
					break;
				}
				s = s->next;
			}
			if(flag == 1)
			{
				printf("======>The word %s is already existing,add more?(y/n):",record);
				fflush(stdin);
				scanf("%c",&ch);
				if(ch == 'y'||ch == 'Y')
				{
					flag = 0;
					continue;
				}else
				{
					return;
				}
			}else
			{
				break;
			}
		}
		p = (Node*)malloc(sizeof(Node));
		if(!p)
		{
			printf(" memory malloc failure!\n");
			return;
		}
		memset(&p->data,0,sizeof(Node));
		strcpy(p->data.word,record);
		StringInput(record,11,"Word's attribute:");/*格式化输入单词词性并检验*/	
		strcpy(p->data.attribute,record);
		StringInput(record,128,"Word's meaning:"); /*格式化输入单词释义并检验*/	
		strcpy(p->data.meaning,record);
		p->next=NULL; /*表明这是链表的尾部结点*/
		r->next=p;  /*将新建的结点加入链表尾部中*/
		r=p;
		fflush(stdin); //清空输入缓冲区
		saveflag =1;
	}
	return;
}
void Save(Link lHead)
{
	FILE* pf = NULL;
	Node *p;
	pf = fopen("Dictionary.dbf","wb");
	p = lHead->next;
	if(pf != NULL)
	{
		while(p)
		{
			if(fwrite(p,sizeof(Node),1,pf) == 1)
			{
				p = p->next;
			}else
			{
				break;
			}
		}
		saveflag = 0;
		printf("\n\n\n=====>Save file complete!\n");
		system("pause");					
	}
	fclose(pf);
}
void LoadWordDB(Link lHead)
{
	FILE* pf = NULL;
	Node *p,*r;
	pf = fopen("Dictionary.dbf","ab+");
//	system("dir /b *.dbf");
	r = lHead;
	if( pf != NULL)
	{
		while(!feof(pf))
		{
			p = (Node*)malloc(sizeof(Node));
			if(!p)
			{
				printf(" memory malloc failure! \n");
				exit(0);
			}
			if(fread(p,sizeof(Node),1,pf) == 1)
			{
				p->next = NULL;
				r->next = p;
				r = p;
			}
		}
		fclose(pf);
		printf("\n\n\t\t=====> Word database load successfully! <=====\n");
		printf("\n\t===========> Welcome to Use English-Chinese Dictinary <===========\n");
		printf("\n\t==               Producer :             杨琳琳、李瑞莹        ==\n");
		printf("\n\t==     Product Identifier :             131120130406-01       ==\n");
		printf("\n\t==	 版权所有,侵权必究  侵权举报电话:18700100913	      ==\n\n\t===========>");
		system("pause");
	}else
	{
		printf("Bad Words Database!\n");
		system("pause");
	}
}
void DelWord(Link lHead)
{
	Node *p,*r;
	char word[20];
	system("cls.exe");
	system("color 0F");
	StringInput(word,16,"Input a word to delete(Press '0' return menu):");	
	if(strcmp(word,"0") == 0)
	{
		return;
	}	
	p = SeekWord(lHead,word);
	if(p)
	{
		r = lHead;
		while(r->next != p)
		{
			r = r->next;
		}
		r->next = p->next;
		free(p);
		saveflag = 1;
		printf("\n\n=====>Delete word successfully!<=====\n");
		system("pause");
	}else
	{
		printf("\n\n=====>Sorry,Not Found!<=====\n");
		system("pause");
	}
}
Node* SeekWord(Link lHead,char* word)
{
	Node *p;
	p = lHead->next;
	while(p)
	{
		if(strcmp(p->data.word,word) == 0)
		{
			break;
		}else
		{
			p = p->next;
		}
	}
	return p;
}
void QueryWord(Link lHead)
{
	Node *p;
	char word[20];
	system("cls.exe");
	system("color 0F");
	StringInput(word,16,"Input a word to query(Press '0' return menu) : ");	
	if(strcmp(word,"0") == 0)
	{
		return;
	}	
	p = SeekWord(lHead,word);
	if(p)
	{
		printf("%s  %s.\t%s\n",p->data.word,p->data.attribute,p->data.meaning);
		system("pause");
	}else
	{
		printf("\n\n=====>Sorry,Not Found!<=====\n");
		system("pause");
	}
}
void ModifyWord(Link lHead)
{
	Node *p;
	char word[20];
	system("cls.exe");
	system("color 0F");
	StringInput(word,16,"Input a word to modify(Press '0' return menu):");
	if(strcmp(word,"0") == 0)
	{
		return ;
	}
	p = SeekWord(lHead,word);
	if(p == NULL)
	{
		printf("\n\n=====>Sorry,Not Found!<=====\n");
		system("pause");
		return ;
	}
	printf("Change word to : ");
	scanf("%s",word);
	strcpy(p->data.word,word);
	printf("Change attribute to : ");
	scanf("%s",word);
	strcpy(p->data.attribute,word);
	printf("Change meaning to : ");
	scanf("%s",word);
	strcpy(p->data.meaning,word);
	fflush(stdin);
	saveflag = 1;
	printf("\n\n=====>Modify word successfully!<=====\n");
	system("pause");
}
void StringInput(char* word,int len,char* notice)
{
	char tmp[127];
	do
	{
		printf(notice);
		scanf("%s",tmp);
		if(strlen(tmp) > len)
		{
			printf("\n=====>Exceed the required length!(1-%d)<=====\n",len);
		}
	}while(strlen(tmp) > len);
	strcpy(word,tmp);
}

代码片段

// "UserType.h"
#ifndef _USER_TYPE_H_INCLUDE_
#define _USER_TYPE_H_INCLUDE_
/*定义与字典有关的数据结构*/
typedef struct Dictionary
{
	char word[16];  /*单词*/
	char attribute[11]; /*单词的词性*/
	char meaning[128]; /*单词的释义*/
};
/* 定义每条记录的数据结构,标记为node */
typedef struct node
{
	struct Dictionary data; /*数据域*/
	struct node *next; /*指针域*/
}Node,*Link;  /* Node为node类型的结构变量,*Link为node类型的指针变量 */

#endif //_USER_TYPE_H_INCLUDE_

转载注明地址:http://www.chengxuyuans.com/code/C++/62168.html

推荐文章