自定义类_string类

来源:互联网 发布:邯郸市教育局网络平台 编辑:程序博客网 时间:2024/04/26 13:28

#include "iostream.h"

////////////////////////////////////////////////////////////
// 功能: 实现自定义 String 类
// 时间: 2005-11-2
// 最后修改时间: 2005-11-18
// 备注: 修正前一版本中内存泄漏问题
////////////////////////////////////////////////////////////
#pragma once

#include "String.h"
#define TRIM_LEFT 1
#define TRIM_RIGHT 2
#define TRIM_BOTH 0

class String
{
public:
 String();
 String( char *string );
 String( String &str );
 ~String();
public:
 String operator = ( String &string );
 String operator + ( String &stirng );
 operator char * ();
public:
 char GetAt( int index );
 int Len();
 int FindFirstSub( String string , int start = 0 );
 bool InStr( String string );
 String Trim( int part = TRIM_BOTH );
 String Left( int sublen );
 String Right( int sublen );
 String Mid( int start , int sublen );
 void Replace( String strTobeReplaced , String strReplace );
protected:
 char *str;
 int len;
};

////////////////////////////////////////////////////////////
// 构造函数、析构函数

//
String::String()
{
 len = 0;
 str = new char[len+1];
 str[0] = 0;
}
//
String::String( char *string )
{
 len = strlen( string );
 str = new char[len+1];
 strcpy( str , string );
}
//
String::String( String &string )
{
 len = string.len;
 str = new char[len+1];
 strcpy( str , string.str );
}
//
String::~String()
{
 delete[]str;
}
//


////////////////////////////////////////////////////////////
// 操作符重载函数

//
String String::operator = ( String &string )
{
 //根据string内容,调整自身
 len = string.len;
 delete[]str;
 str = new char[len+1];
 strcpy( str , string.str );
 
 String resultString( string );
 return resultString;
}
//
String String::operator + ( String &string )
{
 //计算新字符串的大小、并为其分配空间
 int len1 = len;
 int len2 = string.len;

 int len0 = len1 + len2;
 char *string0 = new char[len0+1];

 //生成新的字符串
 strcpy( string0 , str );
 strcat( string0 , string.str );
 
 //生成新的字符串对象
 String resultString( string0 );

 //回收分配的空间
 delete[]string0;

 return resultString;
}
//
String::operator char *()
{
 return str;
}
//


////////////////////////////////////////////////////////////
// 功能函数

//
//按编号返回字符,首字符的编号为0,最后一个字符编号为len-1(len为其长度)
char String::GetAt( int index )
{
 if( index > len || index < 0 )
  return 0;
 return str[index];
}
//
int String::Len()
{
 return len;
}
//
int String::FindFirstSub( String string , int start )
{
 if( start >= len || start < 0 )
  return -1;

 for( int i = start ; i <= len - string.len ; i++ )
 {
  if( str[i] == string.str[0] )
  {
   bool flag = true;
   int j;
   for( j = 0 ; flag && j < string.len && (i + j < len) ; j++ )
   {
    if( str[i+j] != string.str[j] )
     flag = false;
   }
   if( j >= string.len && flag )
   {
    return i;
   }
  }
 }
 return -1;
}
//
bool String::InStr( String string )
{
 if( FindFirstSub( string , 0 ) < 0 )
  return false;
 else
  return true;
}
//
String String::Trim( int part )
{
 //有效字符串的起始位置
 int i;
 int begin = 0;
 int end = len - 1;

 if( part == TRIM_LEFT || part == TRIM_BOTH )
 {
  while( str[begin] == ' ' )
   begin++;
 }
 if( part == TRIM_RIGHT || part == TRIM_BOTH )
 {
  while( end >= 0 && str[end] == ' ' )
   end--;
 }

 if( begin > end )
 {
  len = 0;
  delete[]str;
  str = new char[len+1];
  str[0] = 0;
 }
 else
 {
  len = end - begin + 1;
  char *newStr = new char[len+1];
  char *oldStr = str;
  for( i = begin ; i <= end ; i++ )
   newStr[i-begin] = str[i];
  newStr[i-begin] = 0;
  str = newStr;
  delete[]oldStr;
 }

 String resultString( str );
 return resultString;
}
//
String String::Left( int sublen )
{
 if( sublen > len )
  sublen = len;
 if( sublen < 0 )
  sublen = 0;

 char *substr = new char[sublen+1];
 for( int i = 0 ; i < sublen ; i++ )
  substr[i] = str[i];
 substr[sublen] = 0;
 String resultString( substr );
 delete[]substr;

 return resultString;
}
//
String String::Right( int sublen )
{
 if( sublen > len )
  sublen = len;
 if( sublen < 0 )
  sublen = 0;

 char *substr = new char[sublen+1];
 for( int i = 0 ; i < sublen ; i++ )
  substr[i] = str[len-sublen+i];
 substr[sublen] = 0;
 String resultString( substr );
 delete[]substr;

 return resultString;
}
//
String String::Mid( int start , int sublen )
{
 if( start >= len || sublen < 1 )
 {
  String resultString;
  return resultString;
 }

 if( start < 0 )
  start = 0;
 if( start + sublen > len )
  sublen = len - start;

 int i;
 int begin = start;
 int end = start + sublen - 1;
 
 char *newStr = new char[end-begin+2];
 for( i = begin ; i <= end ; i++ )
  newStr[i-begin] = str[i];
 newStr[i-begin] = 0;

 String resultString( newStr );
 delete[]newStr;

 return resultString;
}
//
void String::Replace( String strTobeReplaced , String strReplace )
{
 String temp;
 int start = 0;
 int index = FindFirstSub( strTobeReplaced , start );
 while( index >= 0 )
 {
  temp = temp + Mid( start , index - start ) + strReplace;

  start = index + strTobeReplaced.len;
  index = FindFirstSub( strTobeReplaced , start );
 }
 temp = temp + Right( len - start );

 len = temp.len;
 delete[]str;
 str = new char[len+1];
 strcpy( str , temp.str );
}
//
// String 类结束
////////////////////////////////////////////////////////////


