zoukankan      html  css  js  c++  java
  • 【C++】String类实现

    //string.h
    
    #include <iostream>
    
    using namespace std;
    
    class String;
    
    istream& operator>>( istream&, String& );
    ostream& operator<<( ostream&, String& );
    
    class String {
        public:
            //String str1;
            //String str2( "literal" );
            //String str3( str2 );
            String();
            String( const char* );
            String( const String& );
    
            ~String();
            
            bool operator==( const String& ) const;
            bool operator==( const char* ) const;
            bool operator!=( const String& ) const;
    
            String& operator=( const String& );
            String& operator=( const char* );
            char& operator[]( const int );
    
            int size() { return _size; }
            char* c_str() { return _string; }
    
        private:
            char *_string;
            int _size;
    };
    

      

    //string.cpp
    
    #include "string.h"
    
    // to include strcmp
    // in the C standard library
    #include <cstring>
    #include <iostream>
    #include <cassert>
    #include <iomanip>
    
    
    using namespace std;
    
    bool
    String::operator==
    ( const String &rhs ) const {
        if ( _size != rhs._size ) 
            return false;
        else
            return strcmp( _string, rhs._string) ? false : true;
    }
    
    bool
    String::operator==( const char *c ) const {
        return strcmp( _string, c ) ? false : true;
    }
    
    
    String::String() {
        _size = 0;
        _string = 0;
    }
    
    
    String::String( const String &rhs ) {
        _size = rhs._size;
        if ( !rhs._string ) {
            _string = 0; }
        else {
            _string = new char[_size + 1];
            strcpy( _string, rhs._string );
        }
    }
    
    
    String::String( const char *s ) {
        if ( !s ) {
            _size = 0; _string = 0; }
        else {
            _size = strlen( s );
            _string = new char[_size + 1];
            strcpy( _string, s );
        }
    }
    
    
    String::~String() {
        delete [] _string;
    }
    
    String&
    String::operator=
    ( const String &rhs ) {
        if ( this != &rhs ) {
            _size = rhs._size;
            delete [] _string;
            if ( !rhs._string )
                _string = 0;
            else {
                _string = new char[_size + 1];
                strcpy( _string, rhs._string );
            }
        }
        else
            return *this;
    }
    
    String&
    String::operator=
    ( const char *c ) {
        if ( !c ) {
            _size = 0; 
            delete [] _string;
            _string = 0;
        }
        else {
            //cout << "operator= invoked for char: " << *c << endl;
            _size = strlen( c );
            delete [] _string;
            _string = new char[_size + 1];
            strcpy( _string, c );
        }    
    }
    
    char&
    String::operator[](const int index) {
        assert( index >= 0 && index < _size);
        return _string[ index ];
    }
    
    istream&
    operator>>( istream& io, String &s) {
        const int limit_string_size =   4096;
        char inBuf[ limit_string_size ];
    
        io >> setw( limit_string_size ) >> inBuf; 
        s = inBuf; //String::operator=( const char* )
    
        return io;
    }
    
    ostream& 
    operator<<( ostream& os, String &s) {
        os << s.c_str();
    }
    

     

    //stringtest.cpp
    #include "string.h"
    #include <iostream>
    
    using namespace std;
    
    
    int main() 
    {
        int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,
            theCnt = 0, itCnt =0, wdCnt = 0, notVowel = 0;
        String buf, the( "the" ), it( "it" );
    
        // invoke operator>> 
        while ( cin >> buf ) {
            ++wdCnt;
    
            //invoke operator<< 
            cout << buf << ' '; 
            if ( wdCnt%12 == 0 ) 
                cout << endl;
    
            if ( buf == the || buf == "The" ) 
                ++theCnt;
            else
                if ( buf == it || buf == "It" )
                    ++itCnt;
            for ( int ix = 0; ix < buf.size(); ++ix )
            {
                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;
                }
            }
        }
    
        cout << "
    
    "
            << "Words read: " << wdCnt << "
    
    "
            << "the/The: "    << theCnt << '
    '
            << "it/It: "      << itCnt << "
    
    "
            << "non-vowel read: " << notVowel << "
    
    "
            << "a: " << aCnt << '
    '
            << "e: " << eCnt << '
    '
            << "i: " << iCnt << '
    '
            << "o: " << oCnt << '
    '
            << "u: " << uCnt << endl;
    
    }
    

      

  • 相关阅读:
    [再寄小读者之数学篇](2014-10-27 无穷多个无穷小量相乘还是无穷小量么?)
    华中师范大学2012年数学分析考研试题参考解答
    本科时的课程与成绩
    2014 年第六届全国大学生数学竞赛预赛数学类试题参考答案
    [家里蹲大学数学杂志]第322期赣南师范学院数学竞赛培训第11套模拟试卷
    [再寄小读者之数学篇](2014-10-18 利用 Lagrange 中值定理求极限)
    和马有关的成语
    PL/pgSQL学习笔记之一
    PostgreSQL的 initdb 源代码分析之二十五
    PostgreSQL的 initdb 源代码分析之二十四
  • 原文地址:https://www.cnblogs.com/dracohan/p/3145519.html
Copyright © 2011-2022 走看看