📄 sol.h
字号:
#include"linklist.h"
int dicin(char* argv,linklist* hash) //将dic.txt中的所有词存入hash表(前两字相同,存入同一链表)
{int n,max=0;
long i;
string temp;
ifstream dic(argv,ios_base::in);
while(dic.good())
{getline(dic,temp);
n=temp.length();
if(n>max) max=n; //max存放最长词的字节数
i=temp[0]*temp[1]+temp[2]*temp[3]; //temp[k]的取值范围是-128 — -1,0<i<=2*128*128
hash[i].insert(temp); //在hash表对应的地址处,插入读到的单词
}
dic.close();
return max;
}
void dicdel(char* argv,linklist* hash) //将Deldic.txt中的词依次读取,并从hash表中删除
{long i;string temp;
ifstream dell(argv,ios_base::in);
while(dell.good())
{getline(dell,temp);
i=temp[0]*temp[1]+temp[2]*temp[3]; //取词对应的hash表地址
hash[i].del(temp); //删除表中的词temp
}
dell.close();
}
long last(string &a) //取string a中第一个非标点(即汉字)字符的位置
{ long m=a.length()-2;
string ch=a.substr(m,2);
while(ch==","||ch=="。"||ch==";"||ch=="!"||ch=="?"||ch==":"||ch=="“"||ch=="”"||ch=="、")
{m=m-2;
ch=a.substr(m,2);
} //从后往前查找,ch非标点时跳出
return m;
}
void segout(char* argv,int max,string& chr,linklist* hash)
{long i,j=0;
long m=last(chr); //m记录第一个非标点字符的位置
string cr,ch,temp;
ofstream sl(argv,ios_base::out); //sl为输出流
for(i=0;i<chr.length();i=i+2)
{ch=chr.substr(i,2);
if(ch==","||ch=="。"||ch==";"||ch=="!"||ch=="?"||ch==":"||ch=="“"||ch=="”"||ch=="、")
{cr=chr.substr(j,i-j); //遇标点,截取出纯汉字段,赋给cr
j=i+2; //j记录该标点后,字符串的起始位置
int n=(max<cr.length())? max:cr.length(); //取字段长与最大词长的较小值赋给n
while(n>2) //循环进行前向最大匹配,直到单字跳出
{long k=cr[0]*cr[1]+cr[2]*cr[3]; //计算hash表地址
temp.assign(hash[k].search(cr,n));
sl<<temp;
cr=cr.substr(temp.length(),cr.length()-temp.length());
n=(max<cr.length())? max:cr.length();
if(j<=m||n!=0) sl<<endl; //该字段未处理完,或字段后还有汉字,输出回车
}
if(n==2) //单字输出
{sl<<cr;
if(j<=m) sl<<endl;
}
}
}
sl.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -