作业
# include <iostream>
# include "mystring.h" using namespace std; int main ( )
{ mystring stra ( "Hello" ) ; mystring strb; cin >> strb; cout << strb << endl; strb += stra; cout << strb << endl; strb = strb + stra; cout << strb << endl; cin >> strb; cout << strb << endl; if ( stra == strb) { cout << "=" << endl; } else if ( stra > strb) { cout << ">" << endl; } else if ( stra < strb) { cout << "<" << endl; } return 0 ;
}
# ifndef MYSTRING_H
# define MYSTRING_H # include <cstring>
# include <iostream> using namespace std; class mystring
{
private : char * str; int size; int len;
public : mystring ( ) ; mystring ( const char * s) ; mystring ( int n, char ch) ; mystring ( mystring & other) ; char & at ( int i) ; bool is_empty ( ) ; void full ( ) ; char * get_c ( ) ; int get_size ( ) ; int get_len ( ) ; void show ( ) ; mystring & operator += ( const mystring & other) ; mystring & operator = ( const mystring & other) ; char & operator [ ] ( int i) ; mystring operator + ( const mystring & other) const ; bool operator == ( const mystring & other) const ; bool operator != ( const mystring & other) const ; bool operator < ( const mystring & other) const ; bool operator > ( const mystring & other) const ; bool operator >= ( const mystring & other) const ; bool operator <= ( const mystring & other) const ; friend ostream & operator << ( ostream & L, const mystring & R) ; friend istream & operator >> ( istream & L, const mystring & R) ; } ; # endif
# include "mystring.h"
mystring:: mystring ( ) : str ( new char [ 10 ] ) , size ( 10 ) , len ( 0 )
{ }
mystring:: mystring ( const char * s) : str ( new char [ strlen ( s) ] ) , size ( strlen ( s) ) , len ( size)
{ strcpy ( str, s) ;
} mystring:: mystring ( int n, char ch) : str ( new char [ n+ 1 ] ) , size ( n+ 1 ) , len ( size)
{ for ( int i= 0 ; i < n; i++ ) { str[ i] = ch; } str[ n+ 1 ] = 0 ;
} mystring:: mystring ( mystring & other) : str ( new char [ other. get_len ( ) ] ) , size ( other. get_len ( ) ) , len ( size)
{ strcpy ( str, other. str) ;
} char & mystring:: at ( int i)
{ if ( i>= 1 && i < len) return str[ i- 1 ] ; else return str[ - 1 ] ;
} bool mystring:: is_empty ( )
{ return len;
} void mystring:: full ( )
{ if ( len > size) { char * temp = new char [ 2 * size] ; memmove ( temp, str, len) ; delete [ ] str; str = temp; size = size* 2 ; temp = NULL ; } } char * mystring:: get_c ( )
{ return this -> str;
} int mystring:: get_size ( )
{ return size;
} int mystring:: get_len ( )
{ return len;
} void mystring:: show ( )
{ cout << str << endl;
} mystring & mystring:: operator += ( const mystring & other)
{ len += other. len; full ( ) ; strcat ( str, other. str) ; return * this ;
} mystring & mystring:: operator = ( const mystring & other)
{ size = other. size; len = other. len; for ( int i = 0 ; i < len; i++ ) { str[ i] = other. str[ i] ; } return * this ;
} char & mystring:: operator [ ] ( int i)
{ return str[ i- 1 ] ;
} mystring mystring:: operator + ( const mystring & other) const
{ mystring temp; temp. str = new char [ this -> len + other. len] ; temp. size = this -> len + other. len; temp. len = this -> len + other. len; strcat ( temp. str, str) ; strcat ( temp. str, other. str) ; return temp;
} bool mystring:: operator == ( const mystring & other) const
{ bool flag = true ; if ( len != other. len || strcmp ( str, other. str) ) { flag = false ; } return flag;
} bool mystring:: operator != ( const mystring & other) const
{ bool flag = true ; if ( len == other. len && strcmp ( str, other. str) ) { flag = false ; } return flag;
} bool mystring:: operator < ( const mystring & other) const
{ bool flag = true ; if ( len >= other. len) { flag = false ; } return flag;
} bool mystring:: operator > ( const mystring & other) const
{ bool flag = true ; if ( len <= other. len) { flag = false ; } return flag;
} bool mystring:: operator >= ( const mystring & other) const
{ bool flag = true ; if ( len < other. len) { flag = false ; } return flag; } bool mystring:: operator <= ( const mystring & other) const
{ bool flag = true ; if ( len > other. len) { flag = false ; } return flag;
} istream & operator >> ( istream & L, const mystring & R)
{ L >> R. str; return L;
} ostream & operator << ( ostream & L, const mystring & R)
{ L << R. str; return L;
}