atoi函数原型
一.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函数原型的更多相关文章
- atoi()函数(转载)
atoi()函数 原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数np ...
- memset和memcpy函数、atoi函数
memset void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c.如下: // 1.将已开辟内存空间s的首n个字节 ...
- atoi()函数
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...
- 题目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]-'; ...
- C语言atoi函数(将字符串转化为int)
头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为:int atoi (const char * str); [函数说明]atoi ...
- C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数
在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...
- 实现字符串转化为整数函数atoi()函数
函数原型: int atoi(const char *nptr); 函数说明: 参数nptr字符串,如果第一个非空格字符存在,并且,如果不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数 ...
- [置顶]
C语言itoa()函数和atoi()函数详解(整数转字符C实现)
头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为: int atoi (const char * str); [函数说明]ato ...
- 算法练习-字符串转换成整数(实现atoi函数)
练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...
随机推荐
- 在 JSDOM v11 中使用jQuery
在JSDOM v11中使用jQuery 从v10开始,jsdom提供了更灵活的API. https://swashata.me/blog/use-jquery-jsdom-v11/ const tes ...
- AngularJS 笔记1
2017-03-23 本文更新链接: http://www.cnblogs.com/daysme/p/6606805.html 什么是 angularjs 2009年有两个外国人创建,后由谷歌收购并开 ...
- web前端知识总结
前言: 一直想着整理一下关于前端的知识体系和资料,工作忙了些,挤挤总会有的,资料很多,就看你能不能耐下心坚持去学了,要多学多敲多想,祝你进步~ 学习之前首先要大概了解什么是HTML ,CSS , JS ...
- Tomcat 跨域问题的解决
先下载CORS对应的Jar: 下载链接: https://download.csdn.net/download/u010739157/10565169 在tomcat的Web.xml中加上如下配置: ...
- 每日质量NPM包事件绑定_bindme(详解React的this)
一.bindme 官方定义: is a helper to bind a list of methods to an object reference 理解: 因为不推荐在render()里构建函数, ...
- Mysql 函数使用记录(三)——UNIX_TIMESTAMP() 、UNIX_TIMESTAMP(date)
参考资料:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_unix-timestamp UN ...
- VMWare16安装windows7遇到的一些问题
本人写这篇博客是为了记录了一些自己在使用VMware16安装Windows7时遇到的一些问题.本人使用的Windows7 ios镜像是小于4g的镜像. Windows7 ios的镜像地址为:https ...
- eclipse中启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误
原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...
- python静态方法、类方法
常规: class Dog(object): def __init__(self,name): self.name=name def eat(self): print('%s is eating'%s ...
- 学习笔记29—Linux基础
一.简单命令: 1.进入文件夹heyi目录:cd ./heyi 2.查看该目录下内容:ls 二.linux压缩和解压缩命令大全 tar命令 解包:tar zxvf FileName.tar 打包:ta ...
