字符串类报告(里面包含源代码)_第1页
字符串类报告(里面包含源代码)_第2页
字符串类报告(里面包含源代码)_第3页
字符串类报告(里面包含源代码)_第4页
字符串类报告(里面包含源代码)_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

类中运算符的重载应用字符串类一、实验内容定义字符串类,并对构造函数进行重载,对主要运算符进行重载,定义对字符串进行操作的其它函数,通过以上操作实现对字符串的基本操作。二、功能模块简介1定义字符串类class Tstringpublic:/构造函数TString();/带有 TString 类常量的构造函数TString(const TString/带有字符指针参变量的构造函数TString(char* src);/析构函数TString();private:char* m_pBuf;2字符串赋值操作:赋值运算符“=“重载public:TString/对赋值运算符“=“进行重载TString3字符串连接操作:连接运算符“+“重载public:TString/对连接运算符“+“进行重载TString4字符串连接、赋值操作:赋值运算符“+=“重载public:TString/对连接、赋值运算符“+=“进行重载TString5求字符串中的单个字符元素操作:下标运算符“重载public:char/对下标运算符“进行重载6求字符串的子串1)求指定的字符串的左边几个字符public:TString Left(int nCount);/求左边几个字符2)求指定的字符串的右边几个字符public:TString Right(int nCount);/求右边几个字符3)求指定的字符串的某个位置开始的几个字符public:TString Mid(int nPos,int nCount);/求某个位置开始的几个字符7求字符串的长度public:int Length();/求字符串的长度8判断字符串是否为空public:bool IsEmpty();/判断字符串是否为空9清空字符串public:void Empty();/清空字符串10输出字符串public:void Print();/输出字符串11字符串类内部函数(私有函数)private:void SetValue(char* src);三、程序代码#include #include class TStringpublic:/构造函数TString();/带有 TString 类常量的构造函数TString(const TString/带有字符指针参变量的构造函数TString(char* src);/析构函数TString();public:/求字符串的长度int Length();/判断字符串是否为空bool IsEmpty();/清空字符串void Empty();/输出字符串void Print();private:void SetValue(char* src);public:/对下标运算符“进行重载char/对赋值运算符“=“进行重载TStringTString/对连接运算符“+“进行重载TStringTString/对连接、赋值运算符“+=“进行重载TStringTStringpublic:/求左边几个字符TString Left(int nCount);/求右边几个字符TString Right(int nCount);/求某个位置开始的几个字符TString Mid(int nPos,int nCount);private:char* m_pBuf;/-TString:TString()m_pBuf = NULL;TString:TString(const TStringTString:TString(char* src)this-SetValue(src);TString:TString()if(m_pBuf)delete m_pBuf;/-int TString:Length()if(m_pBuf=NULL)return 0;return strlen(m_pBuf)+1;bool TString:IsEmpty()if(m_pBuf=NULL)return true;return false;void TString:Empty()if(m_pBuf)delete m_pBuf;m_pBuf = NULL;void TString:Print()if(m_pBuf=NULL)coutm_pBuf)return *this;/以前有分配空间if(m_pBuf)delete m_pBuf;/释放以前的分配空间SetValue(src);return *this;TStringtemp=new TString();temp-m_pBuf=NULL;/传来的对象为空if(src.m_pBuf = NULL)return *this;if(m_pBuf=src.m_pBuf)int nLen=2*strlen(src.m_pBuf)+1;temp-m_pBuf=new charnLen;strcpy(temp-m_pBuf,src.m_pBuf);strcat(temp-m_pBuf,src.m_pBuf);else if(m_pBuf)int nLen = strlen(src.m_pBuf)+strlen(m_pBuf)+1;temp-m_pBuf = new charnLen;strcpy(temp-m_pBuf,m_pBuf);strcat(temp-m_pBuf,src.m_pBuf);elseint nLen = strlen(src.m_pBuf)+1;temp-m_pBuf = new charnLen;strcpy(temp-m_pBuf,src.m_pBuf);return *temp;TStringtemp=new TString();temp-m_pBuf=NULL;if(src=NULL)return *this;else if(m_pBuf)int nLen = strlen(m_pBuf)+strlen(src)+1;temp-m_pBuf = new charnLen;strcpy(temp-m_pBuf,m_pBuf);strcat(temp-m_pBuf,src);elseint nLen = strlen(src)+1;temp-m_pBuf=new charnLen;strcpy(temp-m_pBuf,src);return *temp;TStringif(m_pBuf=src.m_pBuf)int nLen=2*strlen(src.m_pBuf)+1;char *temp=src.m_pBuf;m_pBuf=new charnLen;strcpy(m_pBuf,temp);strcat(m_pBuf,temp);else if(m_pBuf)int nLen=strlen(src.m_pBuf)+strlen(m_pBuf)+1;char* temp=m_pBuf;m_pBuf=new charnLen;strcpy(m_pBuf,temp);strcat(m_pBuf,src.m_pBuf);elseint nLen=strlen(src.m_pBuf)+1;m_pBuf=new charnLen;strcpy(m_pBuf,src.m_pBuf);return *this;TStringif(m_pBuf)int nLen=strlen(m_pBuf)+strlen(src)+1;char *temp=m_pBuf;m_pBuf=new charnLen;strcpy(m_pBuf,temp);strcat(m_pBuf,src);elseint nLen=strlen(src)+1;m_pBuf=new charnLen;strcpy(m_pBuf,src);return *this;/-/求左边几个字符TString TString:Left(int nCount)TString temp;int i;if(m_pBuf=NULL)temp.m_pBuf=NULL;else if(nCountLength()temp.m_pBuf=NULL;elsetemp.m_pBuf=new charnCount+1;for(i=0;iLength()temp.m_pBuf=NULL;elseint nLength=Length()-1;temp.m_pBuf=new charnCount+1;for(i=nLength-1,j=nCount-1;i=(nLength-nCount)i-,j-)temp.m_pBufj=m_pBufi;if(temp.m_pBufj-1!=0)temp.m_pBufnCount=0;return temp;/某个位置开始的几个字符TString TString:Mid(int nPos,int nCount)TString temp;int i,j;if(m_pBuf=NULL)temp.m_pBuf=NULL;else if(nPos=Length()|nCountLength()temp.m_pBuf=NULL;else if(nPos+nCount)Length()temp.m_pBuf=NULL;elseint nLength=Length()-1;temp.m_pBuf=new charnCount+1;for(i=nPos-1,j=0;i(nPos+nCount)i+,j+)temp.m_pBufj=m_pBufi;temp.m_pBufj=0;return temp;/-void main()TString s1=“Hello,“;TString s2=s1;cout“s1=“;s1.Print();cout“n 将 s1 复制到 s2,ns2=“;s2.Print();cout“n 求 s2 的第 3 个字符: “s22endl;TString s3=s1+“world“;cout“n 执行 s3=s1+“world!“之后,ns3=“;s3.Print();s3=s1+s2;cout“n 执行 s3=s1+s2 之后,ns3=“;s3.Print();TString s4;s4+=s1;cout“n 定义 s4:TString s4;执行 s4+=s1 之后,ns4

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论