求平方根,路径文件名,路径文件夹名

本文介绍了一个用于计算无符号长整型平方根的算法实现,该算法通过位移和比较来逐步逼近正确结果。此外,还提供了两个用于处理文件路径的函数,包括获取文件名和目录名的实用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

unsigned short int_sqrt(unsigned long a)
{
    unsigned long rem = 0;
    unsigned long root_val = 0;
    unsigned long divisor = 0;
    int i;

    for(i=0; i<16; ++i)
    {
        root_val <<= 1;
        rem = ((rem << 2) + (a >> 30));
        a <<= 2;
        divisor = (root_val<<1) + 1;
        if(divisor <= rem)
        {
            rem -= divisor;
            root_val++;
        }
    }

    return (unsigned short)(root_val);
}

char *basename(const char *filename)
{
    char *p = strrchr(filename, '/');
    return (p != NULL) ? (p + 1) : ((char *) filename );
}

char * dirname(char *path)
{
    static const char dot[] = ".";
    char *last_slash;

    /* Find last '/'.  */
    last_slash =( (path != NULL) ? strrchr(path, '/') : NULL );

    if ((last_slash != NULL) &&  (last_slash == path ))
    {
        /* The last slash is the first character in the string.  We have to
         return "/".  */
        last_slash++;
    }
    else if ((last_slash != NULL) && (last_slash[1] == '\0'))
    {
        /* The '/' is the last character, return the whole string.  */
        last_slash++;   //last_slash = memchr(path, '/', last_slash - path);        
    }
    else
    {
        print_dbg("");
    }

    if (last_slash != NULL)
    {
        /* Terminate the path.  */
        last_slash[0] = '\0';
    }
    else
    {
        /* This assignment is ill-designed but the XPG specs require to
         return a string containing "." in any case no directory part is
         found and so a static and constant string is required.  */
        path = (char *) dot;
    }
    return path;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值