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. gpus_ReturnGuiltyForHardwareRestart 错误

    经查出是glScissor长宽不能为0,在某些设备上会出问题

  2. Common Linux log files name and usage--reference

    reference:http://www.coolcoder.in/2013/12/common-linux-log-files-name-and-usage.html if you spend lo ...

  3. java和C#中 定义变量,加一个小括号是什么意思。。。

    在群里灌水发现,有人这样定义字符串,感觉很好奇..又问了群友,群友说这样定义没问题.. String strtemp = ("test"); 我自己试了下,java和C#都可以,C ...

  4. C#读取Word模板替换相应的字符串(标签)生成新的Word

    在平常工作中,生成word的方式主要是C#读取html的模板文件处理之后保存为.doc文件,这样的好处是方便,快捷,能满足大部分的需求.不过有些特殊的需求并不能满足,如要生成的Word为一个表格,只是 ...

  5. Jax-ws开发实例

    初次接触Jax-ws(Java API xml web Service)感觉挺简单的,在这里写下我的所学的: 大概的顺序是:首先定义接口 ,然后写接口的实现类,最后编写客户端.步骤是挺简单的,来看看代 ...

  6. python 基础——私有属性

    私有属性 python 私有属性以两个下划线开头. python 私有属性只能在类内部访问,类外面访问会出错. python 私有属性之所以不能在外面直接通过名称来访问,其实质是因为 python 做 ...

  7. HDU 1233 还是畅通工程 (最小生成树)

    还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!&&在eclipse.ini中为eclipse指定jdk启动

    参考:http://blog.csdn.net/zyz511919766/article/details/7442633 http://blog.sina.com.cn/s/blog_028f0c1c ...

  9. PHP中正则替换函数preg_replace用法笔记

    今天应老板的需求,需要将不是我们的页面修改一个链接,用js+iframe应该也能实现,但是我想尝试一下php实现方法. 首先你得先把别人的页面download到你的php中,实现方法可以用curl, ...

  10. Http error code

    概要 当用户试图通过HTTP或文件传输协议(FTP)访问一台正在运行Internet信息服务(IIS)的服务器上的内容时,IIS返回一个表示该请求的状态的数字代码.该状态代码记录在IIS日志中,同时也 ...