一.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. (转载)Rime输入法—鼠须管(Squirrel)词库添加及配置

    为什么用Rime 13年底的时候,日本爆出百度的日本版本输入法的问题,要求政府人员停用,没当回事,反正我没用,当然了,有关搜狗和用户隐私有关的问题就一直没有中断过,也没太在意.但,前几天McAfee爆 ...

  2. Druid介绍

    Druid (大数据实时统计分析数据存储) Druid 是一个为在大数据集之上做实时统计分析而设计的开源数据存储.这个系统集合了一个面向列存储的层,一个分布式.shared-nothing的架构,和一 ...

  3. [转载]grep查看上下文及简单正则表达式

    转载自:https://www.cnblogs.com/mfryf/p/3336288.html inux grep 显示前后几行的信息2016年03月02日 14:10:58 ChenHui246 ...

  4. webpack插件配置(一) webpack-dev-server 路径配置

    本文的路径配置主要涉及到webpack.config.js文件中devServer与output两个选项的配置 webpack-dev-server定义 webpack-dev-server主要是启动 ...

  5. IntelliJ IDEA java selenium

    // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...

  6. CentOS6.5使用yum快速搭建LAMP环境

    1.安装Apache # yum -y install httpd # 开机自启动 # chkconfig httpd on # 启动httpd 服务 # service httpd start # ...

  7. 使用openpyxl实现excel文件的读取操作

    1.环境准备 python3环境.安装openpyxl模块 2.excel文件数据准备 3.为方便直接调用,本代码直接封装成类 from openpyxl import load_workbook c ...

  8. pom.xml文件

    最近在了解maven创建的工程,拿到服务器的一段代码一直报错,是maven的pom.xml文件出错了,但是不知道是什么原因,所以就想知道pom.xml文件的作用及内容. 什么是POM? POM是项目对 ...

  9. es6中export和export default的区别

    export与export default均可用于导出常量.函数.文件.模块 你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使用 ...

  10. 关于fstream、ifstream、ofstream读写文本文件、二进制文件详解

    fstream.ifstream.ofstream是c++中关于文件操作的三个类 fstream类对文件进行读操作和写操作 打开文件 fstream fs("要打开的文件名",打开 ...