字符串类报告里面包含源代码.docx
- 文档编号:8375144
- 上传时间:2023-01-30
- 格式:DOCX
- 页数:14
- 大小:68.09KB
字符串类报告里面包含源代码.docx
《字符串类报告里面包含源代码.docx》由会员分享,可在线阅读,更多相关《字符串类报告里面包含源代码.docx(14页珍藏版)》请在冰豆网上搜索。
字符串类报告里面包含源代码
类中运算符的重载应用——字符串类
一、实验内容
定义字符串类,并对构造函数进行重载,对主要运算符进行重载,定义对字符串进行操作的其它函数,通过以上操作实现对字符串的基本操作。
二、功能模块简介
1.定义字符串类
classTstring
{
public:
//构造函数
TString();
//带有TString类常量的构造函数
TString(constTString&src);
//带有字符指针参变量的构造函数
TString(char*src);
//析构函数
~TString();
private:
char*m_pBuf;
}
2.字符串赋值操作:
赋值运算符"="重载
public:
TString&operator=(constTString&src);//对赋值运算符"="进行重载
TString&operator=(char*src);
3.字符串连接操作:
连接运算符"+"重载
public:
TString&operator+(constTString&src);//对连接运算符"+"进行重载
TString&operator+(char*src);
4.字符串连接、赋值操作:
赋值运算符"+="重载
public:
TString&operator+=(constTString&src);//对连接、赋值运算符"+="进行重载
TString&operator+=(char*src);
5.求字符串中的单个字符元素操作:
下标运算符"[]"重载
public:
char&operator[](intnIndex);//对下标运算符"[]"进行重载
6.求字符串的子串
1)求指定的字符串的左边几个字符
public:
TStringLeft(intnCount);//求左边几个字符
2)求指定的字符串的右边几个字符
public:
TStringRight(intnCount);//求右边几个字符
3)求指定的字符串的某个位置开始的几个字符
public:
TStringMid(intnPos,intnCount);//求某个位置开始的几个字符
7.求字符串的长度
public:
intLength();//求字符串的长度
8.判断字符串是否为空
public:
boolIsEmpty();//判断字符串是否为空
9.清空字符串
public:
voidEmpty();//清空字符串
10.输出字符串
public:
voidPrint();//输出字符串
11.字符串类内部函数(私有函数)
private:
voidSetValue(char*src);
三、程序代码
#include
#include
classTString
{
public:
//构造函数
TString();
//带有TString类常量的构造函数
TString(constTString&src);
//带有字符指针参变量的构造函数
TString(char*src);
//析构函数
~TString();
public:
//求字符串的长度
intLength();
//判断字符串是否为空
boolIsEmpty();
//清空字符串
voidEmpty();
//输出字符串
voidPrint();
private:
voidSetValue(char*src);
public:
//对下标运算符"[]"进行重载
char&operator[](intnIndex);
//对赋值运算符"="进行重载
TString&operator=(constTString&src);
TString&operator=(char*src);
//对连接运算符"+"进行重载
TString&operator+(constTString&src);
TString&operator+(char*src);
//对连接、赋值运算符"+="进行重载
TString&operator+=(constTString&src);
TString&operator+=(char*src);
public:
//求左边几个字符
TStringLeft(intnCount);
//求右边几个字符
TStringRight(intnCount);
//求某个位置开始的几个字符
TStringMid(intnPos,intnCount);
private:
char*m_pBuf;
};
//-------------------------------
TString:
:
TString()
{
m_pBuf=NULL;
}
TString:
:
TString(constTString&src)
{
this->SetValue(src.m_pBuf);
}
TString:
:
TString(char*src)
{
this->SetValue(src);
}
TString:
:
~TString()
{
if(m_pBuf)
delete[]m_pBuf;
}
//---------------------------------------------------
intTString:
:
Length()
{
if(m_pBuf==NULL)
return0;
returnstrlen(m_pBuf)+1;
}
boolTString:
:
IsEmpty()
{
if(m_pBuf==NULL)
returntrue;
returnfalse;
}
voidTString:
:
Empty()
{
if(m_pBuf)
{
delete[]m_pBuf;
m_pBuf=NULL;
}
}
voidTString:
:
Print()
{
if(m_pBuf==NULL)
cout<<""< else cout< } //----------------------------------- voidTString: : SetValue(char*src) { if(src==NULL) m_pBuf=NULL; else { intnLen=strlen(src)+1; m_pBuf=newchar[nLen]; strcpy(m_pBuf,src); } } //--------------------------------------------- char&TString: : operator[](intnIndex) { returnm_pBuf[nIndex]; } TString&TString: : operator=(constTString&src) { //传来的对象和this地址是同一个地址 if(src.m_pBuf==m_pBuf) return*this; //以前有分配空间 if(m_pBuf) delete[]m_pBuf;//释放以前的分配空间 SetValue(src.m_pBuf); return*this; } TString&TString: : operator=(char*src) { //传来的字符串和this中的m_pBuf字符串是一个同一个字符串 if(src==this->m_pBuf) return*this; //以前有分配空间 if(m_pBuf) delete[]m_pBuf;//释放以前的分配空间 SetValue(src); return*this; } TString&TString: : operator+(constTString&src) { TString*temp; temp=newTString(); temp->m_pBuf=NULL; //传来的对象为空 if(src.m_pBuf==NULL) return*this; if(m_pBuf==src.m_pBuf) { intnLen=2*strlen(src.m_pBuf)+1; temp->m_pBuf=newchar[nLen]; strcpy(temp->m_pBuf,src.m_pBuf); strcat(temp->m_pBuf,src.m_pBuf); } elseif(m_pBuf) { intnLen=strlen(src.m_pBuf)+strlen(m_pBuf)+1; temp->m_pBuf=newchar[nLen]; strcpy(temp->m_pBuf,m_pBuf); strcat(temp->m_pBuf,src.m_pBuf); } else { intnLen=strlen(src.m_pBuf)+1; temp->m_pBuf=newchar[nLen]; strcpy(temp->m_pBuf,src.m_pBuf); } return*temp; } TString&TString: : operator+(char*src) { TString*temp; temp=newTString(); temp->m_pBuf=NULL; if(src==NULL) return*this; elseif(m_pBuf) { intnLen=strlen(m_pBuf)+strlen(src)+1; temp->m_pBuf=newchar[nLen]; strcpy(temp->m_pBuf,m_pBuf); strcat(temp->m_pBuf,src); } else { intnLen=strlen(src)+1; temp->m_pBuf=newchar[nLen]; strcpy(temp->m_pBuf,src); } return*temp; } TString&TString: : operator+=(constTString&src) { if(src.m_pBuf==NULL) return*this; if(m_pBuf==src.m_pBuf) { intnLen=2*strlen(src.m_pBuf)+1; char*temp=src.m_pBuf; m_pBuf=newchar[nLen]; strcpy(m_pBuf,temp); strcat(m_pBuf,temp); } elseif(m_pBuf) { intnLen=strlen(src.m_pBuf)+strlen(m_pBuf)+1; char*temp=m_pBuf; m_pBuf=newchar[nLen]; strcpy(m_pBuf,temp); strcat(m_pBuf,src.m_pBuf); } else { intnLen=strlen(src.m_pBuf)+1; m_pBuf=newchar[nLen]; strcpy(m_pBuf,src.m_pBuf); } return*this; } TString&TString: : operator+=(char*src) { if(src==NULL) return*this; if(m_pBuf) { intnLen=strlen(m_pBuf)+strlen(src)+1; char*temp=m_pBuf; m_pBuf=newchar[nLen]; strcpy(m_pBuf,temp); strcat(m_pBuf,src); } else { intnLen=strlen(src)+1; m_pBuf=newchar[nLen]; strcpy(m_pBuf,src); } return*this; } //--------------------------------------------------- //求左边几个字符 TStringTString: : Left(intnCount) { TStringtemp; inti; if(m_pBuf==NULL) temp.m_pBuf=NULL; elseif(nCount>Length()) temp.m_pBuf=NULL; else { temp.m_pBuf=newchar[nCount+1]; for(i=0;i temp.m_pBuf[i]=m_pBuf[i]; if(temp.m_pBuf[i-1]! ='\0') temp.m_pBuf[nCount]='\0'; } returntemp; } //求右边几个字符 TStringTString: : Right(intnCount) { TStringtemp; inti,j; if(m_pBuf==NULL) temp.m_pBuf=NULL; elseif(nCount<1||nCount>Length()) temp.m_pBuf=NULL; else { intnLength=Length()-1; temp.m_pBuf=newchar[nCount+1]; for(i=nLength-1,j=nCount-1;i>=(nLength-nCount)&&j>=0;i--,j--) temp.m_pBuf[j]=m_pBuf[i]; if(temp.m_pBuf[j-1]! ='\0') temp.m_pBuf[nCount]='\0'; } returntemp; } //某个位置开始的几个字符 TStringTString: : Mid(intnPos,intnCount) { TStringtemp; inti,j; if(m_pBuf==NULL) temp.m_pBuf=NULL; elseif(nPos<1||nPos>=Length()||nCount<1||nCount>Length()) temp.m_pBuf=NULL; elseif((nPos+nCount)>Length()) temp.m_pBuf=NULL; else { intnLength=Length()-1; temp.m_pBuf=newchar[nCount+1]; for(i=nPos-1,j=0;i<(nPos+nCount)&&j temp.m_pBuf[j]=m_pBuf[i]; temp.m_pBuf[j]='\0'; } returntemp; } //------------------------------------- voidmain() { TStrings1="Hello,"; TStrings2=s1; cout<<"s1="; s1.Print(); cout<<"\n将s1复制到s2,\ns2="; s2.Print(); cout<<"\n求s2的第3个字符: "< TStrings3=s1+"world"; cout<<"\n执行s3=s1+\"world! \"之后,\ns3="; s3.Print(); s3=s1+s2; cout<<"\n执行s3=s1+s2之后,\ns3="; s3.Print(); TStrings4; s4+=s1; cout<<"\n定义s4: TStrings4;执行s4+=s1之后,\ns4="; s4.Print(); s4+="world! "; cout<<"\n执行s4+=\"world! \"之后,\ns4="; s4.Print(); cout<<"求s4的从第3个字符开始的4个字符: "< s4.Mid(3,4).Print(); cout<<"求s4的左边的6个字符"< s4.Left(6).Print(); cout<<"求s4的最右边的5个字符"< s4.Right(5).Print(); } 四、运行结果
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 字符串 报告 里面 包含 源代码
