//  模拟实现库函数的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="" />

【c语言】 模拟实现库函数的atoi函数的更多相关文章

  1. 【c语言】模拟实现库函数的atof函数

    // 模拟实现库函数的atof函数 #include <stdio.h> #include <string.h> #include <assert.h> #incl ...

  2. 模拟实现库函数的atoi、atof和itoa

    1.函数atoi atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数.广泛的应用在计算机程序和办公软件中.atoi( ) 函数会扫描参数 nptr字符串 ...

  3. C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数

    在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...

  4. 【C语言】模拟实现库函数strcat函数

    //模拟实现库函数strcat函数 #include <stdio.h> #include <string.h> #include <assert.h> char ...

  5. 【C语言】模拟实现atoi函数

    atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...

  6. C语言提供了几个标准库函数 itoa() atoi()

    C语言提供了几个标准库函数C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <s ...

  7. C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...

  8. C语言itoa()函数和atoi()函数详解(整数转字符)

    http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...

  9. C语言itoa函数和atoi 函数

    C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h>  ...

随机推荐

  1. BKDRHash 算法 php 版本( 可用于 字符串 hash 为int 转)

    <?php function BKDRHash($str) { $seed = 131; // 31 131 1313 13131 131313 etc.. $hash = 0; $cnt = ...

  2. Android插件化原理解析——Hook机制之动态代理

    转自 http://weishu.me/2016/01/28/understand-plugin-framework-proxy-hook/ 使用代理机制进行API Hook进而达到方法增强是框架的常 ...

  3. BZOJ 1975 k短路 A*

    思路: 直接上A* //By SiriusRen #include <queue> #include <cstdio> #include <cstring> #in ...

  4. Java中final、finally、finalize的区别与用法

    1.简单区别:final用于声明属性,方法和类,分别表示属性不可交变,方法不可覆盖,类不可继承.finally是异常处理语句结构的一部分,表示总是执行.finalize是Object类的一个方法,在垃 ...

  5. 测试 Zoundry Raven

    安装很方便,看看发布的内容是否好用 但发现从博客上取下来的内容是有问题的,不能正常打开

  6. 来源页面地址 上一页面url

    Uri uri = Request.UrlReferrer;

  7. Ceph 文件系统的安装

    yum install -y wget wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz#md5=01026f87 ...

  8. ASP.NE 上传文件控件

    protected void Button1_Click(object sender, EventArgs e) { //if (Request["id"]==null & ...

  9. day37-1 面向对象高阶

    目录 面向对象高阶 isinstance issubclass 反射(自省) 模块的使用 放在类的使用 call 面向对象高阶 isinstance 判断是否为实例化对象,以后可以用来取代type 和 ...

  10. EL截取url中参数

    function getUrlString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*) ...