【PHP开源代码栏目提醒】:网学会员在PHP开源代码频道为大家收集整理了“【精品】字符串类报告(里面包含源代码) - 其它资料“提供大家参考,希望对大家有所帮助!
类中运算符的重载应用——字符串类一、实验内容 定义字符串类,并对构造函数进行重载,对主要运算符进行重载,定义对字符串进行操作的其它函数,通过以上操作实现对字符串的基本操作。
二、功能模块简介1.定义字符串类class Tstringpublic: //构造函数 TString //带有 TString 类常量的构造函数 TStringconst TString src //带有字符指针参变量的构造函数 TStringchar src //析构函数 TStringprivate: char m_pBuf2.字符串赋值操作:赋值运算符重载public: TString operatorconst TString src//对赋值运算符进行重载 TString operatorchar src3.字符串连接操作:连接运算符重载public: TString operatorconst TString src//对连接运算符进行重载 TString operatorchar src4.字符串连接、赋值操作:赋值运算符重载public: TString operatorconst TString src//对连接、赋值运算符进行重载 TString operatorchar src5.求字符串中的单个字符元素操作:下标运算符重载public: char operatorint nIndex//对下标运算符进行重载6.求字符串的子串1求指定的字符串的左边几个字符public: TString Leftint nCount//求左边几个字符2求指定的字符串的右边几个字符public: TString Rightint nCount//求右边几个字符3求指定的字符串的某个位置开始的几个字符public: TString Midint nPosint nCount//求某个位置开始的几个字符7.求字符串的长度public: int Length//求字符串的长度8.判断字符串是否为空public: bool IsEmpty//判断字符串是否为空9.清空字符串public: void Empty//清空字符串10.输出字符串public: void Print//输出字符串11.字符串类内部函数私有函数private: void SetValuechar src三、程序
代码includeincludeclass TStringpublic: //构造函数 TString //带有 TString 类常量的构造函数 TStringconst TString src //带有字符指针参变量的构造函数 TStringchar src //析构函数 TStringpublic: //求字符串的长度 int Length //判断字符串是否为空 bool IsEmpty //清空字符串 void Empty //输出字符串 void Printprivate: void SetValuechar srcpublic: //对下标运算符进行重载 char operatorint nIndex //对赋值运算符进行重载 TString operatorconst TString src TString operatorchar src //对连接运算符进行重载 TString operatorconst TString src TString operatorchar src //对连接、赋值运算符进行重载 TString operatorconst TString src TString operatorchar srcpublic: //求左边几个字符 TString Leftint nCount //求右边几个字符 TString Rightint nCount //求某个位置开始的几个字符 TString Midint nPosint nCountprivate: char m_pBuf//-------------------------------TString::TString m_pBuf NULLTString::TStringconst TString src this-SetValuesrc.m_pBufTString::TStringchar src this-SetValuesrcTString::TString ifm_pBuf delete m_pBuf//---------------------------------------------------int TString::Length ifm_pBufNULL return 0 return strlenm_pBuf1bool TString::IsEmpty ifm_pBufNULL return true return falsevoid TString::Empty ifm_pBuf delete m_pBuf m_pBuf NULL void TString::Print ifm_pBufNULL coutm_pBufsrc.m_pBuf else int nLen strlensrc.m_pBuf1 temp-m_pBuf new charnLen strcpytemp-m_pBufsrc.m_pBuf return tempTString TString::operatorchar src TString temp tempnew TString temp-m_pBufNULL ifsrcNULL return this else ifm_pBuf int nLen strlenm_pBufstrlensrc1 temp-m_pBuf new charnLen strcpytemp-m_pBufm_pBuf strcattemp-m_pBufsrc else int nLen strlensrc1 temp-m_pBufnew charnLen strcpytemp-m_pBufsrc return tempTString TString::operatorconst TString src ifsrc.m_pBufNULL return this ifm_pBufsrc.m_pBuf int nLen2strlensrc.m_pBuf1 char tempsrc.m_pBuf m_pBufnew charnLen strcpym_pBuftemp strcatm_pBuftemp else ifm_pBuf int nLenstrlensrc.m_pBufstrlenm_pBuf1 char tempm_pBuf m_pBufnew charnLen strcpym_pBuftemp strcatm_pBufsrc.m_pBuf else int nLenstrlensrc.m_pBuf1 m_pBufnew charnLen strcpym_pBufsrc.m_pBuf return thisTString TString::operatorchar src ifsrcNULL return this ifm_pBuf int nLenstrlenm_pBufstrlensrc1 char tempm_pBuf m_pBufnew charnLen strcpym_pBuftemp strcatm_pBufsrc else int nLenstrlensrc1 m_pBufnew charnLen strcpym_pBufsrc return this//---------------------------------------------------//求左边几个字符TString TString::Leftint nCount TString temp int i ifm_pBufNULL temp.m_pBufNULL else ifnCountLength temp.m_pBufNULL else temp.m_pBufnew charnCount1 fori0inLength-nCountj0i--j-- temp.m_pBufjm_pBufi iftemp.m_pBufj-10 temp.m_pBufnCount0 return temp//某个位置开始的几个字符TString TString::Midint nPosint nCount TString temp int ij ifm_pBufNULL temp.m_pBufNULL else ifnPosLengthnCountLength temp.m_pBufNULL else ifnPosnCountLength temp.m_pBufNULL else int nLengthLength-1 temp.m_pBufnew charnCount1 forinPos-1j0i