【题目】

把字符串转换成整数,需要考虑字符串有效性。

代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
bool bValid = true;

int str2int_core(const char *digit, bool minus)
{
    ;
    ;
    while(*digit != '\0')
    {
        if (*digit >= '0' && *digit <= '9')
        {
            sum =  * sum + flag * (*digit - '0');

// whether overflow ?
            if ((!minus && sum > (signed int)0x7fffffff)
                    || (minus && sum < (signed int)0x80000000))
            {
                sum = ;
                break;
            }
            digit ++;
        }
        else
        {
            sum = ;
            break;
        }
    }

if (*digit == '\0')
    {
        bValid = true;
    }
    return (int)sum;
}

int str2int(const char *str)
{
    bValid = false;
    ;
    if (NULL == str || *str == '\0')
        return (int)sum;
    bool minus = false;
    if (*str == '+')
    {
        str++;
    }
    else if (*str == '-')
    {
        str++;
        minus = true;
    }
    if (*str == '\0')
        return  (int)sum;

return str2int_core(str, minus);
}

void test_case(const char *str)
{
    int sum = str2int(str);
    if (bValid)
        cout << sum << endl;
    else
        cout << "Invalid input!" << endl;
}

【参考】

http://zhedahht.blog.163.com/blog/static/25411174200731139971/

17.把字符串转换成整数[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函数)

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

  3. 如何实现字符串转换成整数(实现atoi内置函数)?

    题目描述 输入一个由数字组成的字符串,把它转换成整数并输出.例如:输入字符串"123",输出整数123. 给定函数原型int StrToInt(const char *str) , ...

  4. 剑指offer——把字符串转换成整数(c++)

    题目描述请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不能使用atoi或者其他类似的库函数. 示例 1:输入: " -42"输出: -42解释: 第一个非空 ...

  5. 【Java】 剑指offer(67) 把字符串转换成整数

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请你写一个函数StrToInt,实现把字符串转换成整数这个功能 ...

  6. 《剑指offer》第六十七题(把字符串转换成整数)

    // 面试题67:把字符串转换成整数 // 题目:请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不 // 能使用atoi或者其他类似的库函数. #include <ios ...

  7. 【剑指Offer学习】【面试题49:把字符串转换成整数】

    题目:实现一个函数stringToInt,实现把字符串转换成整数这个功能.不能使用atoi或者其它相似的库函数. 题目解析 这看起来是非常easy的题目,实现基本功能 ,大部分人都能用10行之内的代码 ...

  8. [LeetCode]面试题67. 把字符串转换成整数

    题目 写一个函数 StrToInt,实现把字符串转换成整数这个功能.不能使用 atoi 或者其他类似的库函数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  9. 剑指 Offer 67. 把字符串转换成整数 + 字符串

    剑指 Offer 67. 把字符串转换成整数 Offer_67 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author WaleGa ...

随机推荐

  1. 泛——复习js高级第三版

    1:本地存储的几种方法: (1)cookie: (2)localStorage //园子的自动保存就用了本地存储 (3)sessionStorage (4)globalStorage (5)index ...

  2. 【CodeForces 621C】Wet Shark and Flowers

    题 There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such tha ...

  3. CVE-2014-0160 Heartbleed Vul Analysis && OpenSSL Cryptographic Software Library Bug

    目录 . Heartbleed漏洞简介 . 漏洞造成的风险和影响 . 漏洞的测试.POC . OpenSSL漏洞源代码分析 . 防御.修复方案 . 从漏洞中得到的攻防思考 1. Heartbleed漏 ...

  4. Codeforces 567D One-Dimensional Battle Ships

    传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...

  5. Android 系统属性SystemProperty分析

     http://www.cnblogs.com/bastard/archive/2012/10/11/2720314.html Android System Property 一 System Pro ...

  6. Serverlet程序

    Serverlet是用Java编写的服务器端程序;主要用于交互地浏览和修改数据,生成动态Web内容; 一个serverlet就是一个继承于HttpServlet抽象类的Java类:下面先看一个简单的例 ...

  7. PHP中应用GD2函数在图像上添加文字

    <?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/g ...

  8. PRML Chapter 2. Probability Distributions

    PRML Chapter 2. Probability Distributions P68 conjugate priors In Bayesian probability theory, if th ...

  9. hdu 2085 核反应堆

    看完题,想到用结构体存储高质点和低质点,然后打表存储<33的质点数量. #include<stdio.h> struct hilo { long long hi,lo; }; int ...

  10. JS Resizable Panel 练习

    Resizable Panel HTML <!doctype html> <html> <head> <title>Resizable Panel< ...