【题目】

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

代码】

 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. 关于Jquery 操作Cookie 取值错误

    使用JQuery操作cookie时 发生取的值不正确的问题: 结果发现cookie有四个不同的属性: 名称,内容,域,路径 $.cookie('the_cookie'); // 读取 cookie $ ...

  2. js获取服务器时间戳

    <!DOCTYPE html> <html> <head> <title>ajax</title> </head> <bo ...

  3. FZU2082树链剖分

    简单题. #include<queue> #include<stack> #include<cmath> #include<cstdio> #inclu ...

  4. Java-httpClient警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

    使用HttpClient,总是报出“Going to buffer response body of large or unknown size. Using getResponseBodyAsStr ...

  5. Yii2 radioList设置默认值

    可以在对应的Controller的action中设置 $model->type = 1; 在view中 <?php $form = ActiveForm::begin(); ?>   ...

  6. BZOJ-1050 旅行comf 并查集+乱搞

    好久以前codevs上做过的,拿着改了改.. 1050: [HAOI2006]旅行comf Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2194 S ...

  7. USACO 3.2 ratios 高斯消元

    题目原意很简单,就是解一个三元一次方程组 直接高斯消元解方程组,枚举最后一列的倍数(k) 注意double的精度,有很多细节需要处理 /* PROB:ratios LANG:C++ */ #inclu ...

  8. HD2444The Accomodation of Students(并查集判断二分图+匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. VS2012变化的快捷键:

    VS2012变化的快捷键: 注释::VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C),实际操作,按住Ctrl键不放,先按K键,再按C键.相当于Ctrl+K加 Ctrl ...

  10. Strust的基础情况

    Struts的优点: 1.实现MVC模式,结构清晰 2.丰富的标签(tag) 3.通过配置文件页面导航,便于后期维护 4.与Servlet API松耦合,便于测试 Structs2=Structs1的 ...