atoi函数简单实现
来源:互联网 发布:软件开发人员等级 编辑:程序博客网 时间:2024/05/22 02:27
1. 看一下atoi函数的要求,通过 man 3 atoi。
原型
#include
int aoti(const char *str);
描述
The atoi() function converts the initial portion of the string pointed to bystr toint representation.
atoi函数把str字符串初始有效部分转换成int类型。
因为atoi返回int,出错返回-1是不可取的,因为-1也是有效的,返回0,同样不可取。但是根据其描述,如果str完全无效那么返回的应该是0,否则转换其初始有效部分。
知道了这些,基本就可以实现了。
标准库实现是调用strol,然后调用strtoul。可以识别八进制,十六进制字符。
IMPLEMENTATION NOTES The atoi() function is not thread-safe and also not async-cancel safe. Theatoi() function has been deprecated bystrtol()and should not be used in new code.
原型
#include
int aoti(const char *str);
描述
The atoi() function converts the initial portion of the string pointed to bystr toint representation.
atoi函数把str字符串初始有效部分转换成int类型。
因为atoi返回int,出错返回-1是不可取的,因为-1也是有效的,返回0,同样不可取。但是根据其描述,如果str完全无效那么返回的应该是0,否则转换其初始有效部分。
知道了这些,基本就可以实现了。
点击(此处)折叠或打开
- #include
- int
- my_atoi(const char *str)
- {
- int result;
- char sign;
- for (; str && isspace(*str); ++str)
- ; /* 跳过空白,换行,tab*/
-
- if (!str)
- return 0;
- sign = *str == '+' || *str == '-' ? *str++ : '+'; /* 处理符号 */
- for (result = 0; str && isdigit(*str); ++str) /*转换有效部分 */
- result = result * 10 + *str - '0'; /* FIXME: 没有考虑溢出 */
- return (sign == '+' ? result : -result);
- }
标准库实现是调用strol,然后调用strtoul。可以识别八进制,十六进制字符。
IMPLEMENTATION NOTES The atoi() function is not thread-safe and also not async-cancel safe. Theatoi() function has been deprecated bystrtol()and should not be used in new code.
0 0
- atoi函数简单实现
- atoi函数的简单实现
- atoi函数的简单实现
- 简单的实现atoi函数
- 简单的实现atoi函数源代码
- C语言 atoi函数简单实现
- atoi函数和itoa函数的简单实现
- my atoi 简单实现
- atoi的简单实现
- atoi函数实现
- atoi,itoa函数实现
- atoi函数的实现
- atoi()函数的实现
- atoi函数实现
- atoi函数实现
- atoi函数实现
- atoi函数实现_buwanmei
- 实现atoi函数
- c风格的字符串
- C语言atoi()函数:将字符串转换成int(整数)
- 一口一口吃掉Hibernate(八)——Hibernate中inverse的用法
- FPGA基础知识8(FPGA静态时序分析)
- UEFI启动的windows下安装Ubuntu双系统(绝对可行)
- atoi函数简单实现
- iOS 给控件 画边框
- Sphinx 在 windows 下安装使用
- 非常好理解的遗传算法的例子
- Objective-C——深复制和浅复制
- Dubbo详细介绍与安装使用过程
- 田忌赛马
- 拓扑排序
- cgi详解