游戏开发中,免不了有对char*的操作,以及对List的使用。如果不封装关于此方面的类的话,在使用的时候,将十分的不方便,比如对char*的连接,赋值等等操作。

这里是CEasyStr和CEasyList类的具体代码:

#include "EasyList.h"

CEasyNode::CEasyNode(void)

{

pre=NULL;

data=NULL;

next=NULL;

}

CEasyNode::~CEasyNode(void)

{

 

}

 

 

CEasyList::CEasyList(void)

{

mHead=new CEasyNode;

mLast=new CEasyNode;

 

 

mHead->next=mLast;

 

mLast->pre=mHead;

 

mCount=0;

 

}

 

CEasyList::~CEasyList(void)

{

}

 

int CEasyList::Add(void *p)

{

CEasyNode *bb= new CEasyNode;

 

bb->data=p;

 

bb->pre=mLast->pre;

bb->next=mLast;

mLast->pre->next=bb;

mLast->pre=bb;

//mLast->pre->next=bb;

 

return ++mCount;

}

int CEasyList::Count()

{

return mCount;

}

CEasyNode*    CEasyList::GetNode(int index)

{

if(index>=mCount || index<0)

    {

return NULL;

    }

 

CEasyNode* pNode;

pNode=mHead;

for(int i=0;i<=index;i++)

    {

pNode=pNode->next;

if(pNode==NULL || pNode==mLast)

        {

return NULL;

        }

 

    }

return pNode;

}

void* CEasyList::Get(int index)

{

CEasyNode *pNode=GetNode(index);

 

if(pNode)

    {

return pNode->data;

else

    {

return NULL;

    }

}

int CEasyList::Delete(int index)

{

CEasyNode* pNode;

pNode=GetNode(index);

 

if(pNode)

    {

pNode->pre->next=pNode->next;

 

pNode->next->pre=pNode->pre;

 

mCount--;

 

delete pNode;

pNode=NULL;

    }

 

return mCount;

 

 

 

}

void    CEasyList::Clear()

{

while(Delete(0));

}

////////////////////////////////////////////////////////////////////////////////////////////////

#include "EasyStr.h"

 

CEasyStr::CEasyStr(void)

{

mRef=0;

mData=new char[1];

strcpy(mData,"");

//mData="";

}

 

CEasyStr::~CEasyStr(void)

{

if(--mRef<0)

    {

delete []mData;

mData=NULL;

    }

}

 

 

CEasyStr CEasyStr::GetSelf()

{

AddRef();

return *this;

}

void CEasyStr::AddRef()

{

mRef++;

}

int CEasyStr::GetLen()

{

return strlen(mData);

}

void CEasyStr::operator =(const char *Str)

{

if(mData)

    {

delete []mData;

mData=NULL;

    }

int iLength=strlen(Str);

mData=new char[iLength+1];

strcpy(mData,Str);

 

}

void CEasyStr::operator =(char *Str)

{

if(mData)

    {

delete []mData;

mData=NULL;

    }

int iLength=strlen(Str);

mData=new char[iLength+1];

strcpy(mData,Str);

 

}

void CEasyStr::operator =(int I)

{

char* temp=new char;

sprintf(temp,"%d",I);

//operator =(temp);

this=temp;

}

void CEasyStr::operator =(float F)

{

char* temp=new char;

sprintf(temp,"%.3f",F);

operator =(temp);

}

 

CEasyStr CEasyStr::operator +(const char* Str)

{

CEasyStr str1;

char* s=new char[GetLen()+strlen(Str)+1];

strcpy(s,mData);

strcat(s,Str);

str1=s;

return str1.GetSelf();

}

 

CEasyStr CEasyStr::operator +(char* Str)

{

CEasyStr str1;

char* s=new char[GetLen()+strlen(Str)+1];

strcpy(s,mData);

strcat(s,Str);

str1=s;

return str1.GetSelf();

 

 

}

 

CEasyStr CEasyStr::operator +(int I)

{

char* s=new char;

sprintf(s,"%d",I);

 

return *this+s;

}

CEasyStr CEasyStr::operator +(float F)

{

char* s=new char;

sprintf(s,"%.3f",F);

return *this+s;

}

CEasyStr CEasyStr::operator +(CEasyStr S)

{

S.AddRef();

return *this+S.GetChar();

 

}

char* CEasyStr::GetChar()

{

return mData;

}

void CEasyStr::operator =(CEasyStr Str)

{

this=Str.GetChar();

 

 

}

 

int CEasyStr::FindChar(char* s)

{

int count=0,leng=strlen(s);

 

for(int i=0;i<GetLen()-leng+1;i++)

    {

for(int j=0;j<leng;j++)

        {

if(mData[i+j]!=s[j])

break;

else if(j==leng-1)

return i;

        }

    }

return

}

 

 

 

CEasyStr CEasyStr::CopyFromIndex(int index,int count)

{

CEasyStr cEs;

cEs="";

if (index<0 || count<0 )

    {

return cEs.GetSelf();

    }

 

int length=GetLen();

if(index>=length)

    {

return cEs.GetSelf();

    }

int sLength;

if(index+count>=length)

sLength=length-index;

else

sLength=count;

 

char* str=new char[sLength+1];

for(int i=0;i<sLength;i++)

str[i]=mData[index+i];

str[sLength]='\0';

 

cEs=str;

return cEs.GetSelf();

 

}

CEasyStr CEasyStr::CutLeft(int index)

{    

CEasyStr Str;

Str=CopyFromIndex(0,index+1);

 

return Str.GetSelf();

 

}

CEasyStr CEasyStr::CutRight(int index)

{

CEasyStr Str;

Str=CopyFromIndex(index,GetLen()-index);

 

return Str.GetSelf();

 

}