| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2156 人关注过本帖
标题:一个简单的计算年龄的问题,请各位指点
只看楼主 加入收藏
bylmyy
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-11-23
收藏
 问题点数:0 回复次数:7 
一个简单的计算年龄的问题,请各位指点
输入你的生日,然后计算出你从出生到现在一共多少天了?用C库里的时间函数

[此贴子已经被作者于2004-11-24 08:03:06编辑过]


搜索更多相关主题的帖子: 年龄 生日 库里 函数 
2004-11-24 00:00
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
得分:0 

不知道有没有方法可以从函数库简单运算得出,不过这种方法是行的

//两个年月日之间相隔的天数

#include <stdio.h> #include <conio.h>

int leap(int year) { if( (year%4==0&&year%100!=0)||(year%400==0) ) return 1; else return 0; }

int leftdays1(int year,int month,int day) { int i; int days=0; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; for(i=month+1;i<=12;i++) { if(i==2&&leap(year)==1) days+=29; else days+=d[i]; } days+=(d[month]-day); return days; }

int leftdays2(int year,int month,int day) { int i; int days=0; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; for(i=1;i<month;i++) { if(month>=3&&i==2&&leap(year)==1) days+=29; else days+=d[i]; } days+=day; return days; }

int main() { int b_year,b_month,b_day; int o_year,o_month,o_day; int totaldays; int i,a,b,c=0,m=0; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; clrscr(); printf("please input the start date with space\n"); scanf("%d %d %d",&b_year,&b_month,&b_day); printf("please input the end date\n"); scanf("%d %d %d",&o_year,&o_month,&o_day); a=leftdays1(b_year,b_month,b_day); b=leftdays2(o_year,o_month,o_day);

for(i=1;i<(o_year-b_year);i++) c+=365+leap(b_year+i);

if(o_year>b_year) totaldays=a+b+c; else if(o_year==b_year&&o_month>b_month) { for(i=1;i<(o_month-b_month);i++) { if(i==2) m+=d[i]+leap(b_year); else m+=d[i]; } totaldays=m+d[b_month]-b_day+o_day; } else totaldays=o_day-b_day; printf("There are %d days left\n",totaldays); getch(); return 0; }


2004-11-25 07:40
workhard
Rank: 1
等 级:新手上路
帖 子:96
专家分:0
注 册:2004-11-17
收藏
得分:0 
找不到计算时间差的函数,只有time(),localtime(),只有tm结构可以赋值,但找不到计算时间差的,计算时间差的都是秒级的.哪位高人指点一下,我挺感兴趣的.

2004-11-25 12:32
lgfstar
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2004-11-11
收藏
得分:0 
vc中的时间函数是什么啊?请教
2004-11-25 14:02
bylmyy
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-11-23
收藏
得分:0 
differtime()这个函数是不是计算时间差的?可是我不太会用.大家探究研究.
2004-11-25 20:33
bylmyy
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-11-23
收藏
得分:0 

大家看看这个程序

#include<time.h> main() { int year,month,date;//你的生日 struct date yourbirthd, nowd; struct tm yourbirtht, nowt; long yourbrith,now; double resultt; long result; .....;//输入你的生日 getdate(nowd); gettime(nowt); yourbirthd->da_year=year; yourbirthd_>da_day=date; yourbirthd_>da_mon=month; yourbirth=dostounix(yourbirthd,yourbirtht); now=dostounix(nowd,nowt); resultt=difftime(yourbirth, now); result=resultt/(24*60*60); ....//输出result即可 }

2004-11-25 22:23
workhard
Rank: 1
等 级:新手上路
帖 子:96
专家分:0
注 册:2004-11-17
收藏
得分:0 
difftime()函数计算两个time_t类型的时间差,返回秒数,我也看了好久,不会用。把tm类型转换成time_t类型时老提示错误。

2004-11-26 09:39
leatrang
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2004-12-4
收藏
得分:0 

第一程序我编译成功,运行也对了

谢谢了,辛苦了


2004-12-04 22:01
快速回复:一个简单的计算年龄的问题,请各位指点
数据加载中...
 
   



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

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