#include "stdafx.h" #include "MyString.h" #include <string.h> MyString &MyString::operator+ (const char* str) { static MyString backup; backup.assign(this->Getstring()); backup.append(str); return backup; } MyString &MyString::operator+ (MyString &str) { static MyString backup; backup.assign(this->Getstring()); backup.append(str.Getstring()); return backup; } MyString &MyString::operator= (const char *str) { this->assign(str); return *this; } MyString &MyString::operator= (MyString &str) { this->assign(str.Getstring()); return *this; } MyString::MyString(const char *str) { if(str==NULL) { Data=NULL; } else { Data=new char [strlen(str)+1]; strcpy(Data,str); } } MyString::~MyString() { if(Data!=NULL) delete [] Data; Data=NULL; } void MyString::assign(const char *str) { if(Data!=NULL) delete [] Data; Data=NULL; Data=new char [strlen(str)+1]; strcpy(Data,str); } void MyString::clean() { if(Data!=NULL) delete [] Data; Data=NULL; } int MyString::length() { return strlen(Data); } const char* MyString::Getstring() { return Data; } void MyString::append(const char *str) { if(Data==NULL) assign(str); else { char *Backup=new char [length()+1]; strcpy(Backup,Data); delete [] Data; Data=new char [strlen(Backup)+strlen(str)+1]; strcpy(Data,Backup); strcat(Data,str); delete [] Backup; } } char *MyString::token(char *str) { if(Data==NULL) return NULL; char *result=new char [length()+1]; int count=0; for(int i=0;i<length();i++) { // 檢查是否有token bool found=false; for(unsigned int j=0;j<strlen(str);j++) { if(Data[i]==str[j]) { //如果有找到 直接跳出 並 found=true found=true; break; } } if(!found) //沒找到表示要把目前處理的字元放到 result內 result[count++]=Data[i]; else { result[count]='\0'; // Backup the Left String char *backup=new char[length()-count+1]; int index=0; strcpy(backup,&Data[count+1]); //把剩下的重新給Data delete [] Data; Data=new char[strlen(backup)+1]; strcpy(Data,backup); delete [] backup; return result; } } result[count]='\0'; delete [] Data; Data=NULL; return result; }
2008年8月19日 星期二
簡單運算元多載
MyString.cpp code
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言