Site hosted by Angelfire.com: Build your free website today!
         outline of assignment

#include <CString>

#include <fstream.h>      

#include <cassert>

#include <ctype.h>

 

//The follow is to fix  errors for the lecture note

/*

String::String(const char *s)

{

            if ( !s ) {

                        len = 0;

                        str = 0;

            }

            else {

                        len = strlen( s );

                        str = new char[ len + 1];

                        strcpy( str, s );

            }

}

 

String::String(const String  & S)

{

            if ( !S.length() ) {

                        len = 0;

                        str = 0;

            }

            else {

                        len = S.length();

                        str = new char[ len + 1];

                        strcpy( str, S.str );

            }

}

 

 

String &

String::operator=( const char *s)

{

            if  ( ! s) {

                        len = 0;

        delete [] str;

        str = 0;

    }

    else {

        len = strlen( s );

        delete [] str;

        str = new char[ len + 1 ];

        strcpy( str, s );

    }

    return *this;

}

 

String &

String::operator=( const String &s)

{

    if  (  this != &s ) {

                        delete [] str;

        len = s.len;

        if (  s.str ) {

                                    str = new char [ len + 1 ];

            strcpy( str, s.str );

                        }

        else

            str = 0;

            }

    return *this;

}

 

*/

 

class Hm_Md_String

{

  public:

              /* this is for the case like 'Str3 = "This is a string" + Str2;'*/

            friend Hm_Md_String operator + ( const Hm_Md_String & , const Hm_Md_String &  );

             /* this is for the case like 'Str3 = "This is a string" - Str2;'*/

            friend Hm_Md_String operator - ( const Hm_Md_String & , const Hm_Md_String &  );

 

            //constructor/destructor

    Hm_Md_String( );                         // construct empty string ""

    Hm_Md_String( const char * s );          // construct from string literal

    Hm_Md_String( const Hm_Md_String & str );    // copy constructor

    ~Hm_Md_String( );                       // destructor

 

            // assignment

            Hm_Md_String & operator = ( const char *s );            

            Hm_Md_String & operator = ( const Hm_Md_String & str );

   

 

            /* append the string passed in to the end of the current string */

            Hm_Md_String & operator + ( const char *s );            

            Hm_Md_String & operator + ( const Hm_Md_String & );

            Hm_Md_String & operator += ( const char *s );            

            Hm_Md_String & operator += ( const Hm_Md_String & str );

 

           

            /* if the string passed in matches the very beginning of current string, remove them */

            Hm_Md_String  & operator - ( const char * );

            Hm_Md_String  & operator - ( const Hm_Md_String  & );

            Hm_Md_String  & operator -= (const char *s );

            Hm_Md_String  & operator -= (const Hm_Md_String  & S);

 

            int length( ) const;  // number of chars

   

//            Hm_Md_String & concat( Hm_Md_String  str);

            Hm_Md_String & concat( const char * );

           

            char * c_str( ) const;

 

  private:

     int myLength;// length of string

             char *myStr;

             int myCapacity;  //storage for character

       

};

   

 

Hm_Md_String::Hm_Md_String()

{

   myLength = 0;

   myCapacity = 1;

   myStr = new char[myCapacity];

   myStr[0] = '\0';

}

 

Hm_Md_String::Hm_Md_String(const char * s)

{

    assert (s != 0);               // C-string not NULL?

    if(s != "null")

            {

      myLength = strlen(s);

      myCapacity = myLength + 1;      // make room for '\0'

      myStr = new char[myCapacity];

      strcpy(myStr,s);

            }

            else// null c_string case

            {

               myLength = 0;

       myCapacity = 1;

       myStr = new char[myCapacity];

       myStr[0] = '\0';

            }

}

//copy constructor that copy of str has been constructed

Hm_Md_String::Hm_Md_String(const Hm_Md_String & str)

{

   myLength = str.length();

   myCapacity = myLength+1;//add for '\0'

   myStr = new char[myCapacity];

   strcpy(myStr, str.myStr);

}

 

/* destructor */

Hm_Md_String::~Hm_Md_String()

{

  delete[] myStr;//clean myStr

}

 

 

/* = operator, the current string will equal to the value passed in */

Hm_Md_String & Hm_Md_String::operator = ( const char *s )

{

  int len=0;

  assert(s!=0);

  len = strlen(s);

  if(myCapacity<len+1)

  {

            delete[] myStr;//clear myStr

            myCapacity = len+1;

            myStr = new char[myCapacity];

  }

  myLength= len;

  strcpy(myStr, s);

  return *this;

}

 

Hm_Md_String & Hm_Md_String::operator = ( const Hm_Md_String & str ) // assign str

{

   if(this != &str)

   {

             if(myCapacity < str.length()+1)

             {

               delete [] myStr;

               myCapacity = str.length()+1;

               myStr= new char [myCapacity];

             }

     myLength = str.length();

             strcpy(myStr, str.myStr);

   }

   return *this;

}

 

 

 

 /* returns # of chars in string */

int Hm_Md_String ::length() const

{

  return myLength;

}

 

 /* returns # of chars in string */

 char * Hm_Md_String::c_str() const

{

  return myStr;

}

 

/* append the value passed in to the end of the current string */

Hm_Md_String & Hm_Md_String::concat(const char *str)

