?? stringmatch.h
字號:
#include<fstream.h>
const float CAP_MISMATCH_VAL = 0.9f;
/*---------------------------------------------------------
**函數功能:檢測兩個字符串的匹配相似度
**函數參數:*left和*right為兩個比較的字符串
**返 回 值:bool型 true讀取成功, false讀取失敗
**編 寫 人:qidajiang
**時 間:2008-7-20
---------------------------------------------------------*/
//本函數會返回一個介于0到1之間的浮點值,表示兩個字符串
//之間匹配度的一個近似百分比
float stringMatch(char const *left, char const *right)
{
// 獲取左右兩個字符串,能最長串的長度
int leftSize = strlen(left);
int rightSize = strlen(right);
int largerSize = (leftSize > rightSize) ?
leftSize : rightSize;
char const *leftPtr = left;
char const *rightPtr = right;
float matchVal = 0.0f;
// 對左側的字符串進行迭代操作,直到字符串的最后一個字符
while(leftPtr != (left + leftSize) &&
rightPtr != (right + rightSize))
{
// 首先,我們進行一個簡單的左右匹配檢測
if(*leftPtr == *rightPtr)
{
// 如果匹配,就向匹配總值加上這個字符的百分比值
matchVal += 1.0f / largerSize;
++leftPtr;
++rightPtr;
}
// 如果匹配失敗,進行一次忽略大小寫的匹配檢測
else if(::tolower(*leftPtr) == ::tolower(*rightPtr))
{
matchVal += CAP_MISMATCH_VAL / largerSize;
++leftPtr;
++rightPtr;
}
else
{
char const *lpbest = left + leftSize;
char const *rpbest = right + rightSize;
int totalCount = 0;
int bestCount = INT_MAX;
int leftCount = 0;
int rightCount = 0;
// 這里我們在外層循環中遍歷整個左字串
// 但是為了確保不越過我們當前的最佳數量(bestCount)
// 會進行提前退出循環的條件檢測
for(char const *lp = leftPtr; (lp != (left + leftSize)
&& ((leftCount + rightCount) < bestCount)); ++lp)
{
for(char const *rp = rightPtr; (rp != (right +
rightSize) && ((leftCount + rightCount) <
bestCount)); ++rp)
{
// 在這里,我們不考慮大小寫
if(::tolower(*lp) == ::tolower(*rp))
{
totalCount = leftCount + rightCount;
if(totalCount < bestCount)
{
bestCount = totalCount;
lpbest = lp;
rpbest = rp;
}
}
++rightCount;
}
++leftCount;
rightCount = 0;
}
leftPtr = lpbest;
rightPtr = rpbest;
}
}
// 為了防止浮點出錯,作數值范圍限定
if(matchVal > 0.99f)
matchVal = 1.0f;
else if(matchVal < 0.01f)
matchVal = 0.0f;
return matchVal;
}
/*---------------------------------------------------------
**函數功能:從指定文件中讀取字符串,存入data數組
**函數參數:data存放字符串的數組,fileName讀取的文件名(可含路徑)
**返 回 值:bool型 true讀取成功, false讀取失敗
**編 寫 人:qidajiang
**時 間:2008-7-20
---------------------------------------------------------*/
bool readFileDatalie(char str[1000][1000], char *fileName,int *pNum)
{
bool result;
ifstream fis;
fis.open(fileName,ios::in,0);
if( !fis )
{
cout<<"Error:Can't open file!";
result = false;
}
else
{
*pNum = 0;
char textline[1000];
while(!fis.eof())
{
//memset (textline,0,sizeof(textline));
fis.getline(textline,sizeof(textline));
//cout<<"textline="<<textline<<endl;
strcpy(str[(*pNum)++],textline);
}
fis.close();
result = true;
}
return result;
}
/*---------------------------------------------------------
**函數功能:把單個字符串寫入文件
**函數參數:fileName讀取的文件名(可含路徑),strtmp寫入的字符串
**返 回 值:bool型 true讀取成功, false讀取失敗
**編 寫 人:qidajiang
**時 間:2008-7-20
---------------------------------------------------------*/
bool writeSigleData(char *fileName, char *strtmp)
{
bool result;
fstream fos;
fos.open(fileName,ios::app,0);
if( !fos )
{
cout<<"Error:Can't open file!";
result = false;
}
else
{
//fseek(fp, SEEK_END, SEEK_CUR);
fos<<strtmp<<endl;
fos.close();
result = true;
}
return result;
}
/*---------------------------------------------------------
**函數功能:匹配函數,檢測符合條件的字符串
**函數參數:instr輸入待查詢的字符串,str文本中的字符串,s文本中字符串的個數,limit限制比較參數
**返 回 值:bool型 true讀取成功, false讀取失敗
**編 寫 人:qidajiang
**時 間:2008-7-20
---------------------------------------------------------*/
void tryMatch(char instr[1000],char str[1000][1000],int s,float limit)
{
float reslult=0.0f;
int coumt=0;
for (int i=0;i<s;i++)
{
reslult=stringMatch(instr,str[i]);
cout<<"i = "<<i<<"時"<<"result = "<<reslult<<endl;
if(reslult>limit)
{
//cout<<"The two String match!"<<endl;
//cout<<"reslult= "<<reslult<<endl;
cout<<"the string "<< instr <<" maching is "<< str[i]<<endl;
writeSigleData("test1.txt",str[i]);
coumt++;
}
else
{
//cout<<"The two String don't match!"<<endl;
}
}
if(coumt==0)
{
cout<<"the string "<< instr <<" isn't string maching! "<<endl;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -