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 ...
随机推荐
- Docker1之Container
Document An image is a lightweight, stand-alone, executable package that includes everything needed ...
- HDU-6033 Add More Zero
There is a youngster known for amateur propositions concerning several mathematical hard problems. N ...
- _itemmod_exchange_item
物品强化说明 表说明 `item`物品ID `exchangedItem` 升级后的物品ID `upFlag` 物品等级模式0或1,一般为0 `reqId` 需求ID `chance` 升级几率 `c ...
- unity shader base pass and additional pass
[Unity Shaders]Shader中的光照,shadersshader 写在前面 自己写过Vertex & Fragment Shader的童鞋,大概都会对Unity的光照痛恨不已 ...
- 用html+css+js实现选项卡切换效果
文章转载自:http://tongling.github.io/JSCards/ 用html+css+js实现选项卡切换效果 使用之前学过的综合知识,实现一个新闻门户网站上的常见选项卡效果: 文字素材 ...
- tornado关于AsyncHTTPClient的使用笔记
先来一段同步的httpclient使用代码 url = 'https://www.baidu.com/' http_client = HTTPClient() response = http_clie ...
- JDBC连接数据库的安全性连接方法
PreparedStatement ps=null; ResultSet rs=null; Connection ct=null; try { Class.forName("com.mysq ...
- eclipse打开失败
以前eclipse运行好好的,某一次运行启动不了,一直图标那里转圈,不能启动, 运行eclipsec.exe后,查看发现出现以下错误 SLF4J: Class path contains multip ...
- Qt Model/View学习(二)
Model和View的搭配使用 DEMO pro文件 #------------------------------------------------- # # Project created by ...
- poi实现百万级数据导出
注意使用 SXSSFWorkbook 此类在构造表格和处理行高的时候效率极高,刚开始时我使用的 XSSFWorkbook 就出现构造表格效率极低,一万行基本需要3秒左右,那当导出百万级数据就慢的要死啦 ...