{

            char *temp;

    temp = 0;

            assert(str!=0);

    if (*str != 0) {

                        temp = new char[myLength];

                        strcpy(temp, myStr);

                        delete[] myStr;

 

                        myLength = myLength + strlen(str);

                        myCapacity = myCapacity + strlen(str);

 

                        myStr = new char[myCapacity];

                        strcpy(myStr, temp);

                        strcat(myStr, str);

            }

            return *this;

}

 

 

//+ operator, append the str to the end of the current string

Hm_Md_String & Hm_Md_String::operator + ( const char *str )

{

 

            char *temp;

    temp = 0;

            assert(str!=0);

    if (*str != 0) {

                        temp = new char[myLength];

                        strcpy(temp, myStr);

                        delete[] myStr;

 

                        myLength = myLength + strlen(str);

                        myCapacity = myCapacity + strlen(str);

 

                        myStr = new char[myCapacity];

                        strcpy(myStr, temp);

                        strcat(myStr, str);

            }

 

            return *this;

}

 

Hm_Md_String  & Hm_Md_String::operator + ( const Hm_Md_String  & str)

{

  return *this + str.myStr;

}

 

//cope of s into this string

Hm_Md_String & Hm_Md_String::operator +=(const char *s)

{

  return *this + s;

}

 

 

Hm_Md_String & Hm_Md_String::operator +=(const Hm_Md_String & str)

{

  return *this + str.myStr;

}

 

Hm_Md_String operator + (const Hm_Md_String & str1, const Hm_Md_String & str2)

{

  Hm_Md_String result(str1);

  result +=str2;

  return result;

}

 

// - operator,

Hm_Md_String & Hm_Md_String::operator - (const char *s)

{

    char *temp;

 

            temp = new char[myCapacity];

 

            strcpy(temp, myStr);

 

            for(; *s != 0; s++)

            {

            /* find the s from the this string

               remove them if the whole string or first partial exactly matches the very beginning

               of the current string

            */

              if((tolower(*temp) == *s) || (toupper(*temp) == *s))

                        {            myCapacity --;

                                    myLength --;

                                    temp++;

                        }

                        else {

                                    break;

                        }

            }

           

            delete[] myStr;

            myStr = new char[myCapacity];

            strcpy(myStr, temp);

 

            return *this;

}

 

Hm_Md_String  & Hm_Md_String::operator -( const Hm_Md_String  & str)

{

  return *this - str.myStr;

}

Hm_Md_String  & Hm_Md_String::operator -= (const char *s )

{

   return *this - s;

}

Hm_Md_String  & Hm_Md_String::operator -= (const Hm_Md_String  & str)

{

   return *this - str.myStr;

}

 

Hm_Md_String operator - (const Hm_Md_String & str1, const Hm_Md_String & str2)

{

  Hm_Md_String result(str1);

  result -=str2;

  return result;

}

 

 // str is written to output stream os

ostream& operator <<(ostream & os, const Hm_Md_String& str)

{

    return os << str.c_str();

}

 

/* the stuff in main is used for testing my program */

int main()

{ 

            ofstream outFile;                        // declare variable of type ofstream

            outFile.open("out.txt", ios::out);

 

            if ( outFile.fail() )  {     // file open failed

        outFile<< "ERROR in opening outFile." << endl;

                        return 0;

            }

 

   outFile<<"-----testing 3 constructors------"<<endl;

  

   Hm_Md_String s1;                    //testing default constructor

  

   Hm_Md_String s2("testing");            //testing constructor with the const char

 

   Hm_Md_String s3(s2);              //testing constructor with the reference

 

   outFile<< "s1.length()=" << s1.length() << " s1=" << s1 <<endl;

   outFile<< "s2.length()=" << s2.length() << " s2=" << s2 <<endl;

   outFile<< "s3.length()=" << s3.length() << " s3=" << s3 <<endl << endl;

  

   outFile<<"-----testing = operator------"<<endl;

 

   s1 = "This is a string";  //testing = operator

   s2=s3=s1;

   outFile<< s2 << endl;

   outFile<< s3 << endl << endl;

 

 

   outFile<<"-----testing all + operator------"<<endl;

 

   s1 ="This ";

   outFile << s1 << endl;

   s1 = s1 + "is ";                                  //testing s1 = s1 + "is "; 

   outFile << s1 << endl;

   s2 = "a ";

   s2 = s1 + s2;                         //testing s2 = s1 + s2;

   outFile << s2 << endl;

   s3 = "string";

   s2 += s3;                                           //testing s2 += s3;

   outFile << s2 << endl;

   s2 += ", it works fine!";            //testing s2 += ", it works fine!";

   outFile << s2 << endl << endl;

   s2 = "Testing + operator: " + s2;        //testing s2 = "Testing + operator: " + s2;

   outFile << s2 << endl;

 

 

   outFile<<"-----testing all - operator------"<<endl;

  

   s2 = "This is a string, it works fine!";

   outFile << s2 << endl;

   s2 = s2 - "This ";             //testing s2 = s2 = s2 - "This ";

   outFile << s2 << endl;

   s2 -= "is";                                          //testing s2 -= "is";

   outFile << s2 << endl;

   s1 = " a";

   s2 = s2 - s1;                          //testing s2 = s2 - s1;

   s3 = " string, ";

   s2 -= s3;                                           //testing s2 -= s3;

   outFile << s2 << endl;

   s1 = "it works fineTesting - operators successful!" - s2;

   outFile << s1 << endl;                       //testing s1 = "it works fineTesting - operators successful!" - s2;

 

   cout << "     Thanks using the program, and " << endl;

   cout << "  output is contained in out.txt in current folder. bye!" << endl;

   return 0;

}