标签:
// 模拟实现库函数的atoi函数 #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")); return 0; } <img src="http://img.blog.csdn.net/20150704145110095?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/46755297