C语言模拟实现库函数atoi
来源:互联网 发布:学汉语翻译软件 编辑:程序博客网 时间:2024/06/05 10:56
atoi() 函数用来将字符串转换成整数(int),其原型为:
int atoi (const char * str);
【函数说明】atoi() 函数会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时(‘\0’)才结束转换,并将结果返回。
【返回值】返回转换后的整型数;如果 str 不能转换成 int 或者 str 为空字符串,那么将返回 0。
#include <stdio.h> #include <string.h> #include <assert.h> #include <ctype.h> int my_atoi(char const *p){ int ret = 0; int a = 0; int flag = 1; assert(p != NULL); while (isspace(*p)) { p++; } while (*p) { if (*p == '+') p++; else if (*p == '-') { p++; flag = -1; } else if (*p >= '0'&& *p <= '9') { a = *p - '0'; ret = (ret * 10 + a); p++; } else return 0; } if ((flag == 1 && ret > 0x7FFFFFFF) || (flag == -1 && ret < (signed int)0x80000000)) return 0; return ret*flag;}int main(){ printf("%d\n", my_atoi(" +2345")); printf("%d\n", my_atoi(" -2345")); printf("%d\n", my_atoi("+2345")); printf("%d\n", my_atoi("-2345")); printf("%d\n", my_atoi("2345")); printf("%d\n", my_atoi("2345")); printf("%d\n", my_atoi("")); printf("%d\n", my_atoi("123ab")); system("pause\n"); return 0;}
阅读全文
1 0
- 模拟实现C语言中的库函数atoi
- C语言模拟实现库函数atoi
- [c语言] 模拟实现c语言库函数atoi
- 【c语言】 模拟实现库函数的atoi函数
- 库函数atoi c语言实现
- 模拟实现库函数atoi
- 模拟实现C语言库函数
- c语言:模拟实现库函数的atoi函数,将字符串转换成整数
- atoi ( )【C语言库函数源代码】
- C语言库函数学习: atoi
- C语言模拟实现atoi函数
- C语言模拟实现atoi函数
- C库函数atoi的实现
- 【C语言】模拟实现库函数srtcpy函数
- 【C语言】模拟实现库函数strcat函数
- 【c语言】模拟实现库函数strcpy函数
- 【c语言】 模拟实现库函数strcat函数
- 【C语言】模拟实现memcpy库函数
- Console用于reverseshell
- C++断点异常
- Datepicker和timerPicker的使用以及监听
- UVA 227
- 新版struts2.5与老版在配置上的异同与注意事项
- C语言模拟实现库函数atoi
- js小知识
- LMS最小均方算法
- java jdbc 两层轻量封装
- 《Unix环境高级编程》 总结 (三)
- bzoj 1012: [JSOI2008]最大数maxnumber
- Laravel基础-中间件
- Mac 下 Less 使用方法
- 《Unix环境高级编程》 总结 (四)