这道题容易在一些细节方面出问题,比如数组开的不够大,没有考虑字符串最后一位是'\0'的设计等因素,所以容易崩……最为纠结的是g++编译器不承认
MyString SArray[4]={"big","me","about","take"};
这种写法,而c++编译器是可以通过的。
此外,由于用一句话之内是从右向左进行运算的,这样就导致了s1="cvb"+="xye”的语句不能重载,不好解决,这也是一个很纠结的问题。
根据我的观察,一个运算符在进行多次重载之后,决定哪一个运算符对应的代码这个过程是从左向右进行的,但是在一个表达式之内求职是从右向左进行的,然后程序就崩溃了……
/*
* 141.cpp
*
* Created on: 2010-5-14
* Author: zhanghan
*/
#include<iostream>
#include<stdlib.h>
#include<string>
#include<string.h>
using namespace std;
class MyString
{ friend MyString & operator+ (const char * , const string);
friend ostream & operator<<(ostream & out , const MyString & );//对<<的重载
friend MyString & operator+ (const char * , const MyString & );//对字符串加类的特殊类型的重载
private:
char *str;//指向字符串的首地址的指针
int len;//字符串的长度
public:
MyString(char * s="");//构造函数
~MyString();//析构函数
MyString(MyString &);
const MyString & operator= ( const MyString &);//对=的重载
const MyString & operator= ( const char *);//右端是字符串类型
char & operator[](const int &);//对[]的重载
MyString & operator+ ( const MyString & );//对+的重载
MyString & operator+ ( const char * );//对+的重载,类+字符串型
MyString & operator+=(const char *);//对+=的重载
bool operator<( const MyString & );
bool operator==(const MyString &);
bool operator>(const MyString &);
char* & operator()(int ,int );
int GetLength();
void SetString(const char *);
};
//构造函数
MyString :: MyString(char * p )//对构造函数以及析构函数的写法要清楚,默认是没有返回值的
{
SetString(p);
}
//析构函数
MyString :: ~MyString()
{
delete []str;
}
//复制构造函数
MyString :: MyString (MyString & p)
{
SetString(p.str);
}
//对<<的重载
ostream & operator<<(ostream & out , const MyString & x )
{
out<<x.str;
return out;
}
//对+的重载
//对=运算符的重载
const MyString & MyString:: operator=( const MyString & x )
{
if(&x!=this)//防止自身赋值
{
delete []str;
SetString(x.str);
}
return *this;
}
const MyString & MyString :: operator= ( const char * p)
{
delete[]str;
SetString(p);
return *this;
}
//对[]的重载
char & MyString:: operator[](const int & num)
{
return str[num];
}
MyString & operator+ (const char * p, const string q)
{
static MyString temp3;
delete[]temp3.str;
temp3.len=0;
int l=strlen(p);
temp3.str = new char[l + q.size() +1 ];
int i=0;
for(i=0;i<l;i++)
temp3.str[i]=p[i];
for(i=0;i<q.size();i++)
temp3.str[i+l]=q[i];
temp3.str[l + q.size()]=0;
return temp3;
}
MyString & MyString :: operator+( const MyString & x )
{
static MyString temp2;
delete[]temp2.str;
temp2.len=0;
temp2.str = new char[len + x.len +1 ];
strcpy(temp2.str,str);
strcat(temp2.str,x.str);
temp2.len=len+x.len;
return temp2;
}
//对+的重载,处理加字符串加string类的情况
MyString & operator+ (const char * p , const MyString & x)
{
static MyString temp;
delete[]temp.str;
temp.len=0;
int l=strlen(p);
int k = x.len;
temp.str = new char[l + k +1 ];
memset(temp.str,0,sizeof(temp.str));
strcpy(temp.str,p);
strcat(temp.str,x.str);
temp.len=x.len+strlen(p);
return temp;
}
//对+的重载,处理类加字符串的情况
MyString & MyString :: operator+ (const char * p )
{
static MyString temp1;
delete[]temp1.str;
temp1.len=0;
temp1.str = new char[len + strlen(p) +1 ];
strcpy(temp1.str,str);
strcat(temp1.str,p);
temp1.len =len+ strlen(p);
return temp1;
}
MyString & MyString ::operator+=( const char * p)
{
char *tem=str;
len+=strlen(p);
str=new char [len+1];
if(str!=0)
strcpy(str,tem);
strcat(str ,p);
return *this;
}
char* & MyString :: operator()(int start , int size)
{
static char *tem;
int i=0;
tem= new char [size+1];
for(i=start;i<start+size;i++)
tem[i-start]=str[i];
tem[size]=0;
return tem;
}
bool MyString :: operator<(const MyString & s1 )
{
return strcmp(str,s1.str)<0;
}
bool MyString :: operator==(const MyString & s1 )
{
return strcmp(str,s1.str)==0;
}
bool MyString :: operator>(const MyString & s1 )
{
return strcmp(str,s1.str);
}
int MyString:: GetLength()
{
return len;
}
void MyString ::SetString(const char * p )
{
if(p)
len=strlen(p);
else len=0;
if(p)
{
str=new char [len+1];//字符串以'\0\结尾
if(str!=0)
strcpy(str,p);
}
}
int CompareString(const void * e1,const void *e2)
{
MyString *s1=(MyString *)e1;
MyString *s2=(MyString *)e2;
if( *s1 < *s2 )
return -1;
else if(* s1 == *s2)
return 0;
else if( *s1 > *s2 )
return 1;
}
int main()
{
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4];//={"big","me","about","take"};//这个c++可以过,g++的编译问题
SArray[0]="big";
SArray[1]="me";
SArray[2]="about";
SArray[3]="take";
cout<<"1."<<s1<<s2<<s3<<s4<<endl;
s4=s3;
s3=s1+s3;
cout<<"2. "<<s1<<endl;
cout<<"3. "<<s2<<endl;
cout<<"4. "<<s3<<endl;
cout<<"5. "<<s4<<endl;
cout<<"6. "<<s1[2]<<endl;
s2 = s1;
s1 = "ijkl-";//这里在思考
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 ="xyz"+ s2 + s4 + " uvw " + s3+ " -asdfasf";
//s1= s1+"xyz" ;//从右向左加!!!但是又不能重载这种类型,如何解决?
//s1 = s1 ;
cout << "11. " << s1 << endl;
qsort(SArray,4,sizeof(MyString),CompareString);
for( int i = 0;i < 4;i ++ )
cout << SArray[i] << endl;
//输出s1的下标为0的字符开始长度为4的子串 cout << s1(0,4) << endl; //输出s1的下标为5的字符开始长度为10的子串 cout << s1(5,10) << endl; }
cout << s1(0,4) << endl;
cout << s1(5,10) << endl;
return 0;
}