1. // 模拟实现库函数的atoi函数
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <ctype.h>
  7.  
  8. int my_atoi(char const *p)
  9. {
  10. int ret = 0;
  11. int a = 0;
  12. int flag = 1;
  13. assert(p != NULL);
  14. while (isspace(*p))
  15. {
  16. p++;
  17. }
  18. while (*p)
  19. {
  20. if (*p == '+')
  21. p++;
  22. else if (*p == '-')
  23. {
  24. p++;
  25. flag = -1;
  26. }
  27. else if (*p >= '0'&& *p <= '9')
  28. {
  29. a = *p - '0';
  30. ret = (ret * 10 + a);
  31. p++;
  32. }
  33. else
  34. return 0;
  35. }
  36. if ((flag == 1 && ret > 0x7FFFFFFF) || (flag == -1 && ret < (signed int)0x80000000))
  37. return 0;
  38. return ret*flag;
  39. }
  40.  
  41. int main()
  42. {
  43. printf("%d\n", my_atoi(" +2345"));
  44. printf("%d\n", my_atoi(" -2345"));
  45. printf("%d\n", my_atoi("+2345"));
  46. printf("%d\n", my_atoi("-2345"));
  47. printf("%d\n", my_atoi("2345"));
  48. printf("%d\n", my_atoi("2345"));
  49. printf("%d\n", my_atoi(""));
  50. printf("%d\n", my_atoi("123ab"));
  51. return 0;
  52. }
  53.  
  54. <img src="http://img.blog.csdn.net/20150704145110095?
  55.  
  56. 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. 联想Thinkpad L460安装Win7 64位

    单位发了L460,自带的系统为win10,但是涉及到很多工作以及客户都是在win7环境下,所以必须安装win7的系统,经过一番折腾,终于装好了. 主要顺序如下: 1,制作WINPE启动盘,如大白菜,老 ...

  2. 一、SQL系列之~使用SQL语言导出数据及实现定时导出数据任务

    一般情况下,SQL数据库中带有导入与导出数据的直接按键操作,点击数据表所在的数据库--任务--导出/导入数据,根据导入/导出向导直接将数据导出即可. 但导出的数据格式多为Excel格式,如果需要导出的 ...

  3. Kettle环境变量配置

    KETTLE_DIR=安装目录 KETTLE_HOME=安装目录 安装目录比如:D:\Kettle\pdi-ce-6.0.0.0-353\data-integration

  4. 常见文件MIME类型

    常见文件MIME类型.asx,video/x-ms-asf .xml,text/xml .tsv,text/tab-separated-values .ra,audio/x-pn-realaudio ...

  5. CentOS6 在线安装PostgreSQL10

    本文主要通过实际案例介绍如何在CentOS6环境中在线安装PostgreSQL10,安装环境需具备能够使用yum在线安装功能.具体安装步骤如下, 1 下载对应版本的PGDG文件 从https://yu ...

  6. [编码]ASCII、GBK、Unicode(万国码) 和 UTF-8

    American ASCII编码 (American Standard Code for Information Interchange,美国信息互换标准代码)  China    gbk编码     ...

  7. vue-pdf的使用方法及解决在线打印预览乱码

    最近在用vue做项目的时候,页面中需要展示后端返回的PDF文件,于是便用到了vue-pdf,其使用方法为 : npm install --save vue-pdf 官网地址:https://www.n ...

  8. border使用

    border属性 border-width  border-style  border-color  inherit border-style的值:none  dotted(点线)  dashed(虚 ...

  9. jboss-as-7.1.1.Final配置Jndi数据源(以mysql为例)

    1.获取mysql驱动,可以从mysql官方网站下载: http://dev.mysql.com/downloads/connector/j/ 2.进入jboss-as-7安装目录下的modules目 ...

  10. div 背景放图和直接放图区别

    <html> <head> <meta charset="UTF-8"> <title></title> <sty ...