词典设计(C++)

这个代码是老师给我们展示的例子,我拿来直接用了,注意下这里需要建立项目,因为需要使用外部的一个文本文件,具体情况看代码吧。

#include
#include
#include
#define N 1000
using namespace std;
//定义单词结构体
typedef struct Words
{
	char English[20];//存储单词
	char Chinese[80];//存储汉语解释
}Wds;

int MainContent();//用于显示主菜单
void Process(int option);//根据用户的选择进行处理
void Search(char wd[20]);//搜索单词
void Change(char wd[20]);//修改单词
void Add();//添加单词;
int ChangeContent();//修改单词目录;
int SecondContent(int i,int num);//用于分页显示,显示菜单

int main()
{
	int option=0;//记录用户的选择
    int i;
    do
	{

		i=0;//标记用户的输入是否符合要求
        option=MainContent();
        i=option<=0||option>4;//检验
        if(i)
		{
			cout << "输入的序号不合要求!" << endl;
			fflush(stdin);
			cout << "按任意键返回......";
			getchar();
			system("cls");
			continue;
		}
		Process(option);
	}while(i||option!=4);
	cout << "已退出!" << endl;
	return 0;
}

int MainContent()
{
	int option;//记录用户的选择
	system("cls");
	cout << "___________________________________" << endl;
	cout << "        1    查找                  " << endl;
	cout << "        2    增加                  " << endl;
	cout << "        3    修改                  " << endl;
	cout << "        4    退出                  " << endl;
	cout << "___________________________________" << endl;
	cout << endl;
	cout << "请选择:" << endl;
	cin >> option;
	return option;
}

void Process(int option)
{
	char wd[20];
	switch(option)
	{
	 case 1:
		 {
			 cout << "请输入单词:";
			 cin >> wd;         
			 Search(wd);//"###"表示用户未输入
			 break;
		 }
	 case 2:
		 {
			 Add();
			 break;
		 }
	 case 3:
		 {
			 cout << "请输入单词:";
			 cin >> wd;
			 Change(wd);
		 }
	}
}

void Search(char wd[20])//wd[20]接收用户输入的单词
{
	FILE *fp;
	Wds word[N];
    int i,n,num=0;
    int flag=0;//用于标记单词想法是否查到。
	int flag1=0;//用于标记是否有相关的单词。
	n=strlen(wd);
	if((fp=fopen("Dictionary.txt","r+"))==NULL)
	{
		cout << "打开文件出错!" << endl;
		exit(1);
	}
	while(!flag&&!feof(fp))
	{
		fscanf(fp,"%s\n%s\n",word[num].English,word[num].Chinese);
		if(strncmp(word[num].English,wd,n)==0)
		{
			flag1=1;
			if(strlen(word[num].English)==n)
            {
				
				flag=1;
				break;
			}
			num++;
		}	
	}
	rewind(fp);
	fclose(fp);
	if(flag1==0)
	{
		cout << "无相关单词!" << endl;
		fflush(stdin);
		cout << "按任意键返回......";
		getchar();
		system("cls");
		return;

	}
	if(flag==1)
	{
		//输出要查找的单词的解释
		cout << "解释:" << word[num].Chinese << endl;
		fflush(stdin);
		cout << "按任意键返回主菜単......";
		getchar();
		return;
	}
	if(flag==0)
	{
		int option=0;
		i=0;
		system("cls");
		cout << "你要查找的是这些单词吗?" << endl;
		//实现分页显示
	    while(i10&&i==num))/*(i%10==0&&i!=0)已输出一页;(i>10&&i==num)已输出最后一页且总页数>1;*/
			{
				option=SecondContent(i,num);
				if(option==1)
					system("cls");
				else if(option==2)
				{  
					system("cls");
					if(i==num)
						i=i-(10+(num%10));
					else
						i=i-20;
				}
				else if(option==3)
				{
					cout << endl;
					cout << "请输入单词序号:";
					cin >> i;
					if(i<=0||i>num)
					{
						cout << endl;
						cout << "输入的序号不合要求!" << endl; 
						fflush(stdin);
						cout << "按任意键返回主菜単......";
						getchar();
						system("cls");
					}
					Search(word[i-1].English);
					return;
				}
				else if(option==4)
					return;
			}
		}
	}
	cout << endl;
	cout << "请输入单词序号:";
	cin >> i;
	if(i<=0||i>num)
	{
		cout << endl;
		cout << "输入的序号不合要求!" << endl;
		fflush(stdin);
		cout << "按任意键返回主菜単......";
		getchar();
		system("cls");
	}
	Search(word[i-1].English);
	return;
}

