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 实现的更多相关文章

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

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. 编写atoi库函数

    看到很多面试书和博客都提到编写atoi函数,在很多面试中面试官都会要求应聘者当场写出atoi函数的实现代码,但基本很少人能写的完全正确,倒不是这道题有多么高深的算法,有多么复杂的数据结构,只因为这道题 ...

  3. 行程编码(atoi函数)

    #include<iostream> #include<string> #include<vector> using namespace std; void jie ...

  4. No.008:String to Integer (atoi)

    问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  5. c/c++面试题(8)memcopy/memmove/atoi/itoa

    1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...

  6. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

  7. [LeetCode] 8. String to Integer (atoi)

    Implement atoi to convert a string to an integer. public class Solution { public int myAtoi(String s ...

  8. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

  9. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  10. 【leetcode】atoi (hard) ★

    虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过  “      123” ...

随机推荐

  1. javascript 拷贝

    拷贝简单分为浅拷贝与深度拷贝,即给定一个对象,生成一个相同的对象. 浅拷贝 function copy(source,destiny){ destiny = destiny || {}; if(typ ...

  2. 解决ajax.net 1.0中文乱码问题!

      在使用ajax.net的UpdatePanel的时候,当requestEncoding编码为GB2312的时候,出现乱码.如果要解决这个问题最简单的就是改用utf-8了,但是原来使用GB2312, ...

  3. Android基本控件之listView(三)<用ListView实现分页加载>

    我们之前讨论了ListView的基本使用方法和ListView的优化 今天我们再来讨论一个关于ListView的一个新的东西~就是分页加载.那么什么是分页加载呢?简单点说,就是"下拉刷新&q ...

  4. LeetCode6 ZigZag Conversion

    题意: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  5. [Arduino] 逗号分隔文本到数组的两种方法

    以下是今日练习通过逗号来分割字符数组/字符串的2个例子和方法" 1.通过indexOf函数 /* *Splitsplit sketch *split a comma-separated st ...

  6. 2.1.5 用SSS扫描器实施扫描

    SSS(Shadow Security Scaner)是一款著名的系统漏洞扫描器,可对很大范围内的系统漏洞进行安全.高效.可靠的安全检测,其系统扫描的速度与精度足以让用户敢和专业安全机构的人以及那些专 ...

  7. MVC+easyui 完整实现

    学习mvc半月有余,趁几天假期,没事做就动手做一个完整的网站玩玩,顺便和上家公司的方法做个比较.页面引擎采用mvc自带的功能,建立视图,交给.net自带渲染出页面(对比:上家公司采用的第三方组件渲染的 ...

  8. ARC 和 MRC 小结

    ARC 和 MRC 内存管理 从 MRC—>ARC 就是将内存管理部分,从开发者的函数中转移到函数外部的runtime 中.由于 runtime 的开发简单,逻辑层次高,所以 runtime 的 ...

  9. Java学习笔记——JDK1.7的新特性。

    1,switch中可以使用字串 Java代码: String s = "test"; switch (s) { case "test" : System.out ...

  10. oracle PL/SQL(procedure language/SQL)程序设计之游标cursors

    游标 Cursors--Conception 每一条被Oracle服务器执行的SQL语句都有一个独立的游标与之相关联:隐式游标 Implicit cursors: 用于所有的DML和PL/SQL的SE ...