自写字符串类,支持查找,拆分,重载

//头文件

#ifndef _CUSTOM_STRING_H 

#define _CUSTOM_STRING_H
#endif
#include <stdio.h>
#include <stdlib.h>






class custom_string
{
public:
custom_string(const char* str);
custom_string(void);
custom_string(custom_string &data);
~custom_string(void);


//函数
char* get_string();
int get_strlen();


//重载
void operator=(custom_string* str);
void operator=(char* str);
void operator+=(custom_string str);
void operator+=(char* str);

operator const char*();


custom_string operator+(custom_string str);
custom_string operator+(char* str);


//格式化操作


int str_format(const char* str);
//清空数据


bool str_clear();




//截取数据
void cut_string(char*str,int first,int last);
//查找数据


int str_find(char lab,int first = 0);
int str_find(const char* str,int first = 0);




//成员变量
private:
char* m_pata;
int mem_size;

};





//CPP文件

#include "custom_string.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


custom_string::custom_string():mem_size(0)
{


}


custom_string::custom_string(const char* str)
{
m_pata = (char*)malloc(strlen(str));
strcpy(m_pata,str);
mem_size = strlen(str);
}


custom_string::custom_string(custom_string &data)
{
int p_data_len = strlen(data.m_pata);
mem_size = p_data_len;
this->m_pata = (char*)malloc(p_data_len);
strcpy(this->m_pata,data.get_string());
}


custom_string::~custom_string(void)
{
//free(this->m_pata);
//this->m_pata = NULL;
}




char* custom_string::get_string()
{
return m_pata;
}




int custom_string::get_strlen()
{
return strlen(this->m_pata);
}


void custom_string::operator=(custom_string* str)
{
int p_data_len = strlen(str->m_pata);
if (mem_size < p_data_len && mem_size>0)
{
free(this->m_pata);
this->m_pata = (char*)malloc(p_data_len);
strcpy(this->m_pata,str->m_pata);
}else
{
this->m_pata = (char*)malloc(p_data_len);
strcpy(this->m_pata,str->m_pata);
}
}




void custom_string::operator=(char* str)
{
int p_data_len = strlen(str);
if (mem_size < p_data_len && mem_size>0)
{
memset(this->m_pata,0,mem_size);
this->m_pata = (char*)malloc(p_data_len);
strcpy(this->m_pata,str);
}else
{
this->m_pata = (char*)malloc(p_data_len);
strcpy(this->m_pata,str);
}
}


void custom_string::operator+=(custom_string str)
{
int all_len = strlen(str.m_pata)+get_strlen();
if (mem_size <  all_len && mem_size>0)
{
char* temp = (char*)malloc(get_strlen());
strcpy(temp,this->m_pata);
memset(this->m_pata,0,mem_size);
this->m_pata = (char*)malloc(all_len);
strcpy(this->m_pata,temp);
temp = NULL;
strcat(this->m_pata,str.m_pata);
}else
{
strcat(this->m_pata,str.m_pata);
}
}


void custom_string::operator+=(char* str)
{


int all_len = strlen(str)+get_strlen();
if (mem_size <  all_len && mem_size>0)
{
char* temp = (char*)malloc(get_strlen());
strcpy(temp,this->m_pata);
memset(this->m_pata,0,mem_size);
this->m_pata = (char*)malloc(all_len);
strcpy(this->m_pata,temp);
temp = NULL;
strcat(this->m_pata,str);
}else
{
strcat(this->m_pata,str);
}
}


custom_string::operator const char*()
{
return this->get_string();
}




int custom_string::str_format(const char* str)
{
int p_data_len = strlen(str);
if (mem_size < p_data_len && mem_size>0)
{
memset(this->m_pata,0,mem_size);
this->m_pata = (char*)malloc(p_data_len);
strcpy(this->m_pata,str);
}else
{
this->m_pata = (char*)malloc(p_data_len);
strcpy(this->m_pata,str);
}
return atoi(str);
}




bool custom_string::str_clear()
{
if (this->mem_size != 0)
{
memset(m_pata,0,mem_size);
}


return 1;
}




void custom_string::cut_string(char*str,int first,int last)
{
if (get_strlen() != 0)
{
for (int i = first;i<=last;i++)
{
*str++ =  m_pata[i];
}


*str = '\0';
}
}




int custom_string::str_find(char lab,int first)
{
for (int i = first;i<get_strlen();i++)
{
if (m_pata[i] == lab)
{
return i;
}
}


return -1;
}




int custom_string::str_find(const char* str,int first)
{
int len = strlen(str);
char lab = str[0];
char * temp = (char*)malloc(len);
memset(temp,0,len);


for (int i = first;i<get_strlen();i++)
{
if (lab == m_pata[i])
{
cut_string(temp,i,i+len-1);
if (strcmp(temp,str) == 0)
{
return i;
}
}
}
return -1;
}



custom_string custom_string::operator+(custom_string str)
{
char * temp = (char*)malloc(this->get_strlen());
memset(temp,0,this->get_strlen());
int len = this->get_strlen();
if (mem_size < str.get_strlen()+len && mem_size >0)
{
strcpy(temp,m_pata);
memset(m_pata,0,mem_size);
m_pata = (char*)malloc(len+str.get_strlen());
memset(m_pata,0,len+str.get_strlen());
strcpy(m_pata,temp);
strcat(m_pata,str.m_pata);
}else
{
strcat(m_pata,str.m_pata);
}


return m_pata;
}




custom_string custom_string::operator+(char* str)
{
char * temp = (char*)malloc(this->get_strlen());
memset(temp,0,strlen(str));
int len = this->get_strlen();
if (mem_size < strlen(str) && mem_size >0)
{
strcpy(temp,m_pata);
memset(m_pata,0,mem_size);
m_pata = (char*)malloc(len+strlen(str));
memset(m_pata,0,len+strlen(str));
strcpy(m_pata,temp);
strcat(m_pata,str);
}else
{
strcat(m_pata,str);
}


return m_pata;
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页