标题:字符串--整数函数,编的有问题,没看出来,求教……
只看楼主
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
你定义的str[1000]就是假定程序可以转换出1000位的整数嘛,不然弄那么大干什么。

授人以渔,不授人以鱼。
2014-11-27 16:45
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
得分:0 
i!='\0'是什么,应该是a[i]吧

一片落叶掉进了回忆的流年。
2014-11-27 17:37
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
下面是最簡單的代碼:
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(void)
{
    printf_s("%d\n", atoi("123456789"));

    _getch();
    return EXIT_SUCCESS;
}



授人以渔,不授人以鱼。
2014-11-27 18:46
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 



程序代码:
// crt_atoi.c
// This program shows how numbers
// stored as strings can be converted to
// numeric values using the atoi functions.

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
    char    *str = NULL;
    int     value = 0;

    // An example of the atoi function.
    str = "  -2309 ";
    value = atoi( str );
    printf( "Function: atoi( \"%s\" ) = %d\n", str, value );

    // Another example of the atoi function.
    str = "31412764";
    value = atoi( str );
    printf( "Function: atoi( \"%s\" ) = %d\n", str, value );

    // Another example of the atoi function
    // with an overflow condition occuring.
    str = "3336402735171707160320";
    value = atoi( str );
    printf( "Function: atoi( \"%s\" ) = %d\n", str, value );
    if (errno == ERANGE)
    {
       printf("Overflow condition occurred.\n");
    }
}



授人以渔,不授人以鱼。
2014-11-27 18:50
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:20 
參看資料:





授人以渔,不授人以鱼。
2014-11-27 18:55
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
为什么我给你推荐用库函数?道理有两个:第一,即使你会转换整数,也不会转换浮点数,后者仍然迫使你使用库函数,与其如此,不如直接全使用库函数,这跟你一开始写HelloWorld就用库函数printf()是一样的;第二,这些库函数通常比你自己写的执行更快,除非你确信自己的本事比库函数的众多精英更大。当然,作为练习,你写个转换整数的函数也未尝不可,但不必去弄大数。这种函数很简单,就是数值转换为字符串的逆过程而已。

授人以渔,不授人以鱼。
2014-11-27 20:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-439250-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.204667 second(s), 9 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved