自定义字符串类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 ;
}





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值