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. cmd执行mssql脚本或者执行mysql脚本

    private static int ExecuteMSSql(DbInfo db, string sqlPath) { Console.WriteLine("=============== ...

  2. Android电源管理-休眠简要分析

    一.开篇 1.Linux 描述的电源状态 - On(on)                                                 S0 -  Working - Standb ...

  3. Android HTTPS(3) IOException: Hostname 解决方案

    Common Problems with Hostname Verification As mentioned at the beginning of this article, there are ...

  4. BZOJ 2115 Xor(抑或值最大路径)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2115 题意:给出一个带权无向图.求一条1到n的路径使得路径上权值的抑或值最大? 思路:( ...

  5. Entity Framework Architecture

    http://www.entityframeworktutorial.net/EntityFramework-Architecture.aspx The following figure shows ...

  6. 瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)(一)

    在这炮火连天.技术更新迅猛的年代,不接触了解.甚至会用2~3种框架都不好意思说自己有多少年工作经验.况且出去面试也会有点吹牛的底子嘛. 这次折腾了NHibernate.其实这些ORM框架封装好了都是给 ...

  7. UVa 1608 (分治 中途相遇) Non-boring sequences

    预处理一下每个元素左边和右边最近的相邻元素. 对于一个区间[l, r]和区间内某一个元素,这个元素在这个区间唯一当且仅当左右两边最近的相邻元素不在这个区间内.这样就可以O(1)完成查询. 首先查找整个 ...

  8. HDU 2897 (博弈 找规律) 邂逅明下

    根据博弈论的两条规则: 一个状态是必胜状态当且仅当有一个后继是必败状态 一个状态是必败状态当且仅当所有后继都是必胜状态 然后很容易发现从1开始,前p个状态是必败状态,后面q个状态是必胜状态,然后循环往 ...

  9. singleton单例模式

    单例设计模式 单例设计模式概述    单例模式就是要确保类在内存中只有一个对象,该实例必须自动创建,并且对外提供    优点:    在系统内存中只存在一个对象,因此可以解决系统资源,对于一些需要频繁 ...

  10. HDU 2577 How to Type (DP,经典)

    题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...