稍微對String類的一點改進

 
#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);
String operator + (const String &sorig,const String sdest);
class String
{
	public:
	String():pc(NULL){}
	String(const char *p);
	String(const String &orig);
	String& operator = (const String &orig);
	~String();
	friend ostream& operator << (ostream &os,const String &str);
	friend istream& operator >> (istream &in,String &str);
	friend String operator + (const String &sorig,
				const String sdest);
	char operator [] (size_t index)const;
	bool operator == (const String &orig);
	bool operator != (const String &orig);
	const String& insert(const String &orig,size_t index);
	const String& erase(size_t headindex,size_t backindex);
	bool empty();
	size_t size()const;
	private:
	ostream& print(ostream &os)const;
	istream& putin(istream &in);
	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)const
{
	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;
}
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;
}
size_t String::size()const
{
	return strlen(pc);
}
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);
}
String operator + (const String &sorig,const String sdest)
{
	size_t Lengthorig = sorig.size();
	size_t Lengthdest = sdest.size();
	char *pt = new char[Lengthorig+Lengthdest+1];
	char * porig = sorig.pc;
	char * pdest = sdest.pc;
	memcpy(pt,porig,Lengthorig);
	pt[Lengthorig] = '\0';
	strcat(pt,pdest);
	pt[Lengthorig+Lengthdest] = '\0';
	String Strtemp(pt);
	cout << Strtemp << endl;
	delete [] pt;
	return Strtemp;
}

#include "String.h"
#include <iostream>
using namespace std;
/**切忌不要返回臨時變量的引用**/
int main()
{
	//功能1:指針構造
	char *p = "nihaoya!";
	String sss = p;
	cout << sss << endl;
	//功能2:複製構造
	String sss1(sss);
	cout << sss1 << endl;
	//功能3:賦值
	String sss2;
	sss2 = sss1;
	cout << sss2 << endl;
	//連續賦值
	String sss3;
	String sss4;
	String sss5;
	sss5 = sss4 = sss3 = "you are good!";
	//功能4:下標操作
	char ch = sss5[2];
	cout << ch << endl;
	//功能5:加操作
	String sss6 = sss1+sss4;
	cout << sss6 << endl;
	//功能6:求長度
	int Length = sss6.size();
	cout << Length << endl;
	//功能7:判斷是否爲空
	cout << sss1.empty() << endl;
	//功能8:插入字符串
	sss4.insert("jjjj12dd",3);
	cout << sss4 << endl;
	//功能9:刪除字符串
	sss3.erase(4,5);
	cout << sss3 << endl;
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章