posts - 6,comments - 2,trackbacks - 0

//string.h
#ifndef STRING_H_
#define STRING_H_
#include <iostream>
#include <cstring>
#include <cctype>
#include <cassert>
#include <cstddef>
#include <string>
#include "auto_pointer.h"

class String
{
private:
 size_t  _size;
 char *_string;
 void init (size_t , const char *);
public:
 String () { init (0, NULL); }
 String (const char * str) { init (strlen(str), str); }
 String (const String & str) { init (str.size (), str.c_str());}
 ~String () { delete [] _string; }
 //member methods
 int count (char ch)const;
 size_t size ()const { return _size; }
 char * c_str ()const {return _string ;}
 const char * to_upper ()const;
 //assign operator
 String & operator = (const char * str);
 String & operator = (const String & str);
 //overload operators
 bool operator == (const char *str)const {return strcmp (_string, str) == 0; }
 bool operator == (const String &str)const {return strcmp (_string, str.c_str ()) == 0; }
 bool operator != (const String & str)const { return strcmp (_string, str.c_str ()) != 0; }
 bool operator != (const char *str)const {return strcmp (_string, str) != 0; }
 bool operator < (const char *str)const {return strcmp (_string, str) < 0; }
 bool operator < (const String & str)const {return strcmp (_string, str.c_str ()) < 0; }
 bool operator > (const char *str)const {return strcmp (_string, str) > 0; }
 bool operator > (const String & str)const {return strcmp (_string, str.c_str ()) > 0; }
 String &  operator += (const char *str);
 String  & operator += (const String & str);
 char & operator [] (size_t i) { assert(i >= 0 && i < _size); return _string[i]; }
 //friend methods
 friend std::ostream & operator << (std::ostream & os, const String & str);
 friend std::istream & operator >> (std::istream &is, String & str);
 friend bool operator == (const char *cstr, const String & sstr) {return sstr.operator == (cstr); }
 friend bool operator != (const char *cstr, const String & sstr) {return sstr.operator != (cstr); }
 friend bool operator < (const char *cstr, const String & sstr) {return sstr.operator <(cstr); }
 friend bool operator > (const char *cstr, const String & sstr) {return sstr.operator >(cstr); }
};

#endif
//string.cpp
#include "String.h"
#include <cstring>
#include <iomanip>
#pragma warning(disable : 4996)

void String::init(size_t size, const char *str)
{
 _size = size;
 if (!str)
 {
  _string = new char [1];
  _string[0]  = '\0';
 }
 _string = new char[_size + 1];
 strncpy (_string, str, _size);
 _string[_size ] = '\0';
 //strcpy (_string, str);
}

String & String::operator = (const char *str)
{
 delete [] _string;
 init (strlen (str), str);std::cout << this->_string << "_";
 return *this;
}

String & String::operator = (const String &str)
{
 if (*this == str)
  return *this;
 delete [] _string;
 init (str.size (), str.c_str ());
 return *this;
}

String  & String::operator +=(const char *str)
{
 String temp(*this);
 _size += strlen (str);
 _string = new char[_size + 1];
 strcpy (_string, temp.c_str ());
 //strcat (_string, str);
 strcpy (_string+temp.size (), str);
 return *this;
}

String & String::operator +=(const String &str)
{
 String temp(*this);
 _size +=  str.size ();
 _string = new char[_size + 1];
 strcpy (_string, temp.c_str ());
 //strcat (_string , str.c_str ());
 strcpy (_string+temp.size (), str.c_str ());
 return *this;
}

int String::count (char ch)const
{
 int cn = 0;
 for (size_t i = 0; i < _size; i++)
  if (ch == tolower(_string[i]))
   cn++;
 return cn;
}

const char * String::to_upper ()const
{
 size_t size = strlen (_string);
 char *temp = new char[size + 1]; 
 size_t j = 0;
 for (size_t i = 0; i < size; i++)
  if (isalpha(_string[i]) || _string[i] == ' ')
   if (isalpha (_string[i]))
    temp[j++] = toupper (_string[i]);
   else
    temp[j++] = _string[i];
 temp[j] = '\0';
 return temp;
}
std::ostream & operator << (std::ostream & os, const String & str)
{
 return os << str.c_str ();
}

std::istream & operator >> (std::istream & is, String & str)
{
 const int limit_string_size = 50;
 char Inbuf [limit_string_size];

 //is.read (Inbuf, limit_string_size);
 //is >> setw (limit_string_size) >> Inbuf;
 is.getline (Inbuf, limit_string_size-1, '\n');
 /*if (is)
 {
  while (is.get () == '\n')
   break;
  Inbuf[limit_string_size-1] = '\0';
 }*/
 //is >> Inbuf;
 str = Inbuf;
 return is;
}

posted on 2006-04-11 20:35 Jonathan 阅读(391) 评论(0)  编辑 收藏 引用

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