一.atoi()函数的功能:

1.定义: 将字符串转换成整型数,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')结束转化,并将结果返回(返回转换后的整型数)。

2.头文件: #include <stdlib.h>

3.函数原型:int atoi(const char *nptr);

4.实现时需要注意的问题:
(1)检查字符串是否为空,为空则返回
(2)对空格和规定字符的处理, 若是,则跳过,不影响接下来的处理(很多人的处理忘了规定字符这回事..)
规定字符 具体作用
\n 换行
\f 清屏并翻页
\r 回车
\t 制表符

  结果仍然为11

(3)正负号(+-)的处理

(4)结束条件:遇到非数字或者字符'\0'结束
(5)溢出处理,正溢出返回int上限(2147483647),负溢出返回int下限
int main(void)
{
char *a="-2147483650";
int b=atoi(a);
printf(" %d",b);
return ;
}
输出结果为-2147483648

    char *a="";
int b=atoi(a);
printf(" %d",b);
return ;
输出结果为2147483647

代码如下:

#include<stdio.h> 
#include<iostream>
using namespace std;
enum error{correct,incorrect,overflow,null};
int error=correct;//默认是正确的
int myatoi(const char *str)
{
int flag=; if(str==NULL)
{
error=null;
return ; }
while(*str==' '||*str=='\t'||*str=='\f'||*str=='\r'||*str=='\n')
str++;
if(*str=='-')//负数的处理,直接让计算得出的数乘-1即可
{
flag=-;
str++;
}
else if (*str=='+')//注意对正负号我用的不是循环,正负号只有作为第一个字符出现才是合法的
str++;
int num=;
while(*str!='\0')//*str=='\0'也是结束条件之一
{
if(*str>=''&&(*str<=''))
{
num=num *+*str-'';//注意是‘0‘
     //溢出处理稍后贴上
str++;
}
else
{
error=incorrect;//非法
break;
}
}
return num*flag;
}
int main()
{ char str[];
printf("请输入字符串:");
gets(str);
int result = myatoi(str);
switch(error){
case correct:
cout << "字符串" << str << " 对应的整数是:" << result <<endl;
break;
case null:
cout << "空指针错误" <<endl;
break;
case incorrect:
cout << "输入的字符串中有非数字字符" <<endl;
cout << result <<endl;
break;
case overflow:
cout << "输入的字符串对应的数字使得Int类型溢出" <<endl;
cout << result <<endl;
}
return ;
}
												

atoi函数原型的更多相关文章

  1. atoi()函数(转载)

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

  2. memset和memcpy函数、atoi函数

    memset void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c.如下: // 1.将已开辟内存空间s的首n个字节 ...

  3. atoi()函数

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

  4. 题目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]-'; ...

  5. C语言atoi函数(将字符串转化为int)

    头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为:int atoi (const char * str); [函数说明]atoi ...

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

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

  7. 实现字符串转化为整数函数atoi()函数

    函数原型: int atoi(const char *nptr); 函数说明: 参数nptr字符串,如果第一个非空格字符存在,并且,如果不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数 ...

  8. [置顶] C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为: int atoi (const char * str); [函数说明]ato ...

  9. 算法练习-字符串转换成整数(实现atoi函数)

    练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...

随机推荐

  1. 论文笔记之:Deep Attributes Driven Multi-Camera Person Re-identification

    Deep Attributes Driven Multi-Camera Person Re-identification 2017-06-28  21:38:55    [Motivation] 本文 ...

  2. Derek解读Bytom源码-孤块管理

    作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...

  3. Python多线程爬虫

    前言 用上多线程,感觉爬虫跑起来带着风 运行情况 爬取了9万多条文本记录,耗时比较短,一会儿就是几千条 关键点 多个线程对同一全局变量进行修改要加锁 # 获取锁,用于线程同步 threadLock.a ...

  4. HTML基本内容

    设置背景色:<body bgcolor="#AAAAAA">,设置背景图:<body background="1.png">. 颜色的知 ...

  5. 恢复Intellij idea删除的文件

    恢复Intellij idea的删除文件方法: 右键单机项目名称---->Local History---->Show History 可以看到历史操作记录,右键单机想要恢复的文件---- ...

  6. 下载安装tomcat和jdk,配置运行环境,与Intellij idea 2017关联

    第一篇博客,最近公司要用java和jsp开发新的项目,第一次使用Intellij idea 2017,有很多地方需要一步步配置,有些按照网上的教程很快就配置好了,有的还是琢磨了一会儿,在这里做一个记录 ...

  7. Qt Model/View学习(二)

    Model和View的搭配使用 DEMO pro文件 #------------------------------------------------- # # Project created by ...

  8. JS self=this

    1.每个函数都会有自己的this和arguments:this对象绑定运行环境,arguments绑定调用参数. 2.全局函数:this和全局环境绑定,浏览器指向全局window对象(node.js中 ...

  9. Lua和C++交互 学习记录之四:全局table交互

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...

  10. mac软件

    1. http://www.ifunmac.com/ 2.Mac安装软件时提示已损坏的解决方法 http://www.jianshu.com/p/3d04a2292fcd 3.mac以后有时间在装的软 ...