高分请教,可有现成,简单的字串通配算法,仅像文件搜索那样支持*和?即可(*表示任意个数,任意字符,?代表单个任意) 例如*abc?k123abc9k(满足要求)在网上也找到了一些例程,大多逻辑复杂,完全没有优雅二字.我又想偷懒,请高手成全,谢谢!(粘上代码才给分) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 贴一段以前的代码,优雅说不上,功能能实现。//// 分解字符串void prsString(CString str, CString & strTemp1, CString & strTemp2){ CString strTemp = str.SpanIncluding("*?"); strTemp = str.Mid(strTemp.GetLength()); strTemp1 = strTemp.SpanExcluding("*?"); strTemp2 = strTemp.Mid(strTemp1.GetLength());}//// 比较字符串strS和带有通配符的字符串strBOOL compare(CString str, CString strS){ BOOL bRetVal = 0; int nPos; CString strTemp,strTemp2,strSTemp,strSTemp2; if (str.IsEmpty() && strS.IsEmpty()) return TRUE; if (str.Find('?') == 0) { bRetVal = compare(str.Mid(1), strS.Mid(1)); if (!bRetVal) { return FALSE; } else return TRUE; } if (str.Find('*') == 0) { strSTemp = strS; prsString(str, strTemp, strTemp2); while((strSTemp.GetLength() > 0)) { nPos = strSTemp.Find(strTemp); if (-1 == nPos) { return FALSE; } strSTemp2 = strSTemp.Mid(nPos + strTemp.GetLength()); if (strTemp2.IsEmpty() && strSTemp2.IsEmpty()) { return TRUE; } bRetVal = compare(strTemp2, strSTemp2); if (bRetVal) { return TRUE; } strSTemp = strSTemp.Mid(nPos + 1); } return FALSE; } prsString(str, strTemp, strTemp2); if (strTemp2.IsEmpty()) { bRetVal = strS.Compare(strTemp); if (bRetVal || (strS.GetLength() != str.GetLength())) { return FALSE; } else return TRUE; } strSTemp = strS.Left(strTemp.GetLength()); if (strSTemp.Compare(strTemp)) { return FALSE; } strSTemp = strS.Mid(strSTemp.GetLength()); bRetVal = compare(strTemp2, strSTemp); return bRetVal;} 可以看看<<程序设计实践>>那本书,后面有个章节提供了一个实现.用递归.网上可以下载这个书的电子版本 VC的Menu名字中如何显示“&”? 求vc++.net高手 编译出错 高分求解 ISAPI的中文参数乱码问题 使用CHtmlView这个显示网页时,为什么占用那么大的内存啊。 这是怎么回事啊? 求可将录音内容直接保存成MP3的VC程序,谢谢!!! 如何向别的程序的菜单发送消息? 有谁知道RS232,RS485,RS422,RS423的原理?? 内存泄漏问题 两个问题:ODBC怎么联表查询;ADO能放到一个辅助线程中运行吗? *********两天了,还是没有搞懂 IMPLEMENT_SERIAL(CObPoint,CObject,0) 为什么出错
void prsString(CString str, CString & strTemp1, CString & strTemp2)
{
CString strTemp = str.SpanIncluding("*?");
strTemp = str.Mid(strTemp.GetLength()); strTemp1 = strTemp.SpanExcluding("*?");
strTemp2 = strTemp.Mid(strTemp1.GetLength());
}//// 比较字符串strS和带有通配符的字符串str
BOOL compare(CString str, CString strS)
{
BOOL bRetVal = 0;
int nPos;
CString strTemp,strTemp2,strSTemp,strSTemp2; if (str.IsEmpty() && strS.IsEmpty())
return TRUE;
if (str.Find('?') == 0)
{
bRetVal = compare(str.Mid(1), strS.Mid(1));
if (!bRetVal)
{
return FALSE;
}
else
return TRUE;
}
if (str.Find('*') == 0)
{
strSTemp = strS;
prsString(str, strTemp, strTemp2); while((strSTemp.GetLength() > 0))
{
nPos = strSTemp.Find(strTemp); if (-1 == nPos)
{
return FALSE;
}
strSTemp2 = strSTemp.Mid(nPos + strTemp.GetLength());
if (strTemp2.IsEmpty() && strSTemp2.IsEmpty())
{
return TRUE;
} bRetVal = compare(strTemp2, strSTemp2);
if (bRetVal)
{
return TRUE;
}
strSTemp = strSTemp.Mid(nPos + 1);
}
return FALSE;
} prsString(str, strTemp, strTemp2); if (strTemp2.IsEmpty())
{
bRetVal = strS.Compare(strTemp);
if (bRetVal || (strS.GetLength() != str.GetLength()))
{
return FALSE;
}
else
return TRUE;
} strSTemp = strS.Left(strTemp.GetLength());
if (strSTemp.Compare(strTemp))
{
return FALSE;
} strSTemp = strS.Mid(strSTemp.GetLength());
bRetVal = compare(strTemp2, strSTemp); return bRetVal;
}
网上可以下载这个书的电子版本