【题目】

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

代码】

 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. 【BZOJ 1013】【JSOI2008】球形空间产生器sphere 高斯消元基础题

    最基础的高斯消元了,然而我把j打成i连WA连跪,考场上再犯这种错误就真的得滚粗了. #include<cmath> #include<cstdio> #include<c ...

  2. 【Gym 100733D】Little thief Shi

    题 Shi realized that he was almost out of money, even renting Shitalian lands. Shi was walking on a s ...

  3. Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError

    SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackO ...

  4. Oracle 调度程序(scheduler)摘自一位大神

    在11g中,Oracle提供了一个新建的Scheduler特性,帮助将作业实现自动化.它还可以帮助你控制资源的利用与并可以将数据库中的作业按优先顺序执行.传统的dbms_jobs的一个限制是它只能调度 ...

  5. Java编程思想学习(十六) 并发编程

    线程是进程中一个任务控制流序列,由于进程的创建和销毁需要销毁大量的资源,而多个线程之间可以共享进程数据,因此多线程是并发编程的基础. 多核心CPU可以真正实现多个任务并行执行,单核心CPU程序其实不是 ...

  6. BZOJ-2257 瓶子和燃料 分解因数+数论方面乱搞(裴蜀定理)

    一开始真没想出解法...后来发现那么水.... 2257: [Jsoi2009]瓶子和燃料 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 970 So ...

  7. POJ2531Network Saboteur(DFS+剪枝)

    Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10391   Accepted: 4990 ...

  8. 学习 easyui 之一:easyloader 分析与使用

    http://www.cnblogs.com/haogj/archive/2013/04/22/3036685.html 使用脚本库总要加载一大堆的样式表和脚本文件,在 easyui 中,除了可以使用 ...

  9. 过滤器在Web开发应用------解决中文乱码

    src/com/Encoding.java package com; import java.io.IOException; import javax.servlet.Filter; import j ...

  10. Writing the first draft of your science paper — some dos and don’ts

    Writing the first draft of your science paper — some dos and don’ts 如何起草一篇科学论文?经验丰富的Angel Borja教授告诉你 ...