void main()
{
 String str1( "abcd" );
 cout<< str1 <<endl;

 String str2( str1 );
 cout<< str2 <<endl;

 String str3;
 cout<< (str3 = str1) <<endl;
 cout<< str3 <<endl;

 String str4 = str1 + str2;
 cout<< str4 <<endl;
 cout<< str4 <<endl;

 cout<< str4.GetAt(0) <<endl;
 cout<< str4.GetAt(3) <<endl;
 cout<< str4.GetAt(7) <<endl;

 String str5( "bcd" );
 cout<< str5 <<endl;
 cout<< str4.FindFirstSub( "bcd" ) <<endl;
 cout<< str4.FindFirstSub( str5 ) <<endl;
 cout<< str4.FindFirstSub( str5 , 1 ) <<endl;
 cout<< str4.FindFirstSub( str5 , 2 ) <<endl;

 String str6 = "             a              ";
 String str7 = str6;
 String str8 = str6;

 cout<< str6 <<"|"<<endl;
 cout<< str7.Trim( TRIM_LEFT ) <<"|"<<endl;
 cout<< str8.Trim( TRIM_RIGHT ) <<"|"<<endl;
 cout<< str6.Trim() <<"|"<<endl;


 String str9 = "abcdefghijk";
 cout<< str9 <<endl;
 cout<< str9.Left( -1 ) <<"."<<endl;
 cout<< str9.Left( 0 ) <<"."<<endl;
 cout<< str9.Left( 1 ) <<"."<<endl;
 cout<< str9.Left( 2 ) <<"."<<endl;
 cout<< str9.Left( 3 ) <<"."<<endl;
 cout<< str9.Left( 11 ) <<"."<<endl;
 cout<< str9.Left( 12 ) <<"."<<endl;

 String str10 = "abcdefghijk";
 cout<< str10 <<endl;
 cout<< str10.Right( -1 ) <<"."<<endl;
 cout<< str10.Right( 0 ) <<"."<<endl;
 cout<< str10.Right( 1 ) <<"."<<endl;
 cout<< str10.Right( 2 ) <<"."<<endl;
 cout<< str10.Right( 3 ) <<"."<<endl;
 cout<< str10.Right( 11 ) <<"."<<endl;
 cout<< str10.Right( 12 ) <<"."<<endl;


 String str11 = "abcdef";
 cout<< str11 <<endl;
 cout<< str11.Mid( 0 , 1 ) <<"."<<endl;
 cout<< str11.Mid( 0 , 2 ) <<"."<<endl;
 cout<< str11.Mid( -1 , 3 ) <<"."<<endl;
 cout<< str11.Mid( 0 , 6 ) <<"."<<endl;
 cout<< str11.Mid( 0 , 7 ) <<"."<<endl;

 cout<< str11.Mid( 1 , 1 ) <<"."<<endl;
 cout<< str11.Mid( 1 , 2 ) <<"."<<endl;
 cout<< str11.Mid( 1 , 3 ) <<"."<<endl;
 cout<< str11.Mid( 1 , 6 ) <<"."<<endl;
 cout<< str11.Mid( 1 , 7 ) <<"."<<endl;

 cout<< str11.Mid( 5 , 1 ) <<"."<<endl;
 cout<< str11.Mid( 5 , 2 ) <<"."<<endl;

 cout<< str11.Mid( 6 , 1 ) <<"."<<endl;


 char *a = str11;
 cout<<a<<endl;
 a[0] = 'h';
 a[1] = 0;
 cout<<a<<endl;
 cout<<str11<<endl;


 String str12 = "abc";
 cout<< str12 <<endl;
 str12.Replace( "a" , "aa" );
 cout<< str12 <<endl;
 str12.Replace( "b" , "bbb" );
 cout<< str12 <<endl;
 str12.Replace( "c" , "cccc" );
 cout<< str12 <<endl;

 String str13 = "       a            b           c             ";
 cout<< str13 <<"."<<endl;
 while( str13.InStr( "  " ) )
  str13.Replace( "  " , " " );
 cout<< str13 <<"."<<endl;
 str13.Trim();
 cout<< str13 <<"."<<endl;

}

原创粉丝点击