atoi函数的用法
来源:互联网 发布:人工智能泰迪熊 编辑:程序博客网 时间:2024/05/24 05:21
就自己写写代码(根据atoi()的功能)来表示atoi()函数的实现。我在这里先把atoi()函数的功能贴出来,也好有个参考啊~~~
运行结果:
atoi()函数的功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。
atoi()函数实现的代码:
- /*
- * name:xif
- * coder:xifan@2010@yahoo.cn
- * time:08.20.2012
- * file_name:my_atoi.c
- * function:int my_atoi(char* pstr)
- */
- int my_atoi(char* pstr)
- {
- int Ret_Integer = 0;
- int Integer_sign = 1;
- /*
- * 判断指针是否为空
- */
- if(pstr == NULL)
- {
- printf("Pointer is NULL\n");
- return 0;
- }
- /*
- * 跳过前面的空格字符
- */
- while(isspace(*pstr) == 0)
- {
- pstr++;
- }
- /*
- * 判断正负号
- * 如果是正号,指针指向下一个字符
- * 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符
- */
- if(*pstr == '-')
- {
- Integer_sign = -1;
- }
- if(*pstr == '-' || *pstr =='+')
- {
- pstr++;
- }
- /*
- * 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer
- */
- while(*pstr >= '0' && *pstr <='9')
- {
- Ret_Integer = Ret_Integer * 10 + *pstr - '0';
- pstr++;
- }
- Ret_Integer = Integer_sign * Ret_Integer;
- return Ret_Integer;
- }
现在贴出运行my_atoi()的结果,定义的主函数为:int main ()
- int main()
- {
- char a[] = "-100";
- char b[] = "456";
- int c = 0;
- int my_atoi(char*);
- c = atoi(a) + atoi(b);
- printf("atoi(a)=%d\n",atoi(a));
- printf("atoi(b)=%d\n",atoi(b));
- printf("c = %d\n",c);
- return 0;
- }
运行结果:
- atoi函数的用法
- atoi() 与 itoa()函数的用法
- atoi 等相关函数的用法
- atoi(s)函数用法
- itoa函数和atoi函数的实现和用法
- atoi() 与 itoa()函数用法
- atoi() 与 itoa()函数用法
- atoi() 与 itoa()函数用法
- ATOI的用法
- atoi的用法
- vc++ C函数atoi和itoa的用法总结(转载)
- C语言中itoa和atoi函数的用法
- 学习笔记——atoi函数的用法及用C语言实现atoi
- 学习笔记——atoi函数的用法及用C语言实现atoi
- 学习笔记——atoi函数的用法及用C语言实现atoi
- atoi函数的实现
- atoi函数的学习
- 奇怪的atoi函数!
- Daily-C-Study(13):c语言变量及命名
- 同济大学软件学院万金友院长谈嵌入式(学习与规划)
- 多级页表如何节省进程页表空间
- 职业规划
- 正则表达式语法
- atoi函数的用法
- C++ UpdateData()
- 组播VLAN配置
- VC调试
- android 图片加载方式
- webpy 简单表单一粒
- hdu 1997 汉诺塔VII
- NSTimer 控制 UIScrollView的定时滚动
- 诺顿卸载密码的问题