当前位置:七道奇文章资讯编程技术VC/C++编程
日期:2011-03-22 13:54:00  来源:本站整理

新加坡程序员考题一则及解析[VC/C++编程]

赞助商链接



  本文“新加坡程序员考题一则及解析[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

考题原文:

Problem statement

You 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 functions

1; 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 string

A 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.

参考译文:

问题描写:

你必须成立一个功效强盛的串处理类,String,这个类必须从 C++ 中尺度的 string 类派生,必须在该派生类中增添两个成员函数:

1、情势辨认函数,返回某个串中指定串情势的呈现次数.该函数必须利用 / (除法)运算符重载.

2、用函数 get_token 从串中汲取某个暗号并返回该暗号.

暗号指字符串的一个子串,它位于字符串的空格之间,大概位于空格和串尾之间.如:字符串“ aaa bbb cc ”中的暗号有“aaa”、“bbb”和“cc”.当第一次调用该函数时,它必须返回“aaa”,下次再调用时返回“bbb”,第三次调用时返回“cc”,依此类推.当第四次调用该函数时,它必须返回一个空串,最后当第五次调用它时,返回后果又从 “aaa” 开始.你可以随便扩大这个办理筹划,让暗号可以用某组字符以外的任何字符分割,这组字符可以作为一个串参数传送.

算法解析(写成代码):

int CMyString::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 CMyString::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;
}

声明:这只是粗糙的算法,要想真正实现指定功效,大概细节需求改正!但愿对MFC的初学者有所帮忙!


  以上是“新加坡程序员考题一则及解析[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 《新加坡程序员考题一则及解析》的谈论
  • 新加坡程序员考题一则及解析
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .