分享一个常用函数处理的头文件~
这些函数感觉是比较常用的~就拿出来看看~现在感觉作用不是很大~看看到时会不会去完善一下~感觉COMMON_Input_Data这个函数可以用可变参数实现~到时我学会时再改进一下~
PS:Common.h里面或许有些编译器不支持_int64那个可以去掉(毕竟64位比较函数比较少用)~
程序代码:#ifndef __COMMON__
#define __COMMON__
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<ctype.h>
#define BUFF_MAX 10 //最大缓冲区
void COMMON_Creat_Node(void** p,size_t size); //创建节点
void COMMON_Free_Node(void** p); //删除节点
int COMMON_Comp_Max_Int(const void* a,const void* b); //比较函数
int COMMON_Comp_Max_Float(const void* a,const void* b);
int COMMON_Comp_Max_Double(const void* a,const void* b);
int COMMON_Comp_Max_Char(const void* a,const void* b);
int COMMON_Comp_Max_String(const void* a,const void* b);
int COMMON_Comp_Max_Short(const void* a,const void* b);
int COMMON_Comp_Max_Int64(const void* a,const void* b);
int COMMON_Comp_Min_Int(const void* a,const void* b); //比较函数
int COMMON_Comp_Min_Float(const void* a,const void* b);
int COMMON_Comp_Min_Double(const void* a,const void* b);
int COMMON_Comp_Min_Char(const void* a,const void* b);
int COMMON_Comp_Min_String(const void* a,const void* b);
int COMMON_Comp_Min_Short(const void* a,const void* b);
int COMMON_Comp_Min_Int64(const void* a,const void* b);
int COMMON_Comp_UnMax_Int(const void* a,const void* b); //无符号比较函数
int COMMON_Comp_UnMax_Char(const void* a,const void* b);
int COMMON_Comp_UnMax_Short(const void* a,const void* b);
int COMMON_Comp_UnMax_Int64(const void* a,const void* b);
int COMMON_Comp_UnMin_Int(const void* a,const void* b); //无符号比较函数
int COMMON_Comp_UnMin_Char(const void* a,const void* b);
int COMMON_Comp_UnMin_Short(const void* a,const void* b);
int COMMON_Comp_UnMin_Int64(const void* a,const void* b);
void COMMON_Input_String(char** p); //从输入字符串
/*****************************************辅助函数声明********************************************/
void COMMON_Input_Slove(char** p,char** pt,size_t* length); //对输入数据进行处理
void COMMON_Input_Absorb_Buffer(char** ); //对缓冲区残留数据进行处理
void COMMON_Input_Catch(char* ,char** ); //异常处理
int COMMON_Input_Data(const char* ,void* ); //输入数据
/*********************************************************************************************/
typedef struct COMMON_FUN
{
void (*Creat_Node)(void** p,size_t size);
void (*Free_Node)(void** p);
int (*Comp_Max_Int)(const void* a,const void* b);
int (*Comp_Max_Float)(const void* a,const void* b);
int (*Comp_Max_Double)(const void* a,const void* b);
int (*Comp_Max_Char)(const void* a,const void* b);
int (*Comp_Max_String)(const void* a,const void* b);
int (*Comp_Min_Int)(const void* a,const void* b);
int (*Comp_Min_Float)(const void* a,const void* b);
int (*Comp_Min_Double)(const void* a,const void* b);
int (*Comp_Min_Char)(const void* a,const void* b);
int (*Comp_Min_String)(const void* a,const void* b);
int (*Comp_UnMax_Int)(const void* a,const void* b); //无符号比较函数
int (*Comp_UnMax_Char)(const void* a,const void* b);
int (*Comp_UnMax_Short)(const void* a,const void* b);
int (*Comp_UnMax_Int64)(const void* a,const void* b);
int (*Comp_UnMin_Int)(const void* a,const void* b); //无符号比较函数
int (*Comp_UnMin_Char)(const void* a,const void* b);
int (*Comp_UnMin_Short)(const void* a,const void* b);
int (*Comp_UnMin_Int64)(const void* a,const void* b);
void (*Input_String)(char** p);
int (*Input_Data)(const char* format,void* s);
}COMMON_FUN;
COMMON_FUN Common=
{
COMMON_Creat_Node,
COMMON_Free_Node,
COMMON_Comp_Max_Int,
COMMON_Comp_Max_Float,
COMMON_Comp_Max_Double,
COMMON_Comp_Max_Char,
COMMON_Comp_Max_String,
COMMON_Comp_Min_Int,
COMMON_Comp_Min_Float,
COMMON_Comp_Min_Double,
COMMON_Comp_Min_Char,
COMMON_Comp_Min_String,
COMMON_Comp_UnMax_Int,
COMMON_Comp_UnMax_Char,
COMMON_Comp_UnMax_Short,
COMMON_Comp_UnMax_Int64,
COMMON_Comp_UnMin_Int,
COMMON_Comp_UnMin_Char,
COMMON_Comp_UnMin_Short,
COMMON_Comp_UnMin_Int64,
COMMON_Input_String,
COMMON_Input_Data,
};
void COMMON_Creat_Node(void** p,size_t size) //创建节点
{
*p=malloc(size);
assert(*p);
memset(*p,0,size);
}
void COMMON_Free_Node(void** p) //删除节点
{
if (*p==NULL)
return ;
free(*p);
*p=NULL;
}
int COMMON_Comp_Max_Int(const void* a,const void* b) //比较INT型数据
{
int ta=*(int* )a;
int tb=*(int* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_Max_Float(const void* a,const void* b) //比较Float型数据
{
float ta=*(float* )a;
float tb=*(float* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_Max_Double(const void* a,const void* b) //比较Doulbe型数据
{
double ta=*(double* )a;
double tb=*(double* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_Max_Char(const void* a,const void* b) //比较Char型数据
{
char ta=*(char* )a;
char tb=*(char* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_Max_String(const void* a,const void* b) //比较字符串类数据
{
int t=strcmp((char* )a,(char* )b);
if (t<0)
return -1;
if (t>0)
return 1;
return 0;
}
int COMMON_Comp_Max_Short(const void* a,const void* b)
{
short ta=*(short* )a;
short tb=*(short* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_Max_Int64(const void* a,const void* b)
{
_int64 ta=*(_int64*)a;
_int64 tb=*(_int64*)b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_Min_Int(const void* a,const void* b) //比较函数
{
int ta=*(int* )a;
int tb=*(int* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_Min_Float(const void* a,const void* b)
{
float ta=*(float* )a;
float tb=*(float* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_Min_Double(const void* a,const void* b)
{
double ta=*(double* )a;
double tb=*(double* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_Min_Char(const void* a,const void* b)
{
char ta=*(char* )a;
char tb=*(char* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_Min_String(const void* a,const void* b)
{
int t=strcmp((char* )a,(char* )b);
if (t<0)
return 1;
if (t>0)
return -1;
return 0;
}
int COMMON_Comp_Min_Short(const void* a,const void* b)
{
short ta=*(short* )a;
short tb=*(short* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_Min_Int64(const void* a,const void* b)
{
_int64 ta=*(_int64*)a;
_int64 tb=*(_int64*)b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_UnMax_Int(const void* a,const void* b) //无符号比较函数
{
unsigned int ta=*(unsigned int* )a;
unsigned int tb=*(unsigned int* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_UnMax_Char(const void* a,const void* b)
{
unsigned char ta=*(unsigned char* )a;
unsigned char tb=*(unsigned char* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_UnMax_Short(const void* a,const void* b)
{
unsigned short ta=*(unsigned short* )a;
unsigned short tb=*(unsigned short* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_UnMax_Int64(const void* a,const void* b)
{
unsigned _int64 ta=*(unsigned _int64* )a;
unsigned _int64 tb=*(unsigned _int64* )b;
if (ta<tb)
return -1;
if (ta>tb)
return 1;
return 0;
}
int COMMON_Comp_UnMin_Int(const void* a,const void* b) //无符号比较函数
{
unsigned int ta=*(unsigned int* )a;
unsigned int tb=*(unsigned int* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_UnMin_Char(const void* a,const void* b)
{
unsigned char ta=*(unsigned char* )a;
unsigned char tb=*(unsigned char* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_UnMin_Short(const void* a,const void* b)
{
unsigned short ta=*(unsigned short* )a;
unsigned short tb=*(unsigned short* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
int COMMON_Comp_UnMin_Int64(const void* a,const void* b)
{
unsigned _int64 ta=*(unsigned _int64* )a;
unsigned _int64 tb=*(unsigned _int64* )b;
if (ta<tb)
return 1;
if (ta>tb)
return -1;
return 0;
}
void COMMON_Input_String(char** p) //输入字符串
{
char* pt=NULL;
size_t length=0;
COMMON_Creat_Node((void** )p,(BUFF_MAX)*sizeof(char));
pt=*p;
while ((*pt=getchar())!='\n')
COMMON_Input_Slove(p,&pt,&length);
*pt='\0';
pt=(char* )realloc(*p,strlen(*p)*sizeof(char)); //压缩空间
COMMON_Input_Catch(pt,p);
*p=pt;
}
int COMMON_Input_Data(const char* format,void* s)
{
int k=0;
if ((k=scanf(format,s))!=1)
while (getchar()!='\n');
return k;
}
/************************辅助函数调用处理********************************************************/
void COMMON_Input_Slove(char** p,char** pt,size_t* length) //对输入数据进行处理
{
++*length;
if (*length%(BUFF_MAX-1)==0)
{
char* ps=(char* )realloc(*p,(*length+BUFF_MAX)*sizeof(char));
COMMON_Input_Catch(ps,p);
*p=ps;
memset(*p+*length,0,BUFF_MAX*sizeof(char));
*pt=*p+*length-1;
}
++*pt;
}
void COMMON_Input_Absorb_Buffer(char** p)
{
while (getchar()!='\n');
COMMON_Free_Node((void** ) p);
*p=strdup("");
}
void COMMON_Input_Catch(char* ps,char** p)
{
if (ps!=NULL)
return ;
COMMON_Free_Node((void** )p);
assert(1);
}
/*********************************************************************************/
#undef BUFF_MAX
#endif[此贴子已经被作者于2017-7-2 14:29编辑过]




