#include <iostream>
#include <assert.h>
using namespace std;
int ato(const char *str) {
int i=,e=,s=;
int max=,min=-;
int f=;
int tem[]={};
unsigned int pan=;
while(*str==' '){ //过滤掉连续空格
str++;
}
if(*str=='-'||*str=='+'||(*str<=''&&*str>='')){
if(*str=='-'||*str=='+'){ //过滤掉正负号
if(*str=='-')
f=-;
str++;
}
while(*str=='') //过滤掉前面的无用的0
str++;
if( *str<=''&&*str>='' ){ //过滤掉非数字的字符 while( *str!='\0'){ //判断字符串是否结束
if( *str>''||*str<'' ) //字符串还没结束,就出现非数字字符
break;
if( i== ){ //大于10位的数字都要作溢出处理
if(f==-)
return min;
else
return max;
}
tem[i++]=*str-'';
e=e*+(*str-'');
str++;
}
if(i==){ //刚好10位,需要特殊处理
if(f==-){ //负数处理
if(tem[]>){ //超过30,0000,0000
return min;
}
for(i=;i<;i++){
pan=pan*+tem[i];
}
if(pan>=) // 负数的绝对值大于等于2147483648
return min;
}
else{ //正数处理
if(tem[]>){
return max;
}
for(i=;i<;i++){
pan=pan*+tem[i];
}
if(pan>=) // 负数的绝对值大于等于2147483648
return max;
}
}
return f*e;
}
else
return ;
}
return ;
}
int main(){
char str[];
int qq=;
while(cin>>str){
qq=ato(str);
cout<<qq<<endl;
}
return ;
}

这个东西原来挺难考虑周全的,还好leetcode会指出是输入什么数据会输出错误,不然估计找一辈子了。

鉴于这么苛刻的条件,贴出自己的不简洁的代码,这道题第一次AC。

以后有好办法再修改。

LeetCode OJ String to Integer (atoi) 字符串转数字的更多相关文章

  1. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  2. [leetcode]8. String to Integer (atoi)字符串转整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  3. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  4. Leetcode 8 String to Integer (atoi) 字符串处理

    题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...

  5. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  6. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  7. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  8. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  9. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

随机推荐

  1. css——子代与后代选择器

    一直都以为,子代选择器与后代选择器作用是一样的,都是选择一个元素下面的子元素,直到今天才明白: 1.子代选择器(用<连接):ul>li 子选择器仅仅选择ul包围的 子元素 中的 li元素, ...

  2. Installing a single-server IBM FileNet P8 Platform system with IBM Content Navigator

    Product documentation Abstract You can install all of the IBM FileNet P8 Platform components on a si ...

  3. netbios wins dns LLMNR

    NetBIOS名称 Network Basic Input/Output System  (RFC-1001,1002)网络基本输入/输出系统协议 NetBIOS是一种高级网络接口,最初是在硬件中实 ...

  4. .net调用存储过程碰到的一个问题

    问题描述 报错信息如下: Execution of user code in the .NET Framework is disabled. Enable "clr enabled" ...

  5. DataGridView复选框实现单选功能(二)

    双击DataGridView进入事件 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventA ...

  6. PowerShell并发控制-命令行参数之四问

    传教士问: win下如何 获取进程命令行,及命令行参数? 传教士答: 可以用这个powershell命令(实际上是wmi查询): (get-wmiobject -query "select ...

  7. Repeater 合并单元格

    前途页面: <asp:Repeater ID="rptList" runat="server" OnPreRender="rptList_Pre ...

  8. 用VNC远程图形化连接Linux桌面的配置方法

    首先下载到vnc的linux版本和windows版本. 1. 首先,我们要先下载 VNC 安装,假如我们下载的目录是 VNC/vncgo . # cd ~# wget http://www.realv ...

  9. python解无忧公主的数学时间编程题001.py

    python解无忧公主的数学时间编程题001.py """ python解无忧公主的数学时间编程题001.py http://mp.weixin.qq.com/s?__b ...

  10. SpringMvc 单例

    struts2的controller是多例,是因为其中有modeldriven将比如user 或者其他属性暴露出来,接受属性,特别是继承了actionsupport之后,fielderror的属性也会 ...