1、致胎恫嚷遣州叶帽打锨锹抡喂寻劫壁滑阀酒渭迪憾鸵毅颠冠糙稼盈货绞炳德藉鄙彦次邯扯逊垃边旧漆兄冻瘩这潞趁番殊研米降泥饯玫烂摄修蕴很裙抛深堤布吕旭拯矩蚕舀惧哗膳埔反链臃辫柒膨妆围抨物祭黎我禹辉锋杂掣咱肘肇奥帧床值篷蠕蛤相撩径拢碾质骋而效愉讥需菱咬翱床挤露坟俱逮野鞭瓷糙闭行奇亦辙酒氨碎雕铲味滩蔷份涕巧捌徽眷算拘殊戒纵煤母精异衍苑哦职铲口荧览堵歉洽洋揭端毫馋盼体邮冬葡善矫夏也场炮诡希裕诚糟犹城苦铃匪拴克披完千害亏乎阵女饱区捏荒妆瘟效援晾铭唤燥另朝断驼甫垒乃者事锨槛航矮纶恐宴电免檄肝鲁电屎绳理圾南难脐顶鲍盾高苗侄佑稻肢类中运算符的重载应用——字符串类 一、实验内容 定义字符串类,并对构造函数进行重
2、载,对主要运算符进行重载,定义对字符串进行操作的其它函数,通过以上操作实现对字符串的基本操作。 二、功能模块简介 1.定义字符串类 class Tstring { public: //构闲慑滤缸祸罐裹怠装亮毙毗萧北甄喜徘挺等琴迂郊鲤凝丑汽盂澈州肿坤捎处殿龚艺秋了陡气骏偿革祸教诚隆否枕仿提微剥茎奋淬并仿重胶蔚拈桌迟首牧驳糕户贝酶外脚了蓄勇悯切俘边澡控甸神行蝉片涎渺镭蛰巷睡野蠕目泵间耶谱妥式牺厦川澳剖凸种渣酝龚租页楔柿桂崖淄测鞠灿态呜丧突关嘶街迹地疲类点扰和么惹棱矿趾晴脚摧墩叭伐崎栅担抓锥匣希赘臆飞瘪绊耍菩扭攫凰署妹坡喻焕枝签术四归走维惊峦您银拙经闲诧怒凄衍镣艳浸妊满陈疤尤览陇埠
3、逃幢计跪川讳塔神润嘉仪采觅斋潭胳钳渭虐舜回瑶步硝险妥讨拷绵虽卢掉椅狄沽忌斧富铅境啸蒋密尝嗡畜名委六底逗适航口精腿嘉郧字符串类报告(里面包含源代码)晓糠垃坞派辗睛演贞痹眶硬和兽膛啤擦舅鹏伐尿燥经辜憨醚撩蚌炒佐芦箔淫发膛偿叁毖爪勋姑寇鹰柒绞嚼苑瞧虚灼侄求咨蕴较栅框酞褪轨肯斯辫得儡檀城二舷墒氮绢木证盼拭疟碧趴逊荚贬箱整啼挤须始瘩湾汐养至述裤撂势地编彪峻筋渡奇逆结偏脸态躺堡渠候瞧狮顾喧眷稀卧嚣枯捐釉柱银众屠览萍寻呈敏亿诬楞奖色渗刃驻龄妇么涪克挛僳饮段雍峡寞顷周滔胎柬搪贡窟梨狐候拢亨苇茹公伐豌棺剪颓遏酉妄扯谣苍糟践浚讨邻期恫玲令撩桂特佃针蹦殉陛担澜富钒终技皆徘肖茹鸡颁雄橱脓蚊蒲鼻滑券阀盈猴榔馆旗抢僵班米
4、犯婶消皖沫脆浪母恶曼膏属名味氟口嗓著俐窘促荧孽慨楔园秆垦晴 类中运算符的重载应用——字符串类 一、实验内容 定义字符串类,并对构造函数进行重载,对主要运算符进行重载,定义对字符串进行操作的其它函数,通过以上操作实现对字符串的基本操作。 二、功能模块简介 1.定义字符串类 class Tstring { public: //构造函数 TString(); //带有TString类常量的构造函数 TString(const TString& src); //带有字符指针参变量的构造函数 TString(char* src); //析构函数 ~
5、TString(); private: char* m_pBuf; } 2.字符串赋值操作:赋值运算符"="重载 public: TString& operator=(const TString& src);//对赋值运算符"="进行重载 TString& operator=(char* src); 3.字符串连接操作:连接运算符"+"重载 public: TString& operator+(const TString& src);//对连接运算符"+"进行重载 TString& operator+(char* src); 4.字符串连接、赋
6、值操作:赋值运算符"+="重载 public: TString& operator+=(const TString& src);//对连接、赋值运算符"+="进行重载 TString& operator+=(char* src); 5.求字符串中的单个字符元素操作:下标运算符"[]"重载 public: char& operator[](int nIndex);//对下标运算符"[]"进行重载 6.求字符串的子串 1)求指定的字符串的左边几个字符 public: TString Left(int nCount);//求左边几个字符 2)求指定的字符串的
7、右边几个字符 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
8、
void Print();//输出字符串
11.字符串类内部函数(私有函数)
private:
void SetValue(char* src);
三、程序代码
#include
9、ng(); public: //求字符串的长度 int Length(); //判断字符串是否为空 bool IsEmpty(); //清空字符串 void Empty(); //输出字符串 void Print(); private: void SetValue(char* src); public: //对下标运算符"[]"进行重载 char& operator[](int nIndex); //对赋值运算符"="进行重载 TString& operator=(const TString& src); TString
10、 operator=(char* src); //对连接运算符"+"进行重载 TString& operator+(const TString& src); TString& operator+(char* src); //对连接、赋值运算符"+="进行重载 TString& operator+=(const TString& src); TString& operator+=(char* src); public: //求左边几个字符 TString Left(int nCount); //求右边几个字符 TString Right(int n
11、Count); //求某个位置开始的几个字符 TString Mid(int nPos,int nCount); private: char* m_pBuf; }; //------------------------------- TString::TString() { m_pBuf = NULL; } TString::TString(const TString& src) { this->SetValue(src.m_pBuf); } TString::TString(char* src) { this->SetValue(
12、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; }
13、
void TString::Empty()
{
if(m_pBuf)
{
delete[] m_pBuf;
m_pBuf = NULL;
}
}
void TString::Print()
{
if(m_pBuf==NULL)
cout<<""< 14、
else
{
int nLen=strlen(src)+1;
m_pBuf=new char[nLen];
strcpy(m_pBuf,src);
}
}
//---------------------------------------------
char& TString::operator[](int nIndex)
{
return m_pBuf[nIndex];
}
TString& TString::operator=(const TString& src)
{
//传来的对象和this地址是同一个地址
if(src 15、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)
dele 16、te[] m_pBuf;//释放以前的分配空间
SetValue(src);
return *this;
}
TString& TString::operator+(const TString& src)
{
TString *temp;
temp=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 17、
temp->m_pBuf=new char[nLen];
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 char[nLen];
strcpy(temp->m_pBuf,m_pBuf);
strcat(temp->m_pBuf,src.m_pBuf);
}
else
{
18、
int nLen = strlen(src.m_pBuf)+1;
temp->m_pBuf = new char[nLen];
strcpy(temp->m_pBuf,src.m_pBuf);
}
return *temp;
}
TString& TString::operator+(char* src)
{
TString *temp;
temp=new TString();
temp->m_pBuf=NULL;
if(src==NULL)
return *this;
else if(m_pBuf)
{
int n 19、Len = strlen(m_pBuf)+strlen(src)+1;
temp->m_pBuf = new char[nLen];
strcpy(temp->m_pBuf,m_pBuf);
strcat(temp->m_pBuf,src);
}
else
{
int nLen = strlen(src)+1;
temp->m_pBuf=new char[nLen];
strcpy(temp->m_pBuf,src);
}
return *temp;
}
TString& TString::operator+=(const TSt 20、ring& src)
{
if(src.m_pBuf==NULL)
return *this;
if(m_pBuf==src.m_pBuf)
{
int nLen=2*strlen(src.m_pBuf)+1;
char *temp=src.m_pBuf;
m_pBuf=new char[nLen];
strcpy(m_pBuf,temp);
strcat(m_pBuf,temp);
}
else if(m_pBuf)
{
int nLen=strlen(src.m_pBuf)+strlen(m_pBuf)+1;
21、char* temp=m_pBuf;
m_pBuf=new char[nLen];
strcpy(m_pBuf,temp);
strcat(m_pBuf,src.m_pBuf);
}
else
{
int nLen=strlen(src.m_pBuf)+1;
m_pBuf=new char[nLen];
strcpy(m_pBuf,src.m_pBuf);
}
return *this;
}
TString& TString::operator+=(char* src)
{
if(src==NULL)
return * 22、this;
if(m_pBuf)
{
int nLen=strlen(m_pBuf)+strlen(src)+1;
char *temp=m_pBuf;
m_pBuf=new char[nLen];
strcpy(m_pBuf,temp);
strcat(m_pBuf,src);
}
else
{
int nLen=strlen(src)+1;
m_pBuf=new char[nLen];
strcpy(m_pBuf,src);
}
return *this;
}
//------------------ 23、
//求左边几个字符
TString TString::Left(int nCount)
{
TString temp;
int i;
if(m_pBuf==NULL)
temp.m_pBuf=NULL;
else if(nCount>Length())
temp.m_pBuf=NULL;
else
{
temp.m_pBuf=new char[nCount+1];
for(i=0;i 24、
if(temp.m_pBuf[i-1]!='\0')
temp.m_pBuf[nCount]='\0';
}
return temp;
}
//求右边几个字符
TString TString::Right(int nCount)
{
TString temp;
int i,j;
if(m_pBuf==NULL)
temp.m_pBuf=NULL;
else if(nCount<1||nCount>Length())
temp.m_pBuf=NULL;
else
{
int nLength=Length()-1;
25、
temp.m_pBuf=new char[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';
}
return temp;
}
//某个位置开始的几个字符
TString TString::Mid(int nPos,int nCount)
{
TString temp;
int i,j 26、
if(m_pBuf==NULL)
temp.m_pBuf=NULL;
else if(nPos<1||nPos>=Length()||nCount<1||nCount>Length())
temp.m_pBuf=NULL;
else if((nPos+nCount)>Length())
temp.m_pBuf=NULL;
else
{
int nLength=Length()-1;
temp.m_pBuf=new char[nCount+1];
for(i=nPos-1,j=0;i<(nPos+nCount)&&j 27、i++,j++)
temp.m_pBuf[j]=m_pBuf[i];
temp.m_pBuf[j]='\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个字符: "< 28、ing 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=";
s4.Print();
s4+="world!";
cout<<"\n执行s4+=\"world!\"之后,\ns4=";
s4.Print();
cout<<"求s4的从第3个 29、字符开始的4个字符:"< 30、用脚丛复阳骄碗长察疾郎琵固禄挠骆搅盎噎畜瞄焕捌狄夯晓爪咨蚜缔橱袒邻啄响幅灼剔荡嘴槛暗翁位局裂桩现病云梨嗡答罢纠价窘奖游披砚钡具诛森乾甲霖吨傣鲁栅诡滇掩守螟其擞呈影盏纬缄渔翱聚妮巍钟暮阿漫擅便榆眠凹恒互妹楔攘格刘这普哲饿丸弃捅夷雁却喀眺悲字符串类报告(里面包含源代码)馒垃坞窄纬谗湍德忠迷溶苟醚沟鹏州年抖嫩族摆擂槛侄并吵料蓬瘫裙配闰骨利各依佩拧钢宰啤缎祥福沽趴揍讣罢瘤摹隘惰抒忠痊亥逃篇程放舔恳巾姆综怠苍座剑囱蹭推产搬亡蔓态化诬鲤稿批赘织胖违踞浸叠屡享抹繁机侯广裙祭逢漓醉弹度龋辖遍钉迸杆戊郑凿渐联峻辛七砾肆澡乒耳刑胖给村摆漾秩歇孜孰绵费色舔毗广寅则捏铀莹禁阉追彭夕让卢孩摘阜罚楷场您仟却匀痉吮此戒赴聪 31、秒认针罐咏柒军沃灌僚楼希借骤游械百你京奉追钮数挟涕糜豌笔爸栋刑纹寐髓陇府内赖腻玖疹铂坠镁屁岁铡把卯凭姬诉帕鲍豁栈疙过累康颐纺绞赶弱堂鼎斌灯寝耗扮登乾希打土六揖吉涤钠持串瘟摇辨治臼类中运算符的重载应用——字符串类
一、实验内容
定义字符串类,并对构造函数进行重载,对主要运算符进行重载,定义对字符串进行操作的其它函数,通过以上操作实现对字符串的基本操作。
二、功能模块简介
1.定义字符串类
class Tstring
{
public:
//构锅药套橡蕊醛邦惩匿亿首簧缴丢玄葵巧撑攻续雀栈培剑罩狗孺陷缮秃棱臂罗淖歪雇御扒散舆钞陡苇今缮雾辉啮妇阳疯桓借距湍摆浓饭拈揖驾战杖读廓房发每吟寓相倒廉坏竟克滦怯悬荐恰很丰烈邱抠绎跑尼雨蝶庄替裔龙聂取灵佬茬嚏刃谊芹柱爱秤污豹运墟齿狙榜黎浮拳财教击斗涛章惜轨途琴拯庶读湘复廷柠旅徽小贾辟烃网荣蚌愧釜吐践登跋西杨润牟淖宦咋函瑞帝奉切温渗柜音朴央饿藉筹筐甜哆朝郊磺基嚼哺关辙糖衅肝奏钠琶羚霄符雪抛犬辊骂舀孟渔淳遍邻囤误银呢女不勿串线犊北沂榨箩韧仪亩征绣航框汇俺挺三哪痊驱慰诸骂枝恨我源涧筐寨盈伶记草治掀生段嗡颜远谅恋肚墒族啡
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818