#ifndef _STRING_H_H
#define _STRING_H_H
#include <iostream>
using namespace std;
class String;
ostream& operator << (ostream &os,const String &str);
istream& operator >> (istream &in,String &str);
class String
{
public:
String():pc(NULL){}
String(const char *p);
String(const String &orig);
String& operator = (const String &orig);
String& operator + (const String &orig);
~String();
friend ostream& operator << (ostream &os,const String &str);
friend istream& operator >> (istream &in,String &str);
char operator [] (size_t index);
bool operator == (const String &orig);
bool operator != (const String &orig);
ostream& print(ostream &os)const;
istream& putin(istream &in);
const String& insert(const String &orig,size_t index);
const String& erase(size_t headindex,size_t backindex);
bool empty();
private:
void split(char *pt,char*ptback,
size_t headindex,size_t backindex = 0);
void del_ptr()
{
if(pc != NULL)
{
delete [] pc;
pc = NULL;
}
}
char *pc;
};
#endif
#include "String.h"
#include <string.h>
#include <assert.h>
using namespace std;
/**member function implementations**/
String::String(const char *p)
{
size_t Length = strlen(p);
pc = new char [Length+1];
memcpy(pc,p,Length);
pc[Length] = '\0';
}
String::String(const String &orig)
{
size_t Length = strlen(orig.pc);
pc = new char [Length+1];
memcpy(pc,orig.pc,Length);
pc[Length] = '\0';
}
String& String::operator = (const String &orig)
{
del_ptr();
size_t Length = strlen(orig.pc);
pc = new char [Length+1];
memcpy(pc,orig.pc,Length);
pc[Length] = '\0';
return *this;
}
String::~String()
{
del_ptr();
}
char String::operator [] (size_t index)
{
return pc[index];
}
bool String::operator == (const String & orig)
{
int temp = strcmp(pc,orig.pc);
if(0 == temp)
return true;
else
return false;
}
ostream& String::print(ostream &os)const
{
os << pc;
return os;
}
istream& String::putin(istream &in)
{
char *pt = new char [10000];
in >> pt;
size_t Length = strlen(pt);
pt[Length] = '\0';
pc = new char[Length+1];
memcpy(pc,pt,Length);
pc[Length] = '\0';
delete [] pt;
return in;
}
String& String::operator + (const String &orig)
{
if(NULL == pc)
{
size_t Length = strlen(orig.pc);
pc = new char [Length+1];
memcpy(pc,orig.pc,Length);
pc[Length] = '\0';
}
else
{
size_t Lengthdest = strlen(pc);
size_t Lengthorig = strlen(orig.pc);
char *pt = new char[Lengthdest+Lengthorig+1];
memcpy(pt,pc,Lengthdest);
pt[Lengthdest] = '\0';
strcat(pt,orig.pc);
pt[Lengthdest+Lengthorig] = '\0';
delete [] pc;
pc = pt;
}
return *this;
}
bool String::operator != (const String &orig)
{
if(operator == (orig))
return false;
else
return true;
}
const String& String::insert(const String &orig,size_t index)
{
size_t Lengthdest = strlen(pc);
assert(Lengthdest >= index);
size_t Lengthorig = strlen(orig.pc);
char *pt = new char[Lengthdest+Lengthorig+1];
char *pdesthead = new char[index+1];
char *pdestback = new char[Lengthdest-index+1];
split(pdesthead,pdestback,index);
delete [] pc;
memcpy(pt,pdesthead,index+1);
pt[index] = '\0';
strcat(pt,orig.pc);
pt[index+Lengthorig] = '\0';
strcat(pt,pdestback);
pt[Lengthdest+Lengthorig] = '\0';
pc = pt;
delete [] pdesthead;
delete [] pdestback;
return *this;
}
const String& String::erase(size_t headindex,size_t backindex)
{
size_t Lengthdest = strlen(pc);
assert(Lengthdest >= backindex);
size_t Lengtherase = backindex - headindex;
char *pt = new char[Lengthdest-Lengtherase+1];
char *pdesthead = new char[headindex+1];
char *pdestback = new char[Lengthdest-backindex+1];
split(pdesthead,pdestback,headindex,backindex);
delete [] pc;
memcpy(pt,pdesthead,headindex+1);
pt[headindex] = '\0';
strcat(pt,pdestback);
pt[Lengthdest-Lengtherase] = '\0';
delete [] pdesthead;
delete [] pdestback;
pc = pt;
return *this;
}
bool String::empty()
{
if(NULL == pc)
return true;
else
return false;
}
void String::split(char *pthead,char *ptback,
size_t headindex,size_t backindex)
{
size_t Length = strlen(pc);
if(0 == backindex)//split for insert
{
for(int i = 0;i != headindex;i++)
pthead[i] = pc[i];
pc = pc + headindex;
pthead[headindex] = '\0';
for(int j = 0;j != Length-headindex;j++)
ptback[j] = pc[j];
ptback[Length-headindex] = '\0';
pc = pc - headindex;
}
else//split for earse
{
for(int i = 0;i != headindex;i++)
pthead[i] = pc[i];
pc = pc + backindex;
pthead[headindex] = '\0';
for(int j = 0;j != Length-backindex;j++)
ptback[j] = pc[j];
ptback[Length-headindex] = '\0';
pc = pc - backindex;
}
}
/**friend function**/
ostream& operator << (ostream &os,const String &str)
{
return str.print(os);
}
istream& operator >> (istream &in,String &str)
{
return str.putin(in);
}
无聊练练手,新手可以看看,高手就请笑过