字符串转成整型(int)
1 题目
Implement atoito convert a string to an integer.
Hint: Carefullyconsider all possible input cases. If you want a challenge, please do not seebelow and ask yourself what are the possible input cases.
Notes: It isintended for this problem to be specified vaguely (ie, no given input specs).You are responsible to gather all the input requirements up front.
Requirements for atoi:
The functionfirst discards as many whitespace characters as necessary until the firstnon-whitespace character is found. Then, starting from this character, takes anoptional initial plus or minus sign followed by as many numerical digits aspossible, and interprets
them as a numerical value.
The string cancontain additional characters after those that form the integral number, whichare ignored and have no effect on the behavior of this function.
If the firstsequence of non-whitespace characters in str is not a valid integral number, orif no such sequence exists because either str is empty or it contains onlywhitespace characters, no conversion is performed.
If no validconversion could be performed, a zero value is returned. If the correct valueis out of the range of representable values, INT_MAX (2147483647) or INT_MIN(-2147483648) is returned.
2 分析
(1) 空字符串返回0;
(2) 去除字符串前边的0;
(3) 若遇到非数字字符则终止计算;
(4) 若求出的数越界则返回最大值或者最小值。
3 实现
int atoi(string str)
{
int start = str.find_first_not_of(' ');
if (start > 0)
{
str = str.substr(start);
}
int size = str.size();
if (0 == size)
{
return 0;
} int signal = 1;
int i = 0;
if (str[i] == '+')
{
++i;
}
else if (str[i] == '-')
{
signal = -1;
++i;
} long long res = 0;
while (i < size)
{
if (str[i] < '0' || str[i] > '9')
{
break;
}
res = res * 10 + (str[i] - '0');
if (res > INT_MAX)
{
return signal == 1 ? INT_MAX : INT_MIN;
}
++i;
} return res * signal;
}
字符串转成整型(int)的更多相关文章
- 字符串转换成整型,到底使用int.Parse,Convert.ToInt32还是int.TryParse?
当我们想把一个字符串转换成整型int的时候,我们可能会想到如下三种方式:int.Parse,Convert.ToInt32和int.TryParse.到底使用哪种方式呢? 先来考虑string的可能性 ...
- python将字符串转换成整型
将字符串转换成,整型,从字面理解很容易让人误会. 比如,要把这个"abcabc"转换成整型,臣妾做不到啊.除成转成ascii. 我们所说字符串转成整型是这样的. s = " ...
- 【转载】 C#中使用int.TryParse方法将字符串转换为整型Int类型
在C#编程过程中,将字符串string转换为整型int过程中,时常使用的转换方法为int.Parse方法,但int.Parse在无法转换的时候,会抛出程序异常,其实还有个int.TryParse方法可 ...
- 【转载】C#中使用int.Parse方法将字符串转换为整型Int类型
在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为Int类型就是一个常见的类型转换操作,int.Parse方法是C#中专门用来将字符串转换为整型int的,int.Parse方 ...
- C#中IPAddress转换成整型int
string addr = "11.22.33.44"; System.Net.IPAddress IPAddr=System.Net.IPAddress.Parse(addr); ...
- VC++中如何将字符串转换成整型数字
原文:http://blog.csdn.net/yongf2014/article/details/47071663 注意: atoi函数是c的函数,它的输入参数是char *类型. 你声明了stri ...
- wid是一个字符串 必须转化成整型
wid是一个字符串 必须转化成整型
- Java:将字符串中的数字转换成整型
在C语言中,将字符串中的数字转换为整型的方法是是利用atoi这个函数.在Java中,我们可以利用parseInt方法来实现,具体代码如下: public class HelloWorld { publ ...
- 基础数据类型:整型int、布尔值bool、字符串str、与for循环
1.整型 int() p2 long 长整型 p3 全部都是整型 2.布尔值 bool() True --- int() int(True) int() --- True bool(int) 注意点: ...
随机推荐
- 修改MySQL事件
MySQL允许您更改现有事件的各种属性. 要更改现有事件,请使用ALTER EVENT语句,如下所示: ALTER EVENT event_name ON SCHEDULE schedule ON C ...
- 洛谷P2730 魔板 [广搜,字符串,STL]
题目传送门 魔板 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有 ...
- java 读入文件 BufferedReader
package com.mkyong; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOExcep ...
- C和指针之学习笔记(4)
第9章 字符串 字符串的输入与输出 int ch; char strings[80]; FILE *input; (1)scanf(“%c”,&ch); printf(“%c \n” ...
- CentOS 报错cannot execute binary file
在安装软件过程中执行文件,报错cannot execute binary file 1.查看是否root用户登录,当前用户是否有可执行权限 2.ls -l 查看文件是否具有可执行权限 3.要使用对应的 ...
- NOIP2017 D1T2时间复杂度
这道题在考试时看到感觉与第一题放反了位置(因为我还没有看到第一题是结论题) 对于每个语句进行栈的模拟,而如果有语法错误就特判. 对于每一条for语句我们将其与栈顶元素连边,复杂度是1的我们不用考虑,如 ...
- (VIJOS) VOJ 1067 Warcraft III 守望者的烦恼 矩阵快速幂
https://vijos.org/p/1067 就..挺普通的一道题..自己学一下怎么推式子就可以...细节不多但是我还是日常爆细节..比如说循环写成从负数开始... 只求ac不求美观的丑陋 ...
- 【最小表示法】BZOJ2882-工艺
[题目大意] 求一个循环数列的最小表示法. [思路] 最小表示法模板题.之前用SAM做的,MLE了hhhhh戳☆ #include<iostream> #include<cstdio ...
- windows下eclipse搭建android_ndk开发环境
安装cygwin: 由于NDK编译代码时必须要用到make和gcc,所以你必须先搭建一个linux环境, cygwin是一个在windows平台上运行的unix模拟环境,它对于学习unix/linux ...
- lucas定理 FOJ 2020 组合
Problem 2020 组合 Accept: 886 Submit: 2084Time Limit: 1000 mSec Memory Limit : 32768 KB Problem ...