碧海潮生's profile碧海潮生的小屋PhotosBlogLists Tools Help

Blog


    May 25

    设计笔记——标准库(下)

    4 <stdlib.h>

    4.1 字符串转换

    double    atof    (const char*);
    int    atoi    (const char*);
    long    atol    (const char*);

    double    strtod    (const char*, char**);
    long    strtol    (const char*, char**, int);
    unsigned long    strtoul    (const char*, char**, int);

    1> 第二组函数的参数意义如下:
    const char*    指向需要转换的字符串
    char**        更新后指向当前数字串之后的一个位置
    int        基数(进制数)

    2> strtol函数举例:

    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>

    int main()
    {
        char *buf = "  0B 00 41 42 43 44 45 46 47 48 49  ";
        char *ptr = buf;
       
         while (isspace(*ptr))
            ptr++;
        while (*ptr != '\0')
        {
            printf("%ld\n", strtol(ptr, &ptr, 16));
            while (isspace(*ptr))
                ptr++;
        }
       
        return 0;
    }

    4.2 随机数

    常量
    #define    RAND_MAX    0x7FFF        rand的最大返回值

    函数
    void    srand    (unsigned int);        置随机数发生器(种子)
    int    rand    (void);            返回下一个伪随机数


    4.3 内存管理

    常量
    #define NULL    ((void *)0)        空指针

    函数
    void*    calloc    (size_t, size_t);    分配内存, 并清零
    void*    malloc    (size_t);        分配内存
    void*    realloc    (void*, size_t);    重新分配内存, 返回新指针
    void    free    (void*);        释放内存


    4.4 与环境的接口

    常量
    #define    EXIT_SUCCESS    0
    #define    EXIT_FAILURE    1

    函数
    void    abort    (void);
    void    exit    (int);
    int    atexit    (void (*)(void));

    int    system    (const char*);
    char*    getenv    (const char*);


    4.5 查找与排序

    void*    bsearch    (const void*, const void*, size_t, size_t,
                     int (*)(const void*, const void*));
    void    qsort    (const void*, size_t, size_t,
                     int (*)(const void*, const void*));

    1> comp函数的返回值
    int comp(const void *p1, const void *p2)
    {
        const int *pi1 = (const int *)p1;
        const int *pi2 = (const int *)p2;
        return *pi1 - *pi2;
    }
    若第一个指针所指向的内容, 排序后应该放在第二个指针所指向的内容之前, 那么应返回负值;
    反之返回正值; 其他情况返回0. 上例中的comp函数将int数组按升序排序.

    2> qsort的调用方法
    qsort((void *)list, length, sizeof(int), comp);


    4.6 整数运算

    结构
    typedef struct { int quot, rem; } div_t;
    typedef struct { long quot, rem; } ldiv_t;

    函数
    int    abs    (int);
    long    labs    (long);

    div_t    div    (int, int);
    ldiv_t    ldiv    (long, long);


    4.7 多字节字符

    常量
    MB_CUR_MAX        多字节字符中的最大字节数

    函数
    size_t    wcstombs    (char*, const wchar_t*, size_t);
    int    wctomb        (char*, wchar_t);

    int    mblen        (const char*, size_t);
    size_t    mbstowcs    (wchar_t*, const char*, size_t);
    int    mbtowc        (wchar_t*, const char*, size_t);


    5 <string.h>

    5.1 复制

    char*    strcpy (char *s1, const char *s2);
    将字符串s2复制到s1指定的地址

    char*    strncpy (char *s1, const char *s2, size_t len);
    void*     memcpy (void *s1, const void *s2, size_t len);
    将s2的前len个字符(字节)复制到s1中指定的地址, 不加'\0'

    void*    memmove (void *s1, const void *s2, size_t len);
    当源单元和目的单元缓冲区交迭时使用

    size_t    strxfrm (char *s1, const char *s1, size_t len);
    根据程序当前的区域选项, 将s2的前len个字符(字节)复制到s1中指定的地址, 不加'\0'


    5.2 连接

    char*    strcat (char *s1, const char *s2);
    将字符串s2连接到s1尾部

    char*    strncat (char *s1, const char *s2, size_t len);
    将字符串s2的前len个字符连接到s1尾部, 不加'\0'


    5.3 比较

    int    strcmp (const char *s1, const char *s2);
    比较字符串s1和s2

    int    strncmp (const char *s1, const char *s2, size_t len);
    int     memcmp (const void *s1, const void *s2, size_t len);
    对s1和s2的前len个字符(字节)作比较

    int    strcoll (const char *s1, const char *s2);
    根据程序当前的区域选项中的LC_COLLATE, 比较字符串s1和s2


    5.4 查找

    char*    strchr (const char *s, int ch);
    void*    memchr (const void *s, int ch, size_t len);
    在s中查找给定字符(字节值)ch第一次出现的位置

    char*    strrchr (const char *s, int ch);
    在串s中查找给定字符ch最后一次出现的位置, r表示从串尾开始

    char*    strstr (const char *s1, const char *s2);
    在串s1中查找指定字符串s2第一次出现的位置

    size_t    strspn (const char *s1, const char *s2);
    返回s1中第一个在s2中不存在的字符的索引(find_first_not_of)

    size_t    strcspn (const char *s1, const char *s2);
    返回s1中第一个也在s2中存在的字符的索引(find_first_of)

    char*    strpbrk (const char *s1, const char *s2);
    与strcspn类似, 区别是返回指针而不是索引

    char*    strtok (char *s1, const char *s2);
    从串s1中分离出由串s2中指定的分界符分隔开的记号(token)
    第一次调用时s1为需分割的字串, 此后每次调用都将s1置为NULL,
    每次调用strtok返回一个记号, 直到返回NULL为止


    5.5 其他

    size_t    strlen (const char *s);
    求字符串s的长度

    void*    memset (void *s, int val, size_t len);
    将从s开始的len个字节置为val

    char*    strerror (int errno);
    返回指向错误信息字符串的指针


    6 <math.h>

    6.1 三角函数
    double    sin (double);
    double    cos (double);
    double    tan (double);

    6.2 反三角函数
    double    asin (double);            结果介于[-PI/2, PI/2]
    double    acos (double);            结果介于[0, PI]
    double    atan (double);            反正切(主值), 结果介于[-PI/2, PI/2]
    double    atan2 (double, double);        反正切(整圆值), 结果介于[-PI/2, PI/2]

    6.3 双曲三角函数
    double    sinh (double);
    double    cosh (double);
    double    tanh (double);

    6.4 指数与对数
    double    exp (double);
    double    pow (double, double);
    double    sqrt (double);
    double    log (double);            以e为底的对数
    double    log10 (double);

    6.5 取整
    double    ceil (double);            取上整
    double    floor (double);            取下整

    6.6 绝对值
    double    fabs (double);

    6.7 标准化浮点数
    double    frexp (double f, int *p);    标准化浮点数, f = x * 2^p, 已知f求x, p ( x介于[0.5, 1] )
    double    ldexp (double x, int p);    与frexp相反, 已知x, p求f

    6.8 取整与取余
    double    modf (double, double*);        将参数的整数部分通过指针回传, 返回小数部分
    double    fmod (double, double);        返回两参数相除的余数


    7 结语

    某些开发环境下,如Keil C,并不提供所有的标准C库(如time.h、signal.h)。
    某些函数的功能或接口可能也发生了改变(如printf)。
    也有可能提供了某些特殊的库/库函数(如absacc.h、intrins.h)。

    在某种环境下开发,首先应熟悉其提供的语言特性和库函数。


    参考文献:

    [1] C Interfaces and Implementations,David R. Hanson
    [2] C and C++ Code Capsules,Chuck Allison
    [3] C51 User's Guide,Keil Software Inc. and Keil Elektronik GmbH

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://jx-kingwei.spaces.live.com/blog/cns!F7A152EB74B9576E!1505.trak
    Weblogs that reference this entry
    • None