重写常用函数头文件(初测版)~
记得之前写了个常用函数头文件~不过现在学多一点内容重新写感觉会好看一点,这个是初测版还有很多没有完善~P_________S:最新代码15楼更!
Common.h
程序代码:
#include"Common.h"
#define _NDEBUG false
#if _NDEBUG==true
#define NDEBUG
#endif
#include<assert.h>
#define NODE_ISNULL true
#define MEMSET_SET true
#define DEL_SET_NULL true
bool Node_Creat(void**Node,const size_t Size)
{
#if NODE_ISNULL==true
if (*Node!=NULL)
return false;
#else
free(*Node);
#endif
*Node=malloc(Size);
#ifndef NDEBUG
assert(*Node);
#else
if (*Node==NULL)
return false;
#endif
#if MEMSET_SET==true
memset(*Node,0,Size);
#endif
return true;
}
bool Node_Realloc(void**Node,const size_t Size)
{
#ifdef NDEBUG
void* np=*Node;
#endif
*Node=realloc(*Node,Size);
#ifndef NDEBUG
assert(*Node);
#else
if (*Node==NULL)
{
*Node=np;
return false;
}
#endif
return true;
}
bool Node_Free(void** Node)
{
if (*Node==NULL)
return false;
free(*Node);
#if DEL_SET_NULL==true
*Node=NULL;
#endif
return true;
}
int String_Get(FILE* fp,char** p,int end_signe)
{
int c=0;
unsigned len=0;
char* pt=NULL;
ptrdiff_t capacity=BUFSIZ;
#if NODE_ISNULL==true
if (*p!=NULL)
return -1;
#else
free(*p);
#endif
#ifndef NDEBUG
if (Node_Creat((void**)p,BUFSIZ)==false )
return -1;
#else
Node_Creat((void**)p,BUFSIZ);
#endif
pt=*p;
while ((c=fgetc(fp))!=end_signe)
{
*pt++=c;
len=pt-*p;
if (len==capacity-1)
{
#ifndef NDEBUG
if (Node_Realloc((void**)p,sizeof (char )*(capacity+=BUFSIZ))==false )
return -1;
#else
Node_Realloc((void**)p,sizeof (char )*(capacity+=BUFSIZ));
#endif
}
pt=*p+len;
}
#ifndef NDEBUG
if (Node_Realloc((void**)p,sizeof (char )*len)==false )
return -1;
#else
Node_Realloc((void**)p,sizeof (char )*len);
#endif
*pt='\0';
return len;
}
主函数.c
程序代码:
#include"Common.h"
COMP_CREAT(Comp_Int,int,NUM,MAX,0)
COMP_CREAT(Comp_Str,char*,STR,MIN,0)
COMP_CREAT(Comp_Float,float,NUM,MAX,1e-6)
int main()
{
int i=0;
int arr[]={5,6,1,2,3,4,7,9,8};
float arr_f[]={1.52,0.64,3.16,2.57,99.9,0.005};
char* str[]={"aaa","ddd","bbb","ccc"};
char** ps=str;
char* p=NULL;
unsigned len=0;
qsort(arr,sizeof(arr)/sizeof(*arr),sizeof(*arr),Comp_Int);
for (i=0;i<sizeof(arr)/sizeof(*arr);++i)
printf("%-4d",arr[i]);
puts("");
qsort(str,sizeof(str)/sizeof(*str),sizeof(*str),Comp_Str);
while (ps-str<sizeof(str)/sizeof(*str))
puts(*ps++);
qsort(arr_f,sizeof(arr_f)/sizeof(*arr_f),sizeof(*arr_f),Comp_Float);
for (i=0;i<sizeof(arr_f)/sizeof(*arr_f);++i)
printf("%-8.4f",arr_f[i]);
puts("");
len=String_Get(stdin,&p,'\n');
puts(p);
printf("%u\n",len);
Node_Free(&p);
return 0;
}
效果如下~
嗯,或者有时间再更和完善一些功能
~[此贴子已经被作者于2017-11-21 18:02编辑过]










~