免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2246 | 回复: 3
打印 上一主题 下一主题

[函数] 请举个atoi函数的例子。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-12-01 14:46 |只看该作者 |倒序浏览
  
要一个简单的例子。

论坛徽章:
0
2 [报告]
发表于 2003-12-01 14:57 |只看该作者

请举个atoi函数的例子。

char tmp[300];
int  i;
strcpy(tmp,"20";
i = atoi(tmp);
printf("i=%d\n",i);

输出:
i=20

论坛徽章:
0
3 [报告]
发表于 2003-12-01 14:57 |只看该作者

请举个atoi函数的例子。

int GetDateTime( char ?s,long int bill_long,char?d_str, char ?t_str) {
   time_t timer,tim ;
   struct tm tb, ?tb1 ;
   int year_off = 1900 ;
   int mon_off = 1 ;
   char s1[20] ;
   if ( strlen( s )!=14 )
   return -1;
   strncpy( s1, s, 4 );
   s1[4] = '\0' ;
   tb.tm_year = atoi( s1 );
   strncpy( s1, s+4, 2 );
   s1[2] = '\0' ;
   tb.tm_mon = atoi( s1 );
   strncpy( s1, s+6, 2 );
   s1[2] = '\0' ;
   tb.tm_mday = atoi( s1 );
   if ( tb.tm_year==0 || tb.tm_mon==0 ||tb.tm_mday==0 )
   return -1;
   strncpy( s1, s+8, 2 );
   s1[2] = '\0' ;
   tb.tm_hour = atoi( s1 );
   strncpy( s1, s+10, 2 );
   s1[2] = '\0' ;
   tb.tm_min = atoi( s1 );
   strncpy( s1, s+12, 2 );
   s1[2] = '\0' ;
   tb.tm_sec = atoi( s1 );
   tb.tm_year -= year_off ;
   tb.tm_mon -= mon_off ;
   tb.tm_isdst = 0 ;
   tim=mktime( &tb ) ;
   tim=tim-bill_long;
   tb1=localtime(&tim);
   sprintf(d_str, "%#04d%#02d%#02d",1900+tb1->tm_year,tb1->tm_mon+1,tb1->tm_mday);
   sprintf(t_str, "%#02d%#02d%#02d",tb1->tm_hour, tb1->tm_min,tb1->tm_sec);
   return (tb1->tm_wday);
  } /? end of GetDateTime ?/

论坛徽章:
0
4 [报告]
发表于 2003-12-01 14:58 |只看该作者

请举个atoi函数的例子。

不知道你是说应用的例子还是atoi程序源码
我想应用应该很简单,这里就给源码好了 。
int atoi (char s[])
{
  int i,n;
  n = 0;
  for (i=0;s >;='0'&&s <= '9' ; ++i)
    n = 10 * n + (s-'0');

  return n;
}

在标准库中atoi的声明志这样的:
int atoi(const char* s)
atoi函数吧字符串s转换成int类型;该函数等价于
(int)strtol(s, (char**)NULL,10)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP