将每一个字符串处理抽象成一个类,而不再是一个过程函数,这段程序提供的功能如下
![](https://img-blog.csdn.net/20180729093823863?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzEzMzU4NTczMjA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
相关源代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 12 //系统默认的值为5
typedef struct Struct_string
{
//属性
int current_number;
char *pstr[MAX];
//函数行为
//用户设置字符串
int (*Set_string_form_user) (struct Struct_string* pData, const char *pInfo,const char Judge);
//用户得到字符串
int (*Get_string_form_system)(const struct Struct_string* pData,int Serial_number,char **p);
//系统清除 char *pstr[MAX];指向的内存为空 内部函数使用
void (*clear)(struct Struct_string* pData );
}Struct_string,*PStruct_string;
int Set_string_form_user(struct Struct_string* pData, const char *pInfo,const char Judge)
{
char *pNewInfo = NULL;
char *pNext = NULL;
char *pStrItem = NULL;
int i=0;
int nItemCount = 0;
int len = 0;
//第一步 判断用户输入的是否为正确的值 1.传入非法指针 或者 传入空
if (NULL == pInfo || ((len = strlen(pInfo))<=0) || pData->current_number >= MAX) //数据打到了上限
{
printf("User data is illegal\n");
return -1;
}
//第二步进行内存释放功能
pData->clear(pData);
//第三步进行数据的拷贝
pNewInfo = (char *) malloc(len + 1);
strcpy(pNewInfo, pInfo);
//统计个数
nItemCount = 1;
pNext = strchr(pNewInfo, Judge);
while (pNext) //这里没有必要进行统计
{
nItemCount++;
pStrItem = pNext + 1;
pNext = strchr(pStrItem, Judge);
}
//填充数据
pStrItem = pNewInfo;
for (i = 0; i < nItemCount; i++)
{
pNext = strchr(pStrItem, Judge);
if(pNext)
{
*pNext = 0;
if(strlen(pStrItem) > 0)
{
len = strlen(pStrItem);
pData->pstr[pData->current_number] = (char *) malloc(len + 1) ;
strcpy(pData->pstr[pData->current_number], pStrItem);
pData->current_number++;
if(pData->current_number > MAX)
{
printf("Out of data range\n");
return -1;
}
}
pStrItem = pNext + 1;
}
else
{
if (strlen(pStrItem) > 0)
{
len = strlen(pStrItem); //加入到最后一个数据找那个
pData->pstr[pData->current_number] = (char *) malloc(len + 1) ;
strcpy(pData->pstr[pData->current_number], pStrItem);
pData->current_number++;
if(pData->current_number > MAX)
{
printf("Out of data range\n");
return -1;
}
}
break;
}
}
//最后一步 释放内存
free(pNewInfo);
return 0;
}
void clear(struct Struct_string* pData )
{
int i=0;
if(pData->current_number <=0)
{
return ;
}
for( i=0;i<pData->current_number;i++)
{
free(pData->pstr[i]);
pData->pstr[i] = NULL;
}
pData->current_number=0;
}
//进行深度拷贝,复制出去值必须要利用free释放
int Get_string_form_system(const struct Struct_string* pData,int Serial_number,char **p)
{
int len =0;
if( Serial_number<0 || Serial_number > pData->current_number-1) //判断是否在范围内
{
printf("Exceeding the range of read data\n");
return -1;
}
len = strlen(pData->pstr[Serial_number]); //加入到最后一个数据找那个
*p = (char *) malloc(len + 1);
strcpy(*p,pData->pstr[Serial_number]);
return 0;
}
static Struct_string test_string;
int main()
{
char *p_test = NULL;
//初始化数据
memset(test_string.pstr,0,MAX * sizeof (char*));
test_string.current_number =0;
test_string.Set_string_form_user =Set_string_form_user;
test_string.Get_string_form_system=Get_string_form_system;
test_string.clear =clear;
// if(0!=test_string.Set_string_form_user(&test_string,"add,hu,,,,,,,,huhu"))
if(0!=test_string.Set_string_form_user(&test_string,",,,hu,,,-,,,,,,,huxiaoguang",'-'))
{
printf("set data is fall\n");
return 0;
}
if(0!=test_string.Get_string_form_system(&test_string,0,&p_test))//,&p_test
{
printf("read data is fall\n");
printf("the first\n");
return 0;
}
printf("%s\n",p_test); //hu
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,1,&p_test))//,&p_test
{
printf("read data is fall\n");//xiao
printf("the second\n");
return 0;
}
printf("%s\n",p_test);
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,2,&p_test))//,&p_test
{
printf("read data is fall\n");
printf("the third\n");
return 0;
}
printf("%s\n",p_test); //jixiao
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,3,&p_test))//,&p_test
{
printf("read data is fall\n");//hu
printf("the fourth\n");
return 0;
}
printf("%s\n",p_test);
free(p_test);
if(0!=test_string.Set_string_form_user(&test_string,"hu,xiao,jixiao,hu,yang"))
{
printf("set data is fall\n");
return 0;
}
if(0!=test_string.Get_string_form_system(&test_string,0,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //hu
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,1,&p_test))//,&p_test
{
printf("read data is fall\n");//xiao
return 0;
}
printf("%s\n",p_test);
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,2,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //jixiao
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,3,&p_test))//,&p_test
{
printf("read data is fall\n");//hu
return 0;
}
printf("%s\n",p_test);
free(p_test);
//重新复制设置
///////////////////////////////////////////////////////////////////////////////
if(0!=test_string.Set_string_form_user(&test_string,"xiao,huxiao,husn,ren ming i,hxhg"))
{
printf("set data is fall\n");
return 0;
}
if(0!=test_string.Get_string_form_system(&test_string,0,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //hu
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,1,&p_test))//,&p_test
{
printf("read data is fall\n");//xiao
return 0;
}
printf("%s\n",p_test);
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,2,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //jixiao
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,3,&p_test))//,&p_test
{
printf("read data is fall\n");//hu
return 0;
}
printf("%s\n",p_test);
free(p_test);
///////////////////////////////////////////////////////////////////////////////
if(0!=test_string.Set_string_form_user(&test_string,"jixiao,renwu,hu,xiaoghu,hxhg"))
{
printf("set data is fall\n");
return 0;
}
if(0!=test_string.Get_string_form_system(&test_string,0,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //hu
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,1,&p_test))//,&p_test
{
printf("read data is fall\n");//xiao
return 0;
}
printf("%s\n",p_test);
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,2,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //jixiao
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,3,&p_test))//,&p_test
{
printf("read data is fall\n");//hu
return 0;
}
printf("%s\n",p_test);
free(p_test);
///////////////////////////////////////////////////////////////////////////////
if(0!=test_string.Set_string_form_user(&test_string,"xiao,huxiao,husn,ren ming i,hxhg"))
{
printf("set data is fall\n");
return 0;
}
if(0!=test_string.Get_string_form_system(&test_string,0,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //hu
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,1,&p_test))//,&p_test
{
printf("read data is fall\n");//xiao
return 0;
}
printf("%s\n",p_test);
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,2,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //jixiao
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,3,&p_test))//,&p_test
{
printf("read data is fall\n");//hu
return 0;
}
printf("%s\n",p_test);
free(p_test);
///////////////////////////////////////////////////////////////////////////////
if(0!=test_string.Set_string_form_user(&test_string,"ren,hongsh,shi,fei,hxhg"))
{
printf("set data is fall\n");
return 0;
}
if(0!=test_string.Get_string_form_system(&test_string,0,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //hu
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,1,&p_test))//,&p_test
{
printf("read data is fall\n");//xiao
return 0;
}
printf("%s\n",p_test);
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,2,&p_test))//,&p_test
{
printf("read data is fall\n");
return 0;
}
printf("%s\n",p_test); //jixiao
free(p_test);
if(0!=test_string.Get_string_form_system(&test_string,3,&p_test))//,&p_test
{
printf("read data is fall\n");//hu
return 0;
}
printf("%s\n",p_test);
free(p_test);
return 0;
}