atoi()函数的功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。

atoi()函数实现的代码:

  1. /*
  2. * name:xif
  3. * coder:xifan@2010@yahoo.cn
  4. * time:08.20.2012
  5. * file_name:my_atoi.c
  6. * function:int my_atoi(char* pstr)
  7. */
  8. int my_atoi(char* pstr)
  9. {
  10. int Ret_Integer = 0;
  11. int Integer_sign = 1;
  12. /*
  13. * 判断指针是否为空
  14. */
  15. if(pstr == NULL)
  16. {
  17. printf("Pointer is NULL\n");
  18. return 0;
  19. }
  20. /*
  21. * 跳过前面的空格字符
  22. */
  23. while(isspace(*pstr) == 0)
  24. {
  25. pstr++;
  26. }
  27. /*
  28. * 判断正负号
  29. * 如果是正号,指针指向下一个字符
  30. * 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符
  31. */
  32. if(*pstr == '-')
  33. {
  34. Integer_sign = -1;
  35. }
  36. if(*pstr == '-' || *pstr == '+')
  37. {
  38. pstr++;
  39. }
  40. /*
  41. * 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer
  42. */
  43. while(*pstr >= '0' && *pstr <= '9')
  44. {
  45. Ret_Integer = Ret_Integer * 10 + *pstr - '0';
  46. pstr++;
  47. }
  48. Ret_Integer = Integer_sign * Ret_Integer;
  49. return Ret_Integer;
  50. }

现在贴出运行my_atoi()的结果,定义的主函数为:int  main  ()

  1. int main()
  2. {
  3. char a[] = "-100";
  4. char b[] = "456";
  5. int c = 0;
  6. int my_atoi(char*);
  7. c = atoi(a) + atoi(b);
  8. printf("atoi(a)=%d\n",atoi(a));
  9. printf("atoi(b)=%d\n",atoi(b));
  10. printf("c = %d\n",c);
  11. return 0;
  12. }

atoi()函数的实现的更多相关文章

  1. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

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

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

  3. 题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用

    #include<stdio.h> #include<stdlib.h> int sw(char *a){ ,c=; while(a[i]){ ') c=c*+a[i]-'; ...

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

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

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

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

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

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

  7. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  8. atoi()函数(转载)

    atoi()函数 原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数np ...

  9. 源码实现 --> atoi函数实现

    atoi函数实现 atoi()函数的功能是将一个字符串转换为一个整型数值. 例如“12345”,转换之后的数值为12345,“-0123”转换之后为-123. #include <stdio.h ...

随机推荐

  1. URAL 1097 Square Country 2 离散化

    一共才100个正方形,将所有正方形左下角和右上角的X坐标和Y坐标离散化,直接枚举新建公园的点的坐标即可. O(n^3)的时间复杂度. #include <cstdio> #include ...

  2. javascript 简繁转换

    js 简繁转换 function copy(ob) { var obj=findObj(ob); if (obj) { obj.select();js=obj.createTextRange();js ...

  3. 运行javascript的方式

    1.放在超链接中: <a href="javascript:alert('aaaa')" >Test</a> 2.直接加载 <script type= ...

  4. [原]最短路专题【基础篇】(updating...)

    hud1548 a strange lift  最短路/bfs  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:一个奇怪的电梯,每层楼的 ...

  5. Hook入门

    Hook入门 2014-07-24 基本概念 Windows消息机制 Hook(钩子) 运行机制 核心函数 C# hook示例 基本概念[1] Windows消息机制[5] Windows操作系统是建 ...

  6. JSON 之 SuperObject(4): 增、删、改

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  7. php如何控制用户对图片的访问 PHP禁止图片盗链(转载)

    把images目录设置成不充许http访问(把图片目录的:读取.目录浏览 两个权限去掉). 用一个PHP文件,直接用file函数读取这个图片.在这个PHP文件里进行权限控制. apache环境中,在你 ...

  8. UVa 12100 (模拟) Printer Queue

    用一个队列模拟,还有一个数组cnt记录9个优先级的任务的数量,每次找到当前最大优先级的任务然后出队,并及时更新cnt数组. #include <iostream> #include < ...

  9. 多线程-NSOperation中使用ASIHttpRequest注意事项

    最近做的iPhone项目中有一如下功能: app在用户许可后将本地Photos的照片上传到服务器,期间用户可以做其他任何操作,等上传成功后弹出一个toast通知用户. 原先的代码结构是: 获取照片的操 ...

  10. web.xml 中的listener、 filter、servlet 加载顺序

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...