atoi()函数的实现
来源:互联网 发布:java游戏编程下载 编辑:程序博客网 时间:2024/04/29 10:56
atoi()函数是在笔试面试中经常考到的一个题目,主要考察是,int的溢出,字符过滤等问题下面是具体实现:
#define INT_MAX = 32767#define INT_MIN=-32768int atoi(const char * str){ if(!str) return 0; int result = 0; int sign = 1; //提取+ -号 if(*str == '-' || *str == '+') { if(*str = ='-') { sign = -1; } str++; } while('0'<*str && *str<'9') { int digit = *str - '0'; if(result > INT_MAX || (sign > 0?(reslut == INT_MAX/10 &&digit > INT_MAX%10):(result == (unsigned int)(INT_MAX+1)/10 && digit > (unsigned int)(INT_MAX+1) % 10))){ return sign>0? INT_MAX : INT_MIN;} result = result*10+digit;str++; } return result*sign;}注意整数范围,如果是有符号的整数,最大的时候加1就变成最小负数了,最小负数加1就变成最大负数了。、。。。
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- 单实例设计模式的实现
- win7共享xp打印机和文件解决方法(图解)
- C语言之位域小记
- zoj 1115 Digital Roots
- 1-3
- atoi()函数的实现
- MQ setup in websphere
- 第5周实验报告任务1.3
- 关闭Windows 2003/2008中IE增强的安全配置的方法
- HIFI耳机基础知识入门
- 黑马程序员-序列化
- 绿皮书再版!ECMAScript 5th有讲呵!
- 数据结构---栈实现
- C++构造函数