- 注册时间
- 2005-2-11
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2007-12-2 21:50:47
|
显示全部楼层
- //===============================string.cpp===============================//
- #include <string.h>
- #include "string.h"
- string::string(const char *p)
- {
- ptr=new char[strlen(p)+1];
- strcpy(ptr,p);
- }
- string::string(string &p)
- {
- ptr=new char[strlen(p.ptr)+1];
- strcpy(ptr,p.ptr);
- }
- string::~string()
- {
- if(ptr)delete ptr;
- }
- string &string::operator=(const char *p)
- {
- if(ptr)
- {
- delete ptr;
- ptr=0;
- }
- if(p)
- {
- ptr=new char[strlen(p)+1];
- strcpy(ptr,p);
- }
- return *this;
- }
- string &string::operator=(string &p)
- {
- if(ptr)
- {
- delete ptr;
- ptr=0;
- }
- if(p.ptr)
- {
- ptr=new char[strlen(p.ptr)+1];
- strcpy(ptr,p.ptr);
- }
- return *this;
- }
- bool string::operator==(const char *p)
- {
- if(p&&ptr)
- {
- if(!strcmp(ptr,p))return true;
- }
- else if(!(p||ptr))return true;
- return false;
- }
- bool string::operator==(string &p)
- {
- if(p.ptr&&ptr)
- {
- if(!strcmp(ptr,p.ptr))return true;
- }
- else if(!(p.ptr||ptr))return true;
- return false;
- }
- bool string::operator!=(const char *p)
- {
- if(p&&ptr)
- {
- if(!strcmp(ptr,p))return false;
- }
- else if(!(p||ptr))return false;
- return true;
- }
- bool string::operator!=(string &p)
- {
- if(p.ptr&&ptr)
- {
- if(!strcmp(ptr,p.ptr))return false;
- }
- else if(!(p.ptr||ptr))return false;
- return true;
- }
- bool string::operator!()
- {
- if(ptr)
- {
- if(*ptr)return false;
- }
- return true;
- }
- char string::operator[](unsigned long i)
- {
- return ptr[i];
- }
- unsigned long string::size()
- {
- return strlen(ptr);
- }
- void string::subString(string &p,int start,int finish)
- {
- if(ptr)
- {
- delete ptr;
- ptr=0;
- }
- ptr=new char[finish-start+1];
- memcpy(ptr,&p.ptr[start],finish-start);
- ptr[finish-start]=0;
- }
- char *string::getPointer()
- {
- return ptr;
- }
复制代码
[ 本帖最后由 yan 于 2007-12-2 21:53 编辑 ] |
|