?? 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) //統(tǒng)計文件的行數(shù)
{
int counter=0;
char buf[225];
while(!in.eof())
{
in.getline(buf,sizeof(buf)); //把文件中一行的數(shù)據(jù)放入數(shù)組buf中
counter++;
}
return counter;
}
BOOLEAN Dictionary::init() //初始化數(shù)據(jù)成員
{
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]) //繼續(xù)比較后繼字符
{
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() //根據(jù)英文單詞查詢其詞性和中文解釋
{
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() //根據(jù)中文查詢其英文和詞性
{
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); //調用函數(shù)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--; //記錄數(shù)減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); //調用函數(shù)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 + -