C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

CSTRING类(不是我写的)

////CSTRING.H
// Dream House World Static Model
#ifndef __CSTRING__
#define __CSTRING__

#include <iostream.h>

class StringIndexOutOfBounds { };

class CSTRING
{
public:
CSTRING( const char *cstring = "" ); // Constructor
CSTRING( const CSTRING & str ); // Copy constructor
~CSTRING( ) // Destructor
{ delete [ ] buffer; }

const CSTRING & operator+= ( const int number ); // apend number
const CSTRING & operator= ( const CSTRING & rhs ); // Copy
const CSTRING & operator+=( const CSTRING & rhs ); // Append

const char *c_str( ) const // Return C-style CSTRING
{ return buffer; }
int length( ) const // Return CSTRING length
{ return strLength; }

char operator[]( int k ) const; // Accessor operator[]
char & operator[]( int k ); // Mutator operator[]

enum { MAX_LENGTH = 1024 }; // Maximum length for input CSTRING

private:
char *buffer; // storage for characters
int strLength; // length of CSTRING (# of characters)
int bufferLength; // capacity of buffer
};

ostream & operator<<( ostream & out, const CSTRING & str ); // Output
istream & operator>>( istream & in, CSTRING & str ); // Input
istream & getline( istream & in, CSTRING & str ); // Read line

bool operator==( const CSTRING & lhs, const CSTRING & rhs ); // Compare ==
bool operator!=( const CSTRING & lhs, const CSTRING & rhs ); // Compare !=
bool operator< ( const CSTRING & lhs, const CSTRING & rhs ); // Compare <
bool operator<=( const CSTRING & lhs, const CSTRING & rhs ); // Compare <=
bool operator> ( const CSTRING & lhs, const CSTRING & rhs ); // Compare >
bool operator>=( const CSTRING & lhs, const CSTRING & rhs ); // Compare >=

#endif // __CSTRING__
////////////////////////////////////////////////////////////////////
//CSTRING.CPP
// Dream House World Static Model
#include <string.h>
#include <stdio.h>
#include "CSTRING.h"


CSTRING::CSTRING( const char * cstring )
{
if( cstring == NULL )
cstring = "";
strLength = strlen( cstring );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer, cstring );
}

CSTRING::CSTRING( const CSTRING & str )
{
strLength = str.length( );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer,str.buffer );
}

const CSTRING & CSTRING::operator=( const CSTRING & rhs )
{
if( this != &rhs )
{
if( bufferLength < rhs.length( ) + 1 )
{
delete [ ] buffer;
bufferLength = rhs.length( ) + 1;
buffer = new char[ bufferLength ];
}
strLength = rhs.length( );
strcpy( buffer, rhs.buffer );
}
return *this;
}

const CSTRING & CSTRING::operator+=( const CSTRING & rhs )
{
if( this == &rhs )
{
CSTRING copy( rhs );
return *this += copy;
}

int newLength = length( ) + rhs.length( );
if( newLength >= bufferLength )
{
bufferLength = 2 * ( newLength + 1 );

char *oldBuffer = buffer;
buffer = new char[ bufferLength ];
strcpy( buffer, oldBuffer );
delete [ ] oldBuffer;
}

strcpy( buffer + length( ), rhs.buffer );
strLength = newLength;
return *this;
}

const CSTRING & CSTRING::operator+=(const int number)
{
char temp[20];
sprintf(temp,"%d",number);
CSTRING num( temp );
*this += num;
return *this;
}

char & CSTRING::operator[ ]( int k )
{
if( k < 0 || k >= strLength )
throw StringIndexOutOfBounds( );
return buffer[ k ];
}

char CSTRING::operator[ ]( int k ) const
{
if( k < 0 || k >= strLength )
throw StringIndexOutOfBounds( );
return buffer[ k ];
}

ostream & operator<<( ostream & out, const CSTRING & str )
{
return out << str.c_str();
}

istream & operator>>( istream & in, CSTRING & str )
{
char buf[ CSTRING::MAX_LENGTH + 1 ];
in >> buf;
if( !in.fail( ) )
str = buf;
return in;
}

istream & getline( istream & in, CSTRING & str )
{
char buf[ CSTRING::MAX_LENGTH + 1 ];
in.getline( buf, CSTRING::MAX_LENGTH );
if( !in.fail( ) )
str = buf;
return in;
}

bool operator==( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) == 0;
}

bool operator!=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) != 0;
}

bool operator<( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) < 0;
}

bool operator<=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) <= 0;
}

bool operator>( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) > 0;
}

bool operator>=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) >= 0;
}



posted on 2005-12-06 15:49 梦在天涯 阅读(1517) 评论(0)  编辑 收藏 引用 所属分类: CPlusPlus


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1795780
  • 排名 - 5

最新评论

阅读排行榜