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 ...
随机推荐
- Mybatis的分页插件com.github.pagehelper
1. 需要引入PageHelper的jar包 如果没有使用maven,那直接把jar包导入到lib文件夹下即可,这个PageHelper插件在github上有开源, 地址为:https://githu ...
- Redis源码分析(skiplist)
源码版本: redis-4.0.1 源码位置: server.h :zskiplistNode和zskiplist的数据结构定义. t_zset.c: 以zsl开头的函数是SkipList相关的操作函 ...
- Android-ION内存管理简介
ION内存管理简介 https://www.jianshu.com/p/4f681f6ddc3b http://kernel.meizu.com/memory%20management%20-%20i ...
- 第一课 Dubbo背景及原理
1 . 技术背景 Dubbo每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点. Dubbo是一个阿里巴巴开源出来的一个分布式服务框架,致力于 ...
- 第12组 Alpha冲刺 (1/6)
过去两天完成了哪些任务 文字描述 静态页面代码编写以及一些点击事件 展示GitHub当日代码/文档签入记录 接下来的计划 1.继续学习echarts 2.编写所需要的图表代码 还剩下哪些任务 1.图表 ...
- C#生成新浪微博短网址 示例源码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DotN ...
- CentOS 设置网络及安装 ifconfig
centos使用yum报错"Could not resolve host: mirrorlist.centos.org; 未知的错误" 先用NetworkManager包的nmcl ...
- Numpy (嵩老师.)
import numpy as np Numpy 一元函数 对ndarray中的数据执行元素级运算的函数 np.abs(x) np.fabs(x) 计算数组各元素的绝对值 np.sqrt(x) 计算数 ...
- 在cmd中使用vim编译器
下载地址:http://www.vim.org/download.php#pc 下载GVIM,配置下path环境变量就可以在cmd中使用vim了 把vim.exe复制一份,更名为vi.exe,就可以直 ...
- 在安卓开发中需要格式化桌面icon图标
使用以下在线工具即可实现http://www.makeicon.cc/home/index