欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

019:全面的MyString

程序员文章站 2022-03-14 09:58:21
...

题目:

程序填空,输出指定结果

#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.
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
big
me
take
abcd
qrst-abcd-

分析:

    MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);  
    //1。构造函数(字符串);2。复制构造函数

    MyString SArray[4] = {"big","me","about","take"}; 
    //批量构造,该如何处理?不用处理。

    cout << "1. " << s1 << s2 << s3<< s4<< endl; //重载"<<"

    s4 = s3; //赋值,重载"="

    s3 = s1 + s3; //重载 "+",类+类,实现字符串的拼接。

    cout << "6. " << s1[2] << endl; //重载"[]"

    s1 = "ijkl-"; //实质是:s1 = Mystring("ijkl-"),由重载的"="实现。

    s1[2] = 'A' ; //重载"[]",要返回char型变量地址

    s1 += "mnop"; //重载"+=",相当于 s1 = s1 + MyString(mnop)

    s4 = "qrst-" + s2; //再重载 "+",字符 + 类

    s1 = s2 + s4 + " uvw " + "xyz"; 
    //再重载 "+",类+字符 ,字符+字符 不用处理,因为是从左往右计算的

    qsort(SArray,4,sizeof(MyString),CompareString); //CompareString中要重载三个比较符。

    cout << s1(0,4) << endl; //重载"()",返回字符串

代码:

#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
{
// 在此处补充你的代码
private:
    char * Str;
public:
    //把上面的一大堆函数声明为友元。
    friend void strcat(char * d,const char * s);
    friend int strcmp(const char * s1,const char * s2);
    friend void strcpy(char * d,const char * s);
    friend int strlen(const char * s);

    //构造函数(字符串 或 空)
    MyString(const char * _Str = NULL){
        if(_Str){
            Str = new char[strlen(_Str) + 1];
            strcpy(Str,_Str);
        }
        else Str = NULL;
    }

    //复制构造函数
    MyString(const MyString & MyStr){
        if(MyStr.Str){
            Str = new char[strlen(MyStr.Str) + 1];
            strcpy(Str,MyStr.Str);
        }
        else Str = NULL;
    }

    //析构函数
    ~MyString(){
        if(Str) delete []Str;
    }

    //重载"<<"
    friend ostream & operator<<(ostream & os, const MyString & MyStr){
        if(MyStr.Str) os << MyStr.Str;
        return os;
    }

    //重载"=",深拷贝.
    MyString & operator=(const MyString & MyStr){
        if(Str == MyStr.Str) return *this;
        if(Str) delete []Str;
        if(MyStr.Str){
            Str = new char[strlen(MyStr.Str) + 1];
            strcpy(Str,MyStr.Str);
        }
        else Str = NULL;
        return *this;
    }

    //重载 "+" 类+类 ,注意不能直接加在this上面。
    MyString operator+(const MyString & MyStr){
        MyString tmp;
        tmp.Str = new char(strlen(Str) + strlen(MyStr.Str) + 1);
        strcpy(tmp.Str, Str);
        strcat(tmp.Str,MyStr.Str);
        return tmp;
    }

    //再重载 "+",字符 + 类
    friend MyString operator+(char * _Str, MyString & MyStr){
        return MyString(_Str) + MyStr;
    }

    //再重载 "+",类 + 字符
    MyString operator+(char * _Str){
        return *this + MyString(_Str);
    }

    //重载"[]"
    char & operator[](int idx){
        return Str[idx];
    }

    //重载"+=",相当于 s1.Str + _Str
    MyString & operator+=(char * _Str){
        char * tmp = new char[strlen(Str) + 1];
        strcpy(tmp, Str);
        if(Str) delete []Str;
        Str = new char(strlen(tmp) + strlen(_Str) + 1);
        strcpy(Str, tmp);
        strcat(Str,_Str);
        delete []tmp;
        return *this;
    }

    //重载"()",返回字符串
    MyString operator()(int idx,int len){
        char tmp[len + 1];
        int i;
        for(i = 0; i < len; i++){
            tmp[i] = Str[i+idx];
        }
        tmp[i] = 0;
        return MyString(tmp);
    }

    //重载"<"
    bool operator<(const MyString MyStr){
        if( strcmp( Str, MyStr.Str) == -1 ){
            return true;
        }
        return false;
    }

    //重载"=="
    bool operator==(const MyString MyStr){
        if( strcmp( Str, MyStr.Str) == 0 ){
            return true;
        }
        return false;
    }

    //重载">"
    bool operator>(const MyString MyStr){
        if( strcmp( Str, MyStr.Str) == 1 ){
            return true;
        }
        return false;
    }
};


int CompareString( const void * e1, const void * e2)
{
    MyString * s1 = (MyString * ) e1; //将指针转回MyString类型。
    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);  //1。构造函数(字符串);2。复制构造函数
    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 = Mystring("ijkl-"),由重载的"="实现。
    s1[2] = 'A' ; //重载"[]",要返回char型变量地址
    cout << "7. " << s2 << endl;
    cout << "8. " << s1 << endl;
    s1 += "mnop"; //重载"+=",相当于 s1 = s1 + MyString(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);//CompareString中要重载三个比较符。
    /*
    C 库函数
    void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
    对数组进行排序。
    参数
    base -- 指向要排序的数组的第一个元素的指针。
    nitems -- 由 base 指向的数组中元素的个数。
    size -- 数组中每个元素的大小,以字节为单位。
    compar -- 用来比较两个元素的函数。
    返回值
    该函数不返回任何值。
    */
    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;
}