프로그램 작업:MyString 클래스

코드에 직접 부착:
이것은 첫 번째 문제인데, 직접 쓴 것이다.
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class MyString
{
public:
	MyString();
	MyString(char*);
	MyString(const MyString&);
	int getlen() ;
	char& operator [] (int);
	char* operator () (int, int);
	MyString operator += (const char*);
	friend ostream& operator << (ostream& output, const MyString&);
	friend MyString operator + (const MyString&, const MyString&);
	friend bool operator > (MyString&, MyString&);
	friend bool operator < (MyString&, MyString&);
	friend bool operator == (MyString&, MyString&);
private:
	char* str;
};
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; 
}
MyString::MyString()
{
	if(str) delete str;
	str = new char [1];
	str[0] = 0;
}
MyString::MyString(char* x_)
{
	int len = strlen(x_);
	if(str) delete str;
	str = new char [len + 1];
	strcpy(str, x_);
}
MyString::MyString(const MyString& x_)
{
	int len = strlen(x_.str);
	if(str) delete str;
	str = new char [len + 1];
	strcpy(str, x_.str);
}
int MyString::getlen()
{
	return strlen(str);
}
ostream& operator << (ostream& output, const MyString& x_)
{
	output << x_.str;
	return output;
}
MyString operator + (const MyString& x_, const MyString& y_)
{
	int i;
	MyString res;
	int lena = strlen(x_.str), lenb = strlen(y_.str);
	if(res.str) delete res.str;
	res.str = new char [lena + lenb + 1];
	for(i = 0; i != lena; ++i)
	{
		res.str[i] = x_.str[i];
	}
	for(i = 0; i != lenb; ++i)
	{
		res.str[lena + i] = y_.str[i];
	}
	res.str[lena + lenb] = 0;
	return res;
}
char& MyString::operator [] (int x_)
{
	return str[x_];
}
MyString MyString::operator += (const char* x_)
{
	int i;
	int lena = strlen(str), lenb = strlen(x_);
	char *tmp = new char [lena + 1];
	for(i = 0; i != lena; ++i)
	{
		tmp[i] = str[i];
	}
	if(str) delete str;
	str = new char [lena + lenb + 1];
	for(i = 0; i != lena; ++i)
	{
		str[i] = tmp[i];
	}
	for(i = 0; i != lenb; ++i)
	{
		str[lena + i] = x_[i];
	}
	str[lena + lenb] = 0;
	return *this;
}
char* MyString::operator () (int x_, int y_)
{
	int i;
	char* tmp = new char [y_];
	for(i = x_; i != x_ + y_; ++i)
	{
		tmp[i - x_] = str[i];
	}
	tmp[y_] = 0;
	return tmp;
}
bool operator > (MyString& x_, MyString& y_)
{
	int i;
	int lena = x_.getlen(), lenb = y_.getlen();
	int len;
	if(lena < lenb) len = lena;
	else len = lenb;
	for(i = 0; i != len; ++i)
	{
		if(x_.str[i] > y_.str[i])
		{
			return true;
		}
		if(x_.str[i] < y_.str[i])
		{
			return false;
		}
	}
	if(lena < lenb)
	{
		return true;
	}
	return false;
}
bool operator < (MyString& x_, MyString& y_)
{
	if((x_ > y_) || (x_ == y_))
	{
		return false;
	}
	return true;
}
bool operator == (MyString& x_, MyString& y_)
{
	int i;
	int lena = x_.getlen(), lenb = y_.getlen();
	if(lena != lenb)
	{
		return false;
	}
	for(i = 0; i != lena; ++i)
	{
		if(x_.str[i] != y_.str[i])
		{
			return false;
		}
	}
	return true;
}

이것은 두 번째 문제입니다.string류를 계승했습니다.
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class MyString : public string
{
public:
	MyString() : string() { };
	MyString(const char* str) : string(str) { };
	MyString(string str) : string(str) { };
	MyString operator () (int x_, int y_)
	{
		return substr(x_, y_);
	}
};
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; 
}

좋은 웹페이지 즐겨찾기