📄 dictionary.cpp
字号:
#include<fstream.h>
#include<string.h>
#include<iomanip.h>
#include"Dictionary.h"
Dictionary::Dictionary()
{
danci=NULL;
num=0;
return;
}
Dictionary::~Dictionary()
{
if(danci!=NULL)
delete danci;
return;
}
int Dictionary::LineCount(ifstream in) //统计文件的行数
{
int counter=0;
char buf[225];
while(!in.eof())
{
in.getline(buf,sizeof(buf)); //把文件中一行的数据放入数组buf中
counter++;
}
return counter;
}
BOOLEAN Dictionary::init() //初始化数据成员
{
ifstream input_file;
int loop;
input_file.open("dic_file.txt");
if(!input_file)
{
cout<<"该文件打开不成功!"<<endl;
return FALSE;
}
num=LineCount(input_file);
danci=new word[num];
input_file.seekg(0,ios::beg); //文件的读指针重新指向文件开始
for(loop=0;loop<num;loop++)
input_file>>(danci+loop)->English>>(danci+loop)->cixing>>(danci+loop)->Chinese;
input_file.close();
return TURE;
}
int Dictionary::Index(char* s,char* t) //若t为s的子串,则返回t的第一个字符在s中的位置;
{ //否则,返回0
int pos,length_s,length_t,i,j;
length_s=strlen(s);
length_t=strlen(t);
if(length_s>=length_t)
{
i=0;
j=0;
while(i<=length_s-1&&j<=length_t-1)
{
if(s[i]==t[j]) //继续比较后继字符
{
i++;
j++;
}
else //指针后退重新开始匹配
{
i=i-j+1;
j=0;
}
}
if(j>=length_t)
pos=i-length_t+1;
if(j<length_t)
pos=0;
}
else
pos=0;
return pos;
}
void Dictionary::SelectEnglish() //根据英文单词查询其词性和中文解释
{
char* string;
int loop,temp=0;
string=new char[MAXSTR];
cout<<"请输入要查找的单词:"<<endl;
cin>>string;
for(loop=0;loop<num;loop++) //顺序查找与输入相同的单词
{
if(strcmp(string,(danci+loop)->English)==0)
{
cout<<danci+loop; //调用了重载的插入符
break;
}
temp++;
}
if(temp==num)
cout<<"该单词在词典中不存在."<<endl;
delete string;
return;
}
void Dictionary::SelectChinese() //根据中文查询其英文和词性
{
char* string;
int loop,i,j,temp=0;
char ch;
string=new char[MAXSTR];
cout<<"请输入中文:"<<endl;
cin>>string;
for(loop=0;loop<num;loop++) //顺序查找包含了输入的中文
{
i=Index((danci+loop)->Chinese,string); //调用函数Index()
if(i)
{
j=i+strlen(string)-1; //j是string的最后一个字符在某单词中的位置
ch=(danci+loop)->Chinese[j];
if((i==1&&ch==';')||(i==1&&ch=='\0')||(i!=1&&(danci+loop)->Chinese[i-2]==';'&&ch==';')||(i!=1&&(danci+loop)->Chinese[i-2]==';'&&ch=='\0'))
temp++;
}
}
if(temp==0)
cout<<"词典里没有对("<<string<<")的英文解释"<<endl;
else
{
cout<<setw(6)<<"英文解释"<<setw(15)<<"词性"<<endl;
for(loop=0;loop<num;loop++)
{
i=Index((danci+loop)->Chinese,string);
if(i)
{
j=i+strlen(string)-1; //j是string的最后一个字符在某单词中的位置
ch=(danci+loop)->Chinese[j];
if((i==1&&ch==';')||(i==1&&ch=='\0')||(i!=1&&(danci+loop)->Chinese[i-2]==';'&&ch==';')||(i!=1&&(danci+loop)->Chinese[i-2]==';'&&ch=='\0'))
cout<<setw(6)<<(danci+loop)->English<<setw(15)<<(danci+loop)->cixing<<endl;
}
}
}
delete string;
return;
}
int Dictionary::Add() //添加一条记录,若成功添加则返回1,否则返回0
{
word *str,*m;
int loop,temp=0;
str=new word;
if(num<MAXSIZE)
{
cout<<"请输入新单词:"<<endl;
cin>>str->English;
for(loop=0;loop<num;loop++) //查看是否有与输入相同的单词
{
if(strcmp(str->English,(danci+loop)->English)==0)
break;
temp++;
}
if(temp==num) //若不存在,则添加在最后
{
cin>>str; //调用了重载的提取符
m=danci+num;
m=new word;
strcpy((danci+num)->English,str->English);
strcpy((danci+num)->cixing,str->cixing);
strcpy((danci+num)->Chinese,str->Chinese);
num++;
cout<<"添加成功!"<<endl;
delete str;
return 1;
}
else
cout<<"该单词在词典中已存在。"<<endl;
}
else
cout<<"词典已满,添加失败。"<<endl;
delete str;
return 0;
}
int Dictionary::Delete() //删除记录,若成功删除则返回1,否则返回0
{
char* string;
int loop,temp=0,i;
string=new char[MAXSTR];
if(num>0)
{
cout<<"请输入要删除的单词:"<<endl;
cin>>string;
for(loop=0;loop<num;loop++) //顺序查找与输入相同的单词
{
if(strcmp(string,(danci+loop)->English)==0)
break;
temp++;
}
if(temp<num) //若存在,则将其删除
{
for(i=temp;i<num-1;i++)
{
strcpy((danci+i)->English,(danci+i+1)->English);
strcpy((danci+i)->cixing,(danci+i+1)->cixing);
strcpy((danci+i)->Chinese,(danci+i+1)->Chinese);
}
num--; //记录数减1
cout<<"删除成功!"<<endl;
delete string;
return 1;
}
else
cout<<"该单词在词典中不存在!"<<endl;
}
else
cout<<"词典已空。无法进行删除操作!"<<endl;
delete string;
return 0;
}
int Dictionary::Update() //修改记录,若成功修改则返回1,否则返回0
{
word* str;
int loop,temp=0,j;
str=new word;
cout<<"请输入要修改的单词:"<<endl;
cin>>str->English;
for(loop=0;loop<num;loop++) //顺序查找与输入相同的单词
{
if(strcmp((danci+loop)->English,str->English)==0)
break;
temp++;
}
if(temp<num) //若存在,则用新的词性和中文取代旧的
{
j=temp;
cout<<"请输入新的词性:"<<endl;
cin>>str->cixing;
cout<<"请输入新的中文解释:"<<endl;
cin>>str->Chinese;
strcpy((danci+j)->cixing,str->cixing);
strcpy((danci+j)->Chinese,str->Chinese);
delete str;
return 1;
}
else
cout<<"该单词在词典中不存在!"<<endl;
delete str;
return 0;
}
void Dictionary::ImageSelect() //查询以某一前缀开头的单词的词性和中文解释
{
char* string;
int loop,temp=0,i;
string=new char[MAXSTR];
cout<<"请输入单词的前缀:"<<endl;
cin>>string;
for(loop=0;loop<num;loop++)
{
i=Index((danci+loop)->English,string);
if(i==1)
temp++;
}
if(temp==0)
cout<<"词典中不存在以"<<string<<"为前缀的单词."<<endl;
else
{
cout<<"以"<<string<<"为前缀的单词显示如下:"<<endl;
cout<<setw(6)<<"英文"<<setw(15)<<"词性"<<setw(15)<<"中文"<<endl;
for(loop=0;loop<num;loop++)
{
i=Index((danci+loop)->English,string); //调用函数Index()
if(i==1)
cout<<setw(10)<<(danci+loop)->English<<setw(10)<<(danci+loop)->cixing<<setw(20)<<(danci+loop)->Chinese<<endl;
}
}
delete string;
return;
}
void Dictionary::Show() //显示所有记录
{
int loop;
cout<<"词典所有记录显示如下:"<<endl;
cout<<setw(6)<<"英文"<<'\t'<<setw(15)<<"词性"<<'\t'<<setw(15)<<"中文"<<'\t'<<endl;
for(loop=0;loop<num;loop++) //顺序输出所有单词
cout<<setw(10)<<(danci+loop)->English<<'\t'<<setw(10)<<(danci+loop)->cixing<<'\t'<<setw(20)<<(danci+loop)->Chinese<<'\t'<<endl;
return;
}
ostream& operator << (ostream& stream,word* w) //重载插入符
{
stream<<"该单词的词性为:"<<endl;
stream<<w->cixing<<endl;
stream<<"该单词的中文解释为:"<<endl;
stream<<w->Chinese<<endl;
return stream;
}
istream& operator >> (istream& stream,word* w) //重载提取符
{
cout<<"请输入该单词的词性:"<<endl;
stream>>w->cixing;
cout<<"请输入该单词的中文解释:"<<endl;
stream>>w->Chinese;
return stream;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -