题目:在一篇英文文章中查找指定的人名,人名使用二十六个英文字母(可以是大写或小写)、空格以及两个通配符组成(*、?),通配符“*”表示零个或多个任意字母,通配符“?”表示一个任意字母。
如:“J* Smi??” 可以匹配“John Smith” .
请用C语言实现如下函数:
void scan(const char* pszText, const char* pszName);
注:pszText为整个文章字符,pszName为要求匹配的英文名。
请完成些函数实现输出所有匹配的英文名,除了printf外,不能用第三方的库函数等。
#include<iostream.h>
const char *pEnd=NULL; //记录匹配成功后结束的位置,方便输出
bool match(const char *pszText,const char *pszName)
{
if(*pszName=='\0') //匹配到字符串结尾,匹配成功
{
pEnd=pszText;
return true;
}
if(*pszText=='\0') //文本到达末尾
{
if(*pszName=='*') //如果字符串末尾为*则说明可以匹配
//否则,匹配失败
{
pEnd=pszText;
return true;
}
return false;
}
if(*pszName!='*'&&*pszName!='?')
{
if(*pszText==*pszName)
return match(pszText+1,pszName+1);
return false;
}
else
{
if(*pszName=='*')
return match(pszText,pszName+1)||match(pszText+1,pszName);//继续匹配文本的0个或者多个字符
else
return match(pszText+1,pszName+1);
}
}
void scan(const char *pszText,const char *pszName)
{
while(*pszText!='\0')
{
if(match(pszText,pszName))
{
while(pszText!=pEnd)
cout<<*pszText++;
cout<<endl;
}
pszText++;
}
}
void main()
{
char psztext[]="we are the world,we are the child?John Smith say.but the Jay Smisi",
pszname[]="J* Smi??";
scan(psztext,pszname);
}