自己实现的atoi函数
来源:互联网 发布:淘宝 宝贝 排名 编辑:程序博客网 时间:2024/06/08 04:41
不多说,直接上代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int _atoi(const char *str, int len)
{
if(NULL == str)
{
perror("input string is NULL");
return 0;
}
int value = 0;
int sign = 1;
const char *ptr = str;
const char *end = str + len;
//跳过开头的空白符
while((ptr < end) && isspace(*ptr))
{
++ptr;
}
if( ptr == end )
{
perror("all character is space");
return 0;
}
//符号位判断,只要带"-"就是负数,带"+",不带符号或其他的都认为是1
sign = ('-' == *ptr) ? -1 : 1;
//带符号位的需要跳过符号位
if('-' == *ptr || '+' == *ptr)
{
++ptr;
}
while(ptr < end)
{
if(*ptr < '0' || *ptr > '9')
{
break;
}
value = value * 10 + *ptr - '0';
++ptr;
}
if(-1 == sign)
{
value = -value;
}
return value;
}
int main( int argc, char *argv[] )
{
if(argc > 1){
printf("%d\n",_atoi(argv[1], strlen(argv[1])));
}
return 0;
}
- 自己实现的atoi函数
- 自己实现atoi函数
- 自己实现atoi函数
- 自己实现atoi系列(atoi函数)
- 自己设计的atoi函数
- 自己写的atoi函数
- 字符串string转整数int 实现自己的atoi函数
- 自己编写代码实现atoi和itoa函数的功能
- 自己编写代码实现atoi和itoa函数的功能
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项 .
- Android系统启动过程
- USBFiletr 总结
- C++ new和delete重载
- andorid采用post传输文件到自己写的服务器
- 自己实现的atoi函数
- lua loadstring与loadfile
- toupper()函数
- 单链表逆置
- Android游戏框架Libgdx使用入门
- 习近平参观海淀民族小学 参加入队仪式
- POJ 2594 Treasure Exploration(匈牙利算法—最小路径覆盖 + floyd)
- csdn博客 代码块的显示设置以及图片的插入技巧
- Highcharts如何隐藏导出按钮和网址脚标