实现了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;
}