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 ...
随机推荐
- gulp结合webpack开启多页面模式,配置如下
首先老规矩哈.全局包安装先 cnpm install webpack -g cnpm install gulp -g cnpm install babel -g //转换Es6 上面的整合在一起安装可 ...
- website for .Net Core
5 Ways to Build Routing in ASP.NET Core Bundling in .NET Core MVC Applications with BundlerMinifier. ...
- c++中static的用法详解
C 语言的 static 关键字有三种(具体来说是两种)用途: 1. 静态局部变量:用于函数体内部修饰变量,这种变量的生存期长于该函数. int foo(){ static int i = 1; // ...
- 【Ruby】【YAML】
require "YAML" var = YAML.load(File.open('b.yml')) #哈希puts var.class #Hashprint var ," ...
- node.js模块本地代理模块(将自己本机/局域网)服务 代理到外网可以访问
npm 模块 localtunnel模块可以. lt --port 7000 其中7000是本地服务端口 会分配一个动态的url. 局域网中查看本地ip用ifconfg 或者在系统偏好设置里面查看网 ...
- 解决github网站打开慢的问题
一.前言 作为一名合格的程序员,github打开速度太慢怎么能容忍.但是可以通过修改hosts文件信息来解决这个问题.现在chrome访问github速度杠杠的! 二.macOS解决方法 打开host ...
- burp suite 的intruder 四种攻击方式
一:sniper[狙击手] 这种攻击基于原始的请求内容,需要一个字典,每次用字典里的一个值去代替一个待攻击的原始值. 攻击次数=参数个数X字典内元素个数 例如:原始请求中 name=aa , pass ...
- 安装logstash和logstash-input-jdbc
一.安装logstash 1.mac 下直接 brew install logstash 二.安装logstash-input-jdbc 直接在logstash的安装目录bin下运行 ./logsta ...
- 【C#】采用OleDB读取Excel文件转DataTable
using System; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using ...
- C#连接数据库open函数失败
错误信息:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...
