函数原型: int atoi(const char *nptr);

函数说明: 参数nptr字符串,如果第一个非空格字符存在,并且,如果不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。

代码:

#include<stdio.h>
#include<stdlib.h>
#include <cctype> int my_atoi(const char* p)
{
if(p==NULL)
return ;
bool neg_flag = false; // 符号标记
int res = ; // 结果
if(p[]=='+'||p[]=='-')
neg_flag=(*p++!='+');
while(isdigit(*p))
res=res*+(*p++-'');
return neg_flag?-res:res;
} int main()
{
char a[] = "-100" ;
char b[] = "" ;
int c ;
c=atoi(a)+atoi(b) ;
printf("a = %d\n", my_atoi(a)) ;
printf("b = %d\n", my_atoi(b)) ;
printf("c = %d\n", c) ; system("pause");
return ;
}

实现字符串转化为整数函数atoi()函数的更多相关文章

  1. 算法练习-字符串转换成整数(实现atoi函数)

    练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...

  2. 【Java】将字符串转化为整数

    前几天面试遇到这个问题:在Java中如何将字符串转化为整数,当时too young too naive,随便回答了一下.今天跑去看Java源码中paresInt函数的写法,Oh my god!其实不看 ...

  3. 剑指offer 把字符串转化为整数

    题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 输入描述: 输入一个字符串,包括数字字母符号,可以为空 输出描述: 如果是合法 ...

  4. 十六进制字符串转化为十进制值strtoul函数

    eg: NSString *strtest =@"7fffffff"; NSUInteger val = strtoul([[strtest substringWithRange: ...

  5. Python:利用内建函数将字符串转化为整数

    使用内建函数raw_input()内建函数,它读取标准输入,并将读取到的数据赋值给指定的变量.我们可以使用int()内建函数将用户输入的字符串转换为整数: >>> user = ra ...

  6. PHP中将字符串转化为整数(int) intval() printf()

    int <?php $foo = "1"; // $foo 是字符串类型 $bar = (int)$foo; // $bar 是整型 ?> intval <?ph ...

  7. 面试题49:把字符串转换成整数(atoi)

    需要考虑的问题都已在程序中注释 bool isValid; int StrToInt(const char* str) { isValid = false; //不合法情形1:空指针 if (str ...

  8. python 中的 用chr()数值转化为字符串,字符转化为数值ord(s)函数

    1.1 python字符串定义 #!/usr/bin/python # -*- coding: utf8 -*- # 定义一个字符串 s1 = 'this is long String that sp ...

  9. 2016.5.21——atoi()函数的测试

    对函数atoi()函数的测试: atoi()函数将字符串型转换为整型 代码: #include "stdafx.h" #include "iostream" # ...

随机推荐

  1. C# partial 关键字详解

    我们新建一个Windows Form时,后台代码都会自动添加如下代码: public partial class Form1 : Form { public Form1() { InitializeC ...

  2. RHEL6.4 NFS文件共享服务器搭建

    服务端:192.168.56.16客户端:192.168.56.17 服务端安装配置1.安装软件包 # yum install rpcbind nfs-utils 2.配置开机自启动 # chkcon ...

  3. django1.8读书笔记模型高级进阶

    一.访问外键和多对多值 例如:模型类定义如下 from django.db import models class Publisher(models.Model): name = models.Cha ...

  4. CentOS的字符集locale的设置

    LANGLC_*的默认值,是最低级别的设置,如果LC_*没有设置,则使用该值.类似于 LC_ALL. LC_ALL它是一个宏,如果该值设置了,则该值会覆盖所有LC_*的设置值.注意,LANG的值不受该 ...

  5. JS实现随机数生成算法示例代码

    JS实现随机数生成算法的方法有很多,本文为大家介绍一个比较不错的方法. 1, var MT = []; var index = 0; function initialize_generator(see ...

  6. mybatis 一二事(3) - 多表关联查询

    db.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/order jdbc.user ...

  7. google protocol buffer的原理和使用(三)

    介绍下怎么反序列化GoogleBuffer数据.并在最后提供本系列文章中所用到的代码整理供下载. 上一篇文章介绍了如何将数据序列化到了addressbook.data中.那么对于接受方而言该怎么解析出 ...

  8. JNI-获取Java对象的成员变量-GeInttField()

    例: 在Java中定义一个属性,然后用C语言将其设置成另外的值,并输出出来. Java代码: Person.java package com.yuneec.demo; public class Per ...

  9. I/O Completion Ports

    http://weblogs.asp.net/kennykerr/parallel-programming-with-c-part-4-i-o-completion-ports http://webl ...

  10. Cocos2d-x CCScale9Sprite 用法

    1.创建方式有三种: (1).直接创建 auto blocks = Scale9Sprite::create("blocks9.png", Rect(0, 0, 96, 96), ...