myatoi
atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中。int atoi(const char *nptr) 函数会扫描参数 nptr字符串,会跳过前面的空白字符(例如空格,tab缩进)等。如果 nptr不能转换成 int 或者 nptr为空字符串,那么将返回0。特别注意,该函数要求被转换的字符串是按十进制数理解的。atoi输入的字符串对应数字存在大小限制(与int类型大小有关),若其过大可能报错-1。
实现思路:只需一次遍历,将其转化为十进制整型数据。首先定位字符串第一个非空格字符,检查其是否为“-”“+”号,若是,则确定其正负值。然后指针后跳,将字符转为整型(记得减去‘0’才是数字的真实值),乘10在加即可。
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std; class Myatoi
{
public:
int func(const char* str);
}; int Myatoi::func(const char* str)
{
if(strlen(str) == 0)
return 0;
int a = 0, flag = 1; while(str[0] == ' ')
++str; if(str[0] == '-')
{
flag = -1;
++str;
}
else if(str[0] == '+')
{
flag = 1;
++str;
}
while(str[0]>='0' && str[0]<='9')
{
a *= 10;
a += (int)(str[0] - '0');
if(a > 0x80000000)
{
cout<<"Overflow! ";
return -1;
}
++str;
}
return flag*a; } int main()
{
char p[3][20] = {"-12345.678","+132342wr","999999999999999999"}; Myatoi ai;
for(int i=0; i<3; ++i)
{
cout<<"p["<<i<<"] = "<<p[i]<<" myatoi(p["<<i<<"]) = "<<ai.func(p[i]);
cout<<" "<<atoi(p[i])<<endl;
}
return 0;
}
结果如下

看起来在数字过大溢出时,标准atoi并没有正确报错-1。
myatoi的更多相关文章
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- python leetcode 1
开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...
- LeetCode Note 1st,practice makes perfect
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- No.008:String to Integer (atoi)
问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- C库函数使用与总结之字符串处理函数
1. memcpy(拷贝内存内容) [头文件]#include <string.h> [函数原型]void *memcpy(void *dest, const void *src, siz ...
- LeetCode 7 -- String to Integer (atoi)
Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
- [LeetCode] 8. String to Integer (atoi)
Implement atoi to convert a string to an integer. public class Solution { public int myAtoi(String s ...
随机推荐
- docker创建本地主机实例Virtualbox 驱动出错
宿主机系统:Centos7 64位 创建主机实例Virtualbox 命令:docker-machine create -d virtualbox test 连接centos工具:Finalshell ...
- iscsi基本命令
磁阵操作命令 根据磁阵端配置的业务地址(targetIP)和端口(3260),命令iscsiadm -m discovery -t sendtargets -p targetIP:port(3260) ...
- linux&c 进程控制 课后习题
(声明:本篇博客只是博主自己的理解,加以整理,目的是总结刚学过的进程知识,不一定绝对正确,非常愿意听客官您提出宝贵意见.) Q1:进程中的全局数据段(全局变量),局部数据段(局部变量),静态数据段的分 ...
- 【接口】SpringBoot+接口开发(一)
一.接口的简单介绍 1.什么是接口:接口及服务: 2.接口的分类:(1)系统的内部接口;(2)第三方的外部接口; 3.简述接口原理图: 4.接口协议:是指客户端跟服务器之间或者接口与接口间进行的通讯时 ...
- vs Code配置C++运行和调试环境以及相关问题
vs Code配置C++运行和调试环境以及相关问题 第一步:下载c++插件 第二步:安装编译.调试环境 如果没有Dev-C++下载MinGW 下载地址:https://sourceforge.net/ ...
- 使用grep命令,玩转代码审计寻找Sink
好久没分享东西了,今天分享个实用代码审计技巧 使用grep,玩转代码审计,适用于linux/mac,windows需要另行安装grep: 使用场景如下:快速寻找项目中所有的Sink,快速寻找符合适配条 ...
- java中使用Process执行linux命令
代码如下 BufferedReader reader = null; String cmd = "netstat -anp|grep :8080";//命令中有管道符 | 需要如下 ...
- JAVA线上常见问题排查手段(小结)
在平时开发过程中,对于线上问题的排查以及系统的优化,免不了和Linux进行打交道.每逢大促和双十一,对系统的各种压测性能测试,优化都是非常大的一次考验.抽空整理了一下自己在线上问题排查以及系统优化的一 ...
- Part 1 to 10 Basic in C#
Part 1 Introduction The struct of C# program: namespace , class and Main method what is namespace? t ...
- 使用PAM模块实现普通用户之间su免密切换
参考自:Allow user1 to "su - user2" without password https://unix.stackexchange.com/questions/ ...