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.

解决方案 »

  1.   

    好像是比较简单的编程题1. 实现一个模式识别函数,必须使用运算符重载 operator/ (我想是 int String::operator/ (const String& sub) or  int String::operator/ (const char * psz) )返回子串在 String 的出现次数2. 实现 get_token 函数,返回字符串头、尾和空格之间的子串,例如对于字符串 " aaa    bbb cc" 必须依次返回 "aaa" "bbb" "cc" NULL "aaa" ...
      

  2.   

    简单!我随便实现这两个函数的算法吧!
    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;

    这是粗糙的算法,要想真正实现指定功能,可能细节需要修改!