atoi 实现
int atoi(const char *nptr);
把字符串转换成整型数。ASCII to integer 的缩写。
头文件: #include <stdlib.h>
参数nptr字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零,
#include <iostream>
#include <cctype> //参数nptr字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零
int atoi(const char *nptr)
{
if (!nptr) //空字符串
return ; int p = ;
while(isspace(nptr[p])) //清除空白字符
p++; if (isdigit(nptr[p]) || '+' == nptr[p] || '-' == nptr[p]) //清除后第一个是数字相关
{
int res = ;
bool flag = true; //正负数标志 //对第一个数字相关字符的处理
if('-' == nptr[p])
flag = false;
else if('+' != nptr[p])
res = nptr[p] - ''; //处理剩下的
p++;
while(isdigit(nptr[p]))
{
res = res * + (nptr[p] - '');
p++;
} if(!flag) //负数
res = - res;
return res;
}
else
{
return ;
}
}
int main()
{
using std::cin;
using std::cout;
using std::endl; cout << atoi("") <<endl << atoi("+2134") << endl << atoi("-342") <<endl << atoi(" -45d") << endl
<<atoi("\t +3543ddd") <<endl << atoi("\n56.34") << endl << atoi(" .44") << endl << atoi("") <<endl
<< atoi("\t") << endl <<atoi("\o") << endl; cin.get();
}

atoi 实现的更多相关文章
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 编写atoi库函数
看到很多面试书和博客都提到编写atoi函数,在很多面试中面试官都会要求应聘者当场写出atoi函数的实现代码,但基本很少人能写的完全正确,倒不是这道题有多么高深的算法,有多么复杂的数据结构,只因为这道题 ...
- 行程编码(atoi函数)
#include<iostream> #include<string> #include<vector> using namespace std; void jie ...
- No.008:String to Integer (atoi)
问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- c/c++面试题(8)memcopy/memmove/atoi/itoa
1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...
- 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 ...
- atoi()函数
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...
- [Leetcode]String to Integer (atoi) 简易实现方法
刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...
- 【leetcode】atoi (hard) ★
虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过 “ 123” ...
随机推荐
- j简单的递归
1 某人写了n封信和n个信封,如果所有的信都装错了信封.求所有的信都装错信封共有多少种不同情况. 归纳法例子 1.有n个硬币(n为偶数)正面朝上排成一排,每次将n-1个硬币翻成朝上为止.编程让计算机把 ...
- offer档次排名,2014最新版
转自:http://tieba.baidu.com/p/2748469183 综合考虑发展,薪水,环境,压力. 第0档:美国互联网总部special offer(15万刀起薪) 第一档: 股份制银行总 ...
- 一款基于jQuery轮播切换焦点图,可播放多张图片
今天给大家分享一款基于jQuery轮播切换焦点图,可播放多张图片,在这个组件中,你可以任意指定8张图片,然后插件就会帮你自动生成缩略图,并且自动开始切换播放图片.当然,你也可以手动切换图片,只要点击缩 ...
- jquery ui 改写cloes事件
htmlAjax:{//模板ajax请求参数设置项 url:"template/task/task_create.html", data ...
- 关于android listview去掉分割线
1.设置android:divider="@null" 2.android:divider="#00000000" #00000000后面两个零表示透明 3.. ...
- Enterprise Library深入解析与灵活应用(2): 通过SqlDependency实现Cache和Database的同步
对于一个真正的企业级的应用来说,Caching肯定是一个不得不考虑的因素,合理.有效地利用Caching对于增强应用的Performance(减少对基于Persistent storage的IO操作) ...
- E - 娜娜梦游仙境系列——莫名其妙的插曲
E - 娜娜梦游仙境系列——莫名其妙的插曲 E - 娜娜梦游仙境系列——莫名其妙的插曲 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 1 ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- java.util Pattern 和 Mathcer
1.测试给定的正则表达式是否匹配输入的字符串,这里该正则表达式只使用一次 private String regex ; private String input; @Before public voi ...
- Override Inline Styles with CSS
inline style: <div style="background: red;"> The inline styles for this div should m ...