String:自动进行空间扩展

自动进行空间扩展

/*
 * The implementation of String like vector
 
*/
#include <iostream>
#include <cstring>
#include <iomanip>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

using std::istream;
using std::ostream;
using std::cout;
using std::endl;
using std::cin;
using std::setw;

class String{
    public:
        friend istream& operator>>(istream&,String&);
        friend ostream& operator<<(ostream&,const String&);
        friend String operator+(const String&,const String&);
        friend String operator+(const String&,const char*);
    public:
        String():_string(0),_used(0),_len(0){}
        String(int,char);
        String(const char*);
        String(const String&);
        ~String(){delete[] _string;}
        String& operator=(const String&);
        String& operator=(const char*);
        String& operator+=(const String&);
        String& operator+=(const char*);
        String& operator+=(const char&);
        bool operator==(const String &)const;
        bool operator!=(const String &)const;
        bool operator>(const String &)const;
        bool operator>=(const String &)const;
        bool operator<(const String &)const;
        bool operator<=(const String &)const;
        bool operator==(const char*)const;
        bool operator!=(const char*)const;
        bool operator>(const char*)const;
        bool operator>=(const char*)const;
        bool operator<(const char*)const;
        bool operator<=(const char*)const;
        charoperator[](int);//
        const charoperator[](int)const;
        int size(){return _used;}
        char* c_str(){
            Reserve(_used+1);
            _string[_used+1]='\0';
            return _string;
        }
    private:
        void Reserve(size_t n);
        char* _string;
        int _used;
        int _len;
};
void String::Reserve(size_t n){
    if(_len<n){
        size_t newlen=max(_len*1.5,n);
        char* buf=new char[newlen];
        strncpy(buf,_string,_used);
        delete[] _string;
        _string=buf;
        _len=newlen;
    }
}
istream& operator>>(istream& is,String& str){
    const int limit_string_used=4096;
    char inBuf[limit_string_used];
    is>>setw(limit_string_used)>>inBuf;
    str=inBuf;
    return is;
}
ostream& operator<<(ostream& os,const String& str){
    for(int i=0;i<str._used;i++)
        os<<str._string[i];
    return os;
}
String::String(int n,char ch) {
    if(n<0)
        n=-n;
    _len=1.5*n;
    _string=new char[_len];
    _used=n;
    for(int i=0;i<_used;i++)
        _string[i]=ch;
}
String::String(const char* str) {
    if(!str){
        _used=0;
        _len=0;
        _string=NULL;
    }
    else{
        size_t n=strlen(str);
        _len=1.5*n;
        _string=new char[_len];
        _used=n;
        strncpy(_string,str,_used);
    }
}
String::String(const String& str) {
    _len=str._len;
    _used=str._used;
    if(_used==0)
        _string=0;
    else{
        _string=new char[_len];
        strncpy(_string,str._string,_used);
    }
}
String& String::operator=(const String& str) {
    if(this!=&str){
        Reserve(str._used);
        _used=str._used;
        strncpy(_string,str._string,_used);
    }
    return *this;
}
String& String::operator=(const char*str) {
    if(!str){
        _used=0;
    }
    else{
        size_t n=strlen(str);
        Reserve(n);
        _used=n;
        strncpy(_string,str,_used);
    }
    return *this;
}
String& String::operator+=(const String& str) {
    if(str._used==0)
        return *this;
    size_t n=str._used;
    Reserve(_used+n);
    strncpy(_string+_used,str._string,n);
    _used+=n;
    return *this;
}
String& String::operator+=(const char* str) {
    if(!str)
        return *this;
    size_t n=strlen(str);
    Reserve(_used+n);
    strncpy(_string+_used,str,n);
    _used+=n;
    return *this;
}
String& String::operator+=(const char& c) {
    Reserve(_used+1);
    _string[_used++]=c;
    return *this;
}
bool String::operator==(const String& str)const {
    if(_used==str._used)
        return strncmp(_string,str._string,_used)==0?true:false;
    else
        return false;
}
bool String::operator!=(const String& str)const{
    return !(*this==str);
}
bool String::operator>(const String& str)const{
    if(_used==str._used)
        return strncmp(_string,str._string,_used)>0?true:false;
    else{
        int n=min(_used,str._used);
        int cmp=strncmp(_string,str._string,n);
        if(cmp==0)
            return _used<str._used;
        else
            return cmp>0;
    }
}
bool String::operator>=(const String& str)const{
    return  *this==str||*this>str;
}
bool String::operator<(const String& str)const{
    return !(*this>str);
}
bool String::operator<=(const String& str)const{
    return *this==str||*this<str;
}
bool String::operator==(const char* str)const {
    size_t n=strlen(str);
    if(_used==n)
        return strncmp(_string,str,_used)==0?true:false;
    else
        return false;
}
bool String::operator!=(const char* str)const {
    return !(*this==str);
}
bool String::operator>(const char* str)const {
    size_t sz=strlen(str);
    if(_used==sz)
        return strncmp(_string,str,_used)>0?true:false;
    else{
        int n=min(_used,sz);
        int cmp=strncmp(_string,str,n);
        if(cmp==0)
            return _used<sz;
        else
            return cmp>0;
    }
}
bool String::operator>=(const char* str)const {
    return *this==str||*this>str;
}
bool String::operator<(const char* str)const {
    return !(*this>str);
}
bool String::operator<=(const char* str)const {
    return *this==str||*this<str;
}
char& String::operator[](int n){
    if(n>=0&&n<_used)
        return _string[n];
}
const char& String::operator[](int n)const {
    if(n>=0&&n<_used)
        return _string[n];
}
String operator+(const String&str1,const String& str2) {
    if(str1._used==0&&str2._used==0)
        return String();
    else if(str1._used==0&&str2._used!=0){
        return String(str2);
    }
    else if(str1._used!=0&&str2._used==0){
        return String(str1);
    }
    else{
        String newstr;
        newstr._used=str1._used+str2._used;
        newstr._len=newstr._used;
        newstr._string=new char[newstr._len];
        strncpy(newstr._string,str1._string,str1._used);
        strncpy(newstr._string+str1._used,str2._string,str2._used);
        return newstr;
    }
}
String operator+(const String&str1,const char*str2) {
    size_t len=strlen(str2);
    if(str1._used==0&&len==0)
        return String();
    else if(str1._used==0&&len!=0){
        return String(str2);
    }
    else if(str1._used!=0&&len==0){
        return String(str1);
    }
    else{
        String newstr;
        newstr._used=str1._used+len;
        newstr._len=newstr._used;
        newstr._string=new char[newstr._len];
        strncpy(newstr._string,str1._string,str1._used);
        strncpy(newstr._string+str1._used,str2,len);
        return newstr;
    }
}
int main()
{
    int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,
        theCnt = 0, itCnt = 0, wdCnt = 0, notVowel = 0;
    // 为了使用 operator==( const char* )
    
// 我们并不定义 The( "The" )和 It( "It" )
    String buf, the( "the" ), it( "it" );
    // 调用 operator>>( istream&, String& )
    while ( cin >> buf ) {
        ++wdCnt;
        // 调用 operator<<( ostream&, const String& )
        cout << buf << ' ';
        if ( wdCnt % 12 == 0 )
            cout << endl;
        // 调用 String::operator==(const String&) and
        
//
        if ( buf == the || buf == "The" )
            ++theCnt;
        if ( buf == it || buf == "It" )
                ++itCnt;
        // 调用 String::size()
        for ( int ix = 0; ix < buf.size(); ++ix )
        {
            // 调用 String::operator[](int)
            switch( buf[ ix ] )
            {
                case 'a'case 'A': ++aCnt;
                                    break;
                case 'e'case 'E': ++eCnt;
                                    break;
                case 'i'case 'I': ++iCnt;
                                    break;
                case 'o'case 'O': ++oCnt;
                                    break;
                case 'u'case 'U': ++uCnt;
                                    break;
                default: ++notVowel; break;
            }
        }
    }
    // 调用 operator<<( ostream&, const String& )
    cout << "\n\n"
        <<"Words read: " << wdCnt << "\n\n"
        <<"the/The: " << theCnt << '\n'
        <<"it/It: " << itCnt << "\n\n"
        <<"non-vowels read: " << notVowel << "\n\n"
        <<"a: " << aCnt << '\n'
        <<"e: " << eCnt << '\n'
        <<"i: " << iCnt << '\n'
        <<"o: " << oCnt << '\n'
        <<"u: " << uCnt << endl;

    String a="hello";
    String b="world";
    String c=a+b;
    cout<<c<<endl;

    return 0;

}

 

posted @ 2012-08-24 10:41  Mr.Rico  阅读(373)  评论(0编辑  收藏  举报