Jiwu Bu

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  32 随笔 :: 0 文章 :: 25 评论 :: 0 Trackbacks
转载链接:http://blog.chinaunix.net/u2/76292/showart_1845022.html

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <ctype.h>
 4 
 5 long my_atol(const char *nptr)
 6 {
 7     int c; /* current char */
 8     long total; /* current total */
 9     int sign; /* if '-', then negative, otherwise positive */
10 
11     /* skip whitespace */
12     while ( isspace((int)(unsigned char)*nptr) )
13     {
14         ++nptr;
15     }
16     
17     c = (int)(unsigned char)*nptr++;
18     sign = c; /* save sign indication */
19     if (c == '-' || c == '+')
20     {
21         c = (int)(unsigned char)*nptr++/* skip sign */
22     }
23     
24     total = 0;
25 
26     while (isdigit(c)) 
27     {
28         total = 10 * total + (c - '0'); /* accumulate digit */
29         c = (int)(unsigned char)*nptr++/* get next char */
30     }
31 
32     if (sign == '-')
33     {
34        return -total;
35     }
36     else
37     {
38         return total; /* return result, negated if necessary */
39     }
40 }
41 
42 int  main(int argc, char* argv[])
43 {
44     if( argc < 2 )
45     {
46         return -1;
47     }
48 
49     int i = my_atol(argv[1]);
50     printf("[%s]====>[%d]\n", argv[1], i);
51    
52     int j = atoi(argv[1]);
53     printf("[%s]====>[%d]\n", argv[1], j);
54 
55     return 0;
56 }
57 
http://www.cppblog.com/Files/bujiwu/atoi.rar

posted on 2010-01-15 17:52 bujiwu 阅读(1399) 评论(0)  编辑 收藏 引用 所属分类: 算法与数据结构

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理