int SecondContent(int i,int num)
{
	
    char ch='x';//接收用户的选择
	cout << endl;
	cout << "___________________________________" << endl;
	cout << "        >   下一页                 " << endl;
	cout << "        <   上一页                 " << endl;
	cout << "        *   输入单词左边的序号     " << endl;
	cout << "        #   返回主菜单             " << endl;
	cout << "___________________________________" << endl;
	cout << endl;
	cout << "请选择:";
	do
	{
		fflush(stdin);
		ch=getchar();
		if(ch=='>'&&i10)
		{
			putchar(ch);
			return 2;
		}
		else if(ch=='*')
		{
			putchar(ch);
			return 3;
		}
		else if(ch=='#')
		{
			putchar(ch);
			return 4;
		}
	}while(!(ch=='>'&&i10)&&ch!='*'&&ch!='#');
}

void Change(char wd[20])
{
	FILE *fp;
	Wds word[N];
    int i,num=0;	
	int flag=0;//用于标记单词想法是否查到。    
	if((fp=fopen("Dictionary.txt","r+"))==NULL)
	{
		cout << "打开文件出错!";
		exit(1);
	}
	while(!flag&&!feof(fp))
	{
		fscanf(fp,"%s\n%s\n",word[num].English,word[num].Chinese);
		num++;
		if(strcmp(word[num-1].English,wd)==0)
		{
		        flag=1;
				break;
		}
	
	}
	if(flag==1)
	{
		char str[80];
		int  opt;
		system("cls");
		cout << "原单词:" << word[num-1].English << endl;
		cout << "原解释:" << word[num-1].Chinese << endl;
		cout << endl;		
		switch(ChangeContent())//输出目录根据用户的选择进行处理
		{
		case 1:
			{
				cout << endl;
				cout << "输入新解释:" << endl;
				getchar();
				gets(word[num-1].Chinese);
				break;
			}
		case 2:
			{
				cout << endl;
				cout << "输入追加的解释:" << endl;
				getchar();
				gets(str);
				strcat(word[num-1].Chinese,str);
				break;
			}
		case 3:
			{
				cout << endl;
				cout << "输入新单词:" << endl;
				getchar();
				gets(word[num-1].English);
				puts(word[num-1].English);
				cout << endl;
				cout << "输入新解释:" << endl;
				//getchar();
				gets(word[num-1].Chinese);
				puts(word[num-1].Chinese);
				break;
			}
		case 4:
			{
				rewind(fp);
				fclose(fp);
				return;
			}
		}
	   if(strlen(word[num-1].English)==0||strlen(word[num-1].Chinese)==0)
	   {
		   cout << "单词、解释均不准为空!" << endl;
		   fflush(stdin);
		   cout << "按任意键返回主菜単......";
		   getchar();
		   system("cls");
		   return;
	   }
	   cout << endl;
	   cout << '\t' << "1  保存" << '\t' << "2  取消" << endl;
	   cout << "请选择:";
	   cin >> opt;
	   if(opt==1)
	   {
		   rewind(fp);
		   for(i=0;i> opt;	
	if(opt==1)
	{
		if((fp=fopen("Dictionary.txt","r+"))==NULL)
		{
			cout << "打开文件出错!" << endl;
			exit(1);
		}
		fseek(fp,0,SEEK_END);
        fprintf(fp,"%s\n%s\n",word.English,word.Chinese);
		rewind(fp);
		fclose(fp);
		cout << "保存成功!" << endl;
		fflush(stdin);
		cout << "按任意键返回主菜単......";
		getchar();
		system("cls");
	}
	else 
	{
		cout << "未保存!";
		fflush(stdin);
		cout << "按任意键返回主菜単......";
		getchar();
		system("cls");
	}
}

 

你可能感兴趣的:(综合项目)