将字符串转换成对应的整数(简单版,后续会出复杂版的)
2016-04-04 10:09
274 查看
#include <stdio.h> #include <assert.h> #include <stdlib.h> int my_atoi(const char *str) { assert(str); int flag = 1; int ret = 0; while (isspace(*str)) { str++; } if (*str == '-') { flag = -1; } if (*str == '+' || *str == '-') { str++; } while (*str) { ret = ret * 10 + (*str - '0'); str++; } return ret*flag; } int main() { char *str = "-12384"; int ret = my_atoi(str); printf("%d\n", ret); return 0; }
相关文章推荐
- 关于PHP浮点数你应该知道的(All 'bogus' about the float in PHP)
- C#实现移除字符串末尾指定字符的方法
- C语言中字符的输入输出以及计算字符个数的方法详解
- php按字符无乱码截取中文的方法
- 统计输入字符各个字母出现频率的解题思路
- 一个JavaScript处理textarea中的字符成每一行实例
- JS去掉第一个字符和最后一个字符的实现代码
- 基于jQuery实现文本框只能输入数字(小数、整数)
- jquery限定文本框只能输入数字(整数和小数)
- Juery解决tablesorter中文排序和字符范围的方法
- JS中字符问题(二进制/十进制/十六进制及ASCII码之间的转换)
- Javascript正则控制文本框只能输入整数或浮点数
- PHP中将字符串转化为整数(int) intval() printf() 性能测试
- ASP中限制某些字符留言评论
- js整数字符串转换为金额类型数据(示例代码)
- 如何判断一个整数的二进制中有多少个1
- php中删除字符串中最先出现某个字符的实现代码
- IP地址与整数之间的转换实现代码(asp.net)
- js字符编码函数区别分析
- js判断是否含有GBK以外的字符的函数