CString成员函数的实现(符号重载、动态内存)

实现了CString 类,完成了+、=、+=、<、<<、>>、[] 等符号的重载,带参/无参的动态内存存储的构造函数

实现了字符串排序、查找特殊字符等功能。

写了一堆bug,改了好久,勉强实现了。还有很多没有完善和优化的地方,有时间再改一改。

StringLow和StringUp的意思好像是把字符串改成大小写,我好像理解错题意了.....

#include<iostream>
#include<string.h>
using namespace std;
class CString{
private:
	char *m_pcData;//保存字符串
public:
	CString(const char*pcStr);
	CString(const CString &iStr);
	CString(){m_pcData=NULL;}
	~CString(){delete m_pcData;}
	CString(string a);
	CString & operator+(CString &a);
	CString & operator=(const CString &);
	void StringLow();
	void StringUp();
	int FindCharNumber(char);
	char & operator[](int i);
	CString &operator+=(CString &a);
	friend bool operator<(const CString &a,const CString &b);
	friend bool operator==(const CString &a,const CString &b);
	friend ostream &operator<<(ostream &output,CString &obj);
	friend istream &operator>>(istream &input,CString &obj);

};
//类构造
CString::CString(const CString &iStr){
	int len = strlen(iStr.m_pcData);
	m_pcData= new char[len+1];
	strcpy(m_pcData,iStr.m_pcData);
}
//参构造
CString::CString(const char*pcStr){
	int len =strlen(pcStr);
	m_pcData= new char[len+1];
	strcpy(m_pcData,pcStr);
}
//=重载
CString & CString::operator=(const CString & a){
	int len =strlen(a.m_pcData);
	m_pcData= new char[len+1];
	strcpy(m_pcData,a.m_pcData);
	return *this;
}
// +重载
CString &CString::operator+(CString &a){

	int len1 = strlen(m_pcData);
	int len2 = strlen(a.m_pcData);
	char *mpc = new char[len1+1];
	strcpy(mpc,m_pcData);
	m_pcData = new char[len1+len2+1];
	strcpy(m_pcData,mpc);
	strcat(m_pcData,a.m_pcData);
	return *this;
}
// += 重载
CString &CString::operator+=(CString &a){
	int len1 = strlen(m_pcData);
	int len2 = strlen(a.m_pcData);
	char *mpc = new char[len1+1];
	strcpy(mpc,m_pcData);
	m_pcData = new char[len1+len2+1];
	strcpy(m_pcData,mpc);
	strcat(m_pcData,a.m_pcData);
	return *this;

}
// []重载
char &CString::operator[](int i){
          if( i > strlen(m_pcData))
          {
              cout << "索引超过最大值" <<endl; 
              return m_pcData[0];
          }
          return m_pcData[i];
}
//从小到大排序
void CString::StringLow(){
	char cha;
	for(int i= 0;i<strlen(m_pcData);i++)
	{
		for (int j=0;j<strlen(m_pcData)-i;j++)
		{
			if(m_pcData[j]>m_pcData[j+1])
			{
				cha=m_pcData[j];
				m_pcData[j]=m_pcData[j+1];
				m_pcData[j+1]=cha;
			}
		}
	}
}
//从大到小排序
void CString::StringUp(){
	char cha;
	for(int i= 0;i<strlen(m_pcData);i++)
	{
		for (int j=0;j<strlen(m_pcData)-i;j++)
		{
			if(m_pcData[j]<m_pcData[j+1])
			{
				cha=m_pcData[j];
				m_pcData[j]=m_pcData[j+1];
				m_pcData[j+1]=cha;
			}
		}
	}
}
//查找字符个数
int CString::FindCharNumber(char cha){
	int num=0;
	for(int i=0;i<strlen(m_pcData);i++)
	{
		if(m_pcData[i]==cha)
			num++;
	}
	return num;
}
bool operator<(const CString &a,const CString &b){

	return (strcmp(a.m_pcData,b.m_pcData)<0);
}
bool operator==(const CString &a,const CString &b){
	return(strcmp(a.m_pcData,b.m_pcData)==0);
}
//重载输出
ostream &operator<<(ostream &output,CString &obj){
	if(obj.m_pcData!=NULL)
		output<<obj.m_pcData;
	else
		output<<"NULL";
	return output;
}
//重载输入
istream &operator>>(istream &input,CString &obj){

	char temp[10];
	input.get(temp,10);//istream&get(char * s,streamsize n);
	if(input)
		obj=temp;
	while(input&&input.get()!='\n')
		continue;
	return input;
}

int main(){
	CString a("abCd");
	cout<<a<<endl;
	CString b ="WwwC";
	cout<<b<<endl;
	CString c(a);
	cout<<c<<endl;
	CString d;
	CString e =a;
	cout<<"e is "<<e<<endl;
	CString f;
	cout<<"请输入f:"<<endl;
	cin>>f;
	cout<<f<<endl;
	CString g;
	g=a+b;
	if(a<b)
		cout<<"a<b"<<endl;
	else
		cout<<"a>=b"<<endl;
	if(e==a)
		cout<<"e==a"<<endl;
	else 
		cout<<"e!=a"<<endl;
	b+=a;
	cout<<a<<endl;
	cout<<b<<endl;
	a.StringLow();
	b.StringUp();
	cout<<a<<endl;
	cout<<b<<endl;
	b.StringLow();
	cout<<"The total of 'w' in b is :"<<b.FindCharNumber('w')<<endl;
	cout<<c<<endl;
	cout<<d<<endl;
	cout<<e<<endl;
	cout<<f<<endl;
	cout<<g<<endl;
	g[2]='B';
	cout<<g[2]<<endl;
	cout<<g<<endl;
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值