当前位置: 首页 > 图文教程 > 开发语言 > VC++ > 新加坡程序员考题一则及分析

VC++
在类VC的界面实现中加入目录树
软件换肤技术在 BCB 中的实现
利用非模窗口生成MDI介面
报表输出轻松搞定
Windows 中不规则窗体的编程实现
解说Win32的窗口子类化
使用测试优先方法开发用户界面
一个简单的登录对话框的实现
一个简单的日记本程序
从资源中加载皮肤
一个在RichEdit中添加表情图象的类
ActiveSkin 4.3 软件换肤在VC中的实现
一种另类“关于(About)”对话框的动态显示方法
对话框打印预览及打印
关于如何换肤、子类化的解决方案
制作 MSN、QQ 的消息提示窗口
如何对 BCGControlBarPro 进行换肤
定制个性化的对话框窗口类
改变窗口中的光标形状
更新MFC中的视图,跟踪.NET Framework中的事件

VC++ 中的 新加坡程序员考题一则及分析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 38 ::
收藏到网摘: n/a

新加坡程序员考题一则及分析
作者: 孙玉玮

考题原文:

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的初学者有所帮助!