mylib.c
来自「自己做的常用库和实现的数据结构。public domain.」· C语言 代码 · 共 219 行
C
219 行
#include "my.h"/* Print error message on stderr, then invoke exit(EXIT_FAILURE). */void fatal_error(const char *msg){ fprintf(stderr, "Fatal: %s\n", msg); exit(EXIT_FAILURE);}/* Invoke putchar('\a'). */void beep(void){ putchar('\a');}/* Various compare unit for function like qsort(), pick up what you need * according to cast type(c=char, i=int, etc) and keyword order(a or d). */int ccmpa(const void *a, const void *b){ return ( *((char *)a) - *((char *)b));}int ccmpd(const void *a, const void *b){ return ( *((char *)b) - *((char *)a));}int uccmpa(const void *a, const void *b){ return ( *((unsigned char *)a) - *((unsigned char *)b));}int uccmpd(const void *a, const void *b){ return ( *((unsigned char *)b) - *((unsigned char *)a));}int icmpa(const void *a, const void *b){ return ( *((int *)a) - *((int *)b));}int icmpd(const void *a, const void *b){ return ( *((int *)b) - *((int *)a));}int uicmpa(const void *a, const void *b){ unsigned int c = *( (unsigned int *)a), d = *( (unsigned int *)b); return c > d ? 1 : (c==d ? 0 : -1);}int uicmpd(const void *a, const void *b){ unsigned int c = *( (unsigned int *)a), d = *( (unsigned int *)b); return c < d ? 1 : (c==d ? 0 : -1);}int lcmpa(const void *a, const void *b){ long c = *( (long *)a), d = *( (long *)b); return c > d ? 1 : (c==d ? 0 : -1);}int lcmpd(const void *a, const void *b){ long c = *( (long *)a), d = *( (long *)b); return c < d ? 1 : (c==d ? 0 : -1);}int ulcmpa(const void *a, const void *b){ unsigned long c = *( (unsigned long *)a), d = *( (unsigned long *)b); return c > d ? 1 : (c==d ? 0 : -1);}int ulcmpd(const void *a, const void *b){ unsigned long c = *( (unsigned long *)a), d = *( (unsigned long *)b); return c < d ? 1 : (c==d ? 0 : -1);}#if defined(__ISOC) && (__ISOC >= 1999)int llcmpa(const void *a, const void *b){ long long c = *( (long long *)a), d = *( (long long *)b); return c > d ? 1 : (c==d ? 0 : -1);}int llcmpd(const void *a, const void *b){ long long c = *( (long long *)a), d = *( (long long *)b); return c < d ? 1 : (c==d ? 0 : -1);}int ullcmpa(const void *a, const void *b){ unsigned long long c = *( (unsigned long long *)a), d = *( (unsigned long long *)b); return c > d ? 1 : (c==d ? 0 : -1);}int ullcmpd(const void *a, const void *b){ unsigned long long c = *( (unsigned long long *)a), d = *( (unsigned long long *)b); return c < d ? 1 : (c==d ? 0 : -1);}#endif/* Available flag: 'm', 'c', 'r'. invoke malloc/calloc/realloc() correspondingly, * and handle it's return value. (if NULL, invoke exit(EXIT_FAILURE)). */void *mem(const char flag, ...){ va_list ap; void *p = NULL, *ptr; size_t nmemb, size; va_start(ap, flag); switch(flag) { case 'm': case 'M': size = va_arg(ap, size_t); if( (p = malloc(size)) == NULL) fatal_error("mem alloc failed !"); break; case 'c': case 'C': nmemb = va_arg(ap, size_t); size = va_arg(ap, size_t); if( (p = calloc(nmemb, size)) == NULL) fatal_error("mem alloc failed !"); break; case 'r': case 'R': ptr = va_arg(ap, void *); size = va_arg(ap, size_t); if( (p = realloc(ptr, size)) == NULL) fatal_error("mem alloc failed !"); break; default: fatal_error("flag must be 'm', 'c' or 'r' !"); } va_end(ap); return p;}/* Invoke fopen() and check it's return value; * if NULL, invoke exit(EXIT_FAILURE), * else return fopen()'s return value. */FILE *ffopen(const char *path, const char *mode){ FILE *fp = fopen(path, mode); if(fp == NULL) fatal_error("file open error !"); return fp;}/* Get array's NO. offset element. */void *getelem(void *base, size_t size, size_t offset){ return (void *)((char *)base + offset * size);}/* Check if array is keyword ordered or not. */bool asc_or_desc(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)){ char *p, *q; int flaga=0,flagd=0; size_t i; int t; for(i=0; i < nmemb-1; i++) { p = (char *)base + i*size, q = p + size; t = (*compar)(p, q); if(t < 0) flaga=1; else if(t > 0) flagd=1; } return flaga && flagd ? false : true;}/* Find array's min value or max value element. */void *min_or_max(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)){ char *p = base, *q; size_t i; for(i=1; i < nmemb; i++) { q = (char *)base + i * size; if( (*compar)(p, q) > 0 ) p = q; } return (void *)p;}/* Find array's medium value element. if(qsort_flag) qsort() will be invoked; * otherwise make sure array is keyword ordered. */void *medium(int qsort_flag, void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)){ const char *p, *q; size_t cnt = 0, cnt2 = 0; if(qsort_flag) qsort(base, nmemb, size, compar); for(p=base; p < (const char *)base + (nmemb-1) * size; p += size) for(q = p+size; q < (const char *)base + nmemb * size; q += size) if( (*compar)(p, q) == 0) cnt++; for(q = (const char *)base + size; q < (const char *)base + nmemb * size; q += size) if( ( (*compar)(base, q) != 0) && (cnt2++ == (nmemb - cnt)/2 )) return (void *)q; return base;}/* Bubble sort source array. */void bsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *), void (*swap)(void *, void *)){ char *p, *q; size_t i, j; for(i=0; i < nmemb-1; i++) { p = (char *)base + i*size; for(j=i+1; j<nmemb; j++) { q = (char *)base + j * size; if( (*compar)(p, q) > 0 ) (*swap)(p,q); } }}/* Selection sort source array. */void ssort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *), void (*swap)(void *, void *)){ size_t i; char *p, *q; for(i=0; i < nmemb-1; i++) { p = (char *)base + i*size; q = min_or_max(p, nmemb-i, size, compar); if(p != q) (*swap)(p,q); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?