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

C++面向对象学习:全面的MyString

程序员文章站 2024-03-17 19:21:10
...

今天完成到第五周的任务了,课程也终于学完了一半了。
不得不说,学习了面向对象之后,感觉对于c++的构成有了更加深入的了解。之前很多没有搞懂的原理在学了之后感觉其实也挺简单的。感觉设计者的思路真的很巧妙啊。
题目链接
题目有一点繁琐,就是各种各样的重载操作。没有什么好说的。
不过在做题目的时候,还是遇到了几个问题:
strlen 不能够去求指针NULL的长度,cout 输出字符也是以’\0’ 结尾的,所以不能输出NULL。
感觉如果在大程序之中,如果在调用返回的时候都构造一个新的对象 程序应该比较慢。

#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
{
    public:
        int Len;
        char *Sequnece;
        MyString(const char *s=NULL){
            if (s){
                Len = strlen(s);
                Sequnece = new char [Len+1];
                strcpy(Sequnece, s);
            }
            else {
                Len = 0;
                Sequnece = NULL;
            }
        }
        MyString(const MyString & s){
            if (s.Len){
                Len = s.Len;
                Sequnece = new char [Len+1];
                strcpy(Sequnece, s.Sequnece);
            }
            else {
                Len = 0;
                Sequnece = NULL;
            }
        }
        ~MyString(){
            delete []Sequnece;
        }
        MyString & operator=(const MyString &s){
            if (s.Sequnece == Sequnece) return *this;
            if (s.Len){
                delete []Sequnece;
                Len = s.Len;
                Sequnece = new char [Len+1];
                strcpy(Sequnece, s.Sequnece);
            }
            else {
                Len = 0;
                Sequnece = NULL;
            }
            return *this; 
        }
        MyString operator+(const MyString &s){
            MyString ss;
            ss.Len = Len + s.Len;
            ss.Sequnece = new char[ss.Len+1];
            strcpy(ss.Sequnece, Sequnece);
            strcat(ss.Sequnece, s.Sequnece);
            return ss;
        }
        MyString & operator+=(const MyString &s){
            char *ss = new char [Len+1];
            strcpy(ss, Sequnece);
            delete []Sequnece;
            Len = Len + s.Len;
            Sequnece = new char [Len+1];
            strcpy(Sequnece, ss);
            strcat(Sequnece, s.Sequnece);
            return *this;
        }
        char* operator()(int t,int l){
            char *s = new char [l+1];
            int sp = 0;
            for (int i = t; i < t + l; i++) s[sp++] = Sequnece[i];
            s[sp]=0;
            return s;
        }
        char &operator[](const int &i){
            return Sequnece[i];
        }
        friend ostream & operator<<(ostream & o,const MyString & s){
            if (s.Len) o<<s.Sequnece;
            return o;
        }
        friend MyString operator+(const char *l,const MyString & r){
            MyString ss;
            ss.Len = strlen(l) + r.Len;
            ss.Sequnece = new char[ss.Len+1];
            strcpy(ss.Sequnece, l);
            strcat(ss.Sequnece, r.Sequnece);
            return ss;
        }
        friend int operator<(const MyString &l,const MyString &r){
            int f = strcmp(l.Sequnece, r.Sequnece);
            if (f == -1) return 1;
            return 0;
        }
        friend int operator==(const MyString &l,const MyString &r){
            int f = strcmp(l.Sequnece, r.Sequnece);
            if (f == 0) return 1;
            return 0;
        }
        friend int operator>(const MyString &l,const MyString &r){
            int f = strcmp(l.Sequnece, r.Sequnece);
            if (f == 1) return 1;
            return 0;
        }
};


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<< "\n";
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << "\n";
	cout << "3. " << s2 << "\n";
	cout << "4. " << s3 << "\n";
	cout << "5. " << s4 << "\n";
	cout << "6. " << s1[2] << "\n";
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A' ;
	cout << "7. " << s2 << "\n";
	cout << "8. " << s1 << "\n";
	s1 += "mnop";
	cout << "9. " << s1 << "\n";
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << "\n";
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << "\n";
	qsort(SArray,4,sizeof(MyString),CompareString);
	for( int i = 0;i < 4;i ++ )
	cout << SArray[i] << "\n";
	//s1的从下标0开始长度为4的子串
	cout << s1(0,4) << "\n";
	//s1的从下标5开始长度为10的子串
	cout << s1(5,10) << "\n";
	return 0;
}

相关标签: C++面向对象