c++编程练习 020:全面的MyString

北大程序设计与算法(三)测验题汇总(2020春季)


描述

程序填空,输出指定结果

#include <cstdlib>
#include <iostream>
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-

样例输入

样例输出

  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-
    来源
    Guo Wei

分析

首先定义一个成员,用于存储字符串

private:
	char *str;
  1. MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);构造函数构造;
MyString(){
		str = new char[2];
		str[0] = '\0';
} 
MyString(const char *_str){//字符串
	str = new char[strlen(_str) + 1];
	strcpy(str,_str);
}
MyString(const MyString & s){//复制构造
		if(strlen(str) < strlen(s.str)){
			delete []str;
			str = new char[strlen(s.str) + 1];
		}
		strcpy(str,s.str);
}
  1. <<的重载
    cout << "1. " << s1 << s2 << s3<< s4<< endl;
friend ostream & operator << ( ostream & o,const MyString & s) {
		o << s.str ; 
		return o;
}
  1. =的重载
    s4 = s3;
MyString & operator=(const MyString & s) {
		if(str == s.str)
			return *this;
		if(strlen(str) < strlen(s.str)){
			delete []str;
			str = new char[strlen(s.str) + 1];
		}
		strcpy(str,s.str);
		return *this;
}

返回对象的引用目的是保证可以连等。

  1. +的重载
    s3 = s1 + s3;
MyString operator + ( const MyString & s ) {
		char *temp = new char[strlen(str) + strlen(s.str) + 2];
		strcpy(temp,str);
		strcat(temp,s.str);
		MyString os(temp);
		delete []temp;
		return os;
}

注意此处不能是返回引用,因为如果返回引用的话相当于s1 + s3生成的临时对象的引用传给s3显然这是不合理的,类型不匹配。
5. []的重载
cout << "6. " << s1[2] << endl;
s1[2] = 'A' ;

char & operator[](int i){ 
		return str[i];
}

返回参数的应用的目的是保证第二条赋值代码顺利进行。
6. =重载
s1 = "ijkl-";可能有同学会想,这儿难道不可以调用构造函数成临时对象在利用等号重载吗?其实,这儿如果调用构造函数就必须是初定义的时候才会调用,而s1显然不是此时初定义。

MyString & operator=(const char * s ) {
		if(strlen(str) < strlen(s)){
			delete []str;
			str = new char[strlen(s) + 1];
		}
		strcpy(str,s);
		return *this;
	}
  1. +重载
    s4 = "qrst-" + s2;
friend MyString operator +( const char * s1,const MyString & s2) {
		MyString tmp(s1); 
		tmp+= s2; 
		return tmp;
}
  1. +=重载
    s1 += "mnop";//此处会发生临时对象创建
MyString & operator += ( const MyString & s) {
		char *temp = new char[strlen(str) + strlen(s.str) + 2];
		strcpy(temp,str);
		strcat(temp,s.str);
		delete []str;
		str = temp;
		return *this;
	}
  1. ()重载
MyString operator()(int start,int len) const { 
		char * tmp = new char[len + 1]; 
		for( int i = 0;i < len ; ++i) 
			tmp[i] = str[start+i];
		tmp[len] = 0; 
		MyString s(tmp); 
		delete [] tmp; 
		return s;
	}

其中调用构造函数的有

MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);

MyString SArray[4] = {"big","me","about","take"};//类型转换

s1 = "ijkl-";//类型转换

s1 += "mnop";//类型转换

s1 = s2 + s4 + " uvw " + "xyz";//类型转换

值得说明的是,在测试中发现,如果s4 = "cxasv" + s2;是按照s4 = s2 + "cw";的方式书写的话,其实直接调用类型转换构造函数即可,不需要额外重载+

至于s1 = s2 + s4 + " uvw " + "xyz";//类型转换的计算顺序,我想了很久,经过测试,还是按照从左到右的顺序计算,此处利用+重载。

发布了196 篇原创文章 · 获赞 47 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44116998/article/details/104375273
今日推荐