Problem statementYou must work out a super string class, String, that is derived from the C++ standard class string. This derived String class must add two new member functions1; a pattern recognition function that returns the number of occurrences of a string pattern in a string. This function must use operator overloading of the / (division) operator.
2; a function get_token that returns tokens extracted from the stringA token is a substring occurring between space characters in the string or between space characters and the end of the string. The string " aaa bbb cc " has the tokens "aaa", "bbb", and "cc" . When the function is called the first time, it must return "aaa", the next time "bbb", and then "cc". When it is called the 4th time it must return an empty string, and when it is called the 5th time it starts all over returning "aaa". Optionally, you may extend the solution so that tokens may be separated by any character out of a set of character given as a string argument.
2; a function get_token that returns tokens extracted from the stringA token is a substring occurring between space characters in the string or between space characters and the end of the string. The string " aaa bbb cc " has the tokens "aaa", "bbb", and "cc" . When the function is called the first time, it must return "aaa", the next time "bbb", and then "cc". When it is called the 4th time it must return an empty string, and when it is called the 5th time it starts all over returning "aaa". Optionally, you may extend the solution so that tokens may be separated by any character out of a set of character given as a string argument.
int String::operator/ (const String& sub)
{
if(sub.IsEmpty())
return 0;
int count=0;//sub在字符串中的出现次数count
int ret=Find(sub);//辅助变量ret
if(ret==-1)
return 0;
else if(ret<=GetLength())
{
do
{
count++;
ret=Find(sub,GetAt(ret));
}
while(ret!=-1)
}
return count;
}
CString get_token()
{
static int callednum=0;//callednum纪录该函数的被调用次数
int totalnum=operator/(' ');//totalnum是空格的总个数
if(totalnum==0)
return NULL;
int tokennum,ret1=0,ret2=0;//tokennum是的token的总个数
while((ret1=Find(' ',ret2))!=-1 &&((ret2=Find(' ',ret1))!=-1)
{
if(ret1==ret2-1)
totalnum--;//两个相邻的空格算作一个
return Mid(ret1,ret2-ret1);
}
if(ret2==-1)
return Right(GetLength()-ret1);
tokennum=totalnum;
(callednum++)%=tokennum;
}
这是粗糙的算法,要想真正实现指定功能,可能细节需要修改!