1. 程式人生 > >自定義字串類MyString的C++實現

自定義字串類MyString的C++實現

/////////////////////////////////////////////////////////////
//
// 自定義字串類MyString
//
/////////////////////////////////////////////////////////
////////////////////
#include <iostream>
#include <cstring>
#include <malloc.h>
using namespace std ;

// 宣告
class MyString ;

// 宣告
ostream& operator << ( ostream& os , const MyString& str ) ;
istream& operator >> ( istream& is , const MyString& str ) ;

// define of class
class MyString
{
public:
	// 預設建構函式
	MyString( char* str = "" ) ;

	// 拷貝建構函式
    MyString( const MyString& str ) ;

	// 過載賦值運算子
	MyString& operator = ( const MyString& str ) ;

	// 過載輸入運算子
	friend istream& operator >> ( istream& is , const MyString& str ) ;

	// 過載輸出運算子
	friend ostream& operator << ( ostream& os , const MyString& str ) ;

	// 過載等於運算子
	bool operator == ( const MyString& ) ;

	// 返回字串的長度
	int length() const { return strlen( ch )  ; }

	// 過載+運算子
	MyString& operator + ( const MyString& str ) ;

	// 在字串中查詢某個字元
	char* findChar( char* start , char* last , char ch ) ;

public:
	char ch[1000] ;
} ;

// implement of class 

// 預設建構函式
MyString::MyString( char* str ) 
{
	int i = 0 ;
	while( str[i] != '\0' )
	{
		ch[i] = str[i] ;
		i++ ;
	}
	ch[i] = '\0' ;
}

// 拷貝建構函式
MyString::MyString( const MyString& str )
{
	int i = 0 ;
	while( str.ch[i] != '\0' )
	{
		ch[i] = str.ch[i] ;
		i++ ;
	}
	ch[i] = '\0' ;
}

// 過載賦值運算子
MyString& MyString::operator = ( const MyString& str )
{
	if( &str != this )
	{
		int i = 0 ;
		while( str.ch[i] != '\0' )
		{
			ch[i] = str.ch[i] ;
			i++ ;
		}
		ch[i] = '\0' ;
	}
	return *this ;
}

// 過載輸出運算子
ostream& operator << ( ostream& os , const MyString& str )
{
	for( int i = 0 ; i < strlen( str.ch ) ; i++ )
	{
		os << str.ch[i] ; 
	}

	return os ;
}

// 過載輸入運算子
istream& operator >> ( istream& is , MyString& str )
{
	char c ;
	int i = 0 ;
	while( 1 )
	{
		is >> noskipws ;
		is >> c ;
		if( c == '\n' )
		{
			break ;
		}
		else
		{
			str.ch[i] = c ;
			i++ ;
		}
	}
	str.ch[i] = '\0' ;

	return is ;
}

// 過載等於運算子
bool MyString::operator == ( const MyString& str )
{
	bool is_OK = true ;

	if( strlen( this->ch ) == strlen( str.ch ) )
	{
		for( int i = 0 ; i < strlen( ch ) ; i++ )
		{
			if( ch[i] != str.ch[i] )
			{
				is_OK = false ;
			}
		}
	}
	else
	{
		return false ;
	}
	return is_OK ;
}

// 過載+運算子
MyString& MyString::operator + ( const MyString& str )
{
	int size = this->length() + str.length() ;

	char* s = ( char* )malloc( sizeof( char ) * size ) ;

	for( int i = 0 ; i < this->length() ; i++ )
	{
		s[i] = this->ch[i] ;
	}

	for( int j = 0 ; j < str.length() ; j++ )
	{
		s[i+j] = str.ch[j] ;
	}

	s[i+j] = '\0' ;   // 字串結尾,必須要注意

	return MyString( s ) ;
}

// 返回指向要查詢的字元的指標,找不到返回NULL
char* MyString::findChar( char* first , char* last , char ch ) 
{
	while( first != last && *first != ch )
	{
		++first ;
	}
	if( first == last )
	{
		return NULL ;
	}
	else
	{
		return first ;
	}
}

//測試程式
int main()
{
	MyString  str( "hello World!" ) ;
	cout << "str=" << str << endl ;
	cout << &str.ch ;

	cout << endl << endl  ;

	MyString str1( str ) ;
	cout << "str1= " << str1 << endl ;
	cout << &str1.ch ;

	cout << endl << endl  ;


	MyString str2 ;
	str2 = str ;
    cout << "str2=" << str2 << endl ;
	cout << &str2.ch ;

	cout << endl << endl  ;


	MyString str3 , str4 ;
	cin >> str3 >> str4 ;
	if( str3 == str4 )
	{
		cout << "str3 == str4 " << endl ;
	}
	else
	{
		cout << "str3 != str4 " << endl ;
	}

	MyString str5 = str3 + str4 ;

	cout << "str5 = " << str5 << endl ;

	char ch ;
	cout << "輸入要查詢的字元:" ;
	cin >> ch ;

	char* pChar = str5.findChar( str5.ch , str5.ch + str5.length() , ch ) ;

	if( pChar != NULL )
	{
		cout << "查詢到的字元為:" << *pChar << endl ;
	}
	else
	{
		cout << "未找到!" << endl ;
	}

	return 0 ;
}