c++做题记录1 01:全面的MyString 查看提交统计提问 总时间限制: 1000ms 内存限制: 65536kB 描述 程序填空,输出指定结果

001:全面的MyString
查看提交统计提问
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,输出指定结果

#include
#include
using namespace std;
int strlen(const char * s)
{ int i = 0;
for(; s[i]; ++i);
return i;
}
void strcpy(char * d,const char * s)
{
int i = 0;
for( i = 0; s[i]; ++i)
d[i] = s[i];
d[i] = 0;

}
int strcmp(const char * s1,const char * s2)
{
for(int i = 0; s1[i] && s2[i] ; ++i) {
if( s1[i] < s2[i] )
return -1;
else if( s1[i] > s2[i])
return 1;
}
return 0;
}
void strcat(char * d,const char * s)
{
int len = strlen(d);
strcpy(d+len,s);
}
class MyString
{
// 在此处补充你的代码
};

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”};
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 = s2 + s4 + " uvw " + “xyz”;
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;
return 0;
}
输入

输出

  1. abcd-efgh-abcd-
  2. abcd-
  3. abcd-efgh-
  4. efgh-
  5. c
  6. abcd-
  7. ijAl-
  8. ijAl-mnop
  9. qrst-abcd-
  10. abcd-qrst-abcd- uvw xyz
    about
    big
    me
    take
    abcd
    qrst-abcd-
    样例输入

    样例输出
  11. abcd-efgh-abcd-
  12. abcd-
  13. abcd-efgh-
  14. efgh-
  15. c
  16. abcd-
  17. ijAl-
  18. ijAl-mnop
  19. qrst-abcd-
  20. abcd-qrst-abcd- uvw xyz
    about
    big
    me
    take
    abcd
    qrst-abcd-
    来源
    Guo Wei
class MyString
{	
	char p[100];
	public:
	MyString(const char *a){
		strcpy(p,a);
	} 
	MyString(){
	}
	~MyString(){
	}
	MyString(const MyString & m){
		if(m.p==NULL)return;
		strcpy(p,m.p);
	}

	friend ostream & operator<<(ostream &o,MyString &m){
		if(m.p)o<<m.p;return o;
	}
	MyString & operator=(const MyString &m){
		strcpy(p,m.p);return *this;
	}
	MyString & operator=(const char *m){
		strcpy(p,m);return *this;
	}	
	MyString operator+(const MyString &m){
		MyString t(*this);
		strcat(t.p,m.p);
		return t;
	}
	MyString & operator+=(const char *m){
		strcat(p,m);
		strcpy(this->p,p);
		return *this;
	}
	friend MyString operator+(const char *p,const MyString &m){
		MyString s;
		strcpy(s.p,p);
		strcat(s.p,m.p);
		return s;
	}
	int operator<(const MyString &m){
		if(strcmp(p,m.p)<0)return 1;return 0;
	}
	bool operator==(const MyString &m){		
		return !strcmp(p,m.p);
	}
	bool operator>(const  MyString &m){
		return strcmp(p,m.p);
	}
	char & operator[](int i){
		return p[i];
	}
	char * operator()(int i,int len){
		char *k=new char[100];
		int l=i,t=0;
		for(;l<i+len;l++,t++)
			k[t]=p[l];
		k[t]=0;
		return k;
	}
};

笔者有些地方没做好,可能会出现内存泄漏。

关机重启就好了

你可能感兴趣的:(c++,c++)