Posted on 2007-07-03 23:23
停留的风 阅读(589)
评论(0) 编辑 收藏 所属分类:
C语言学习历程
#include <stdio.h>
struct time
{
int hour;
int minute;
int second;
};
struct date
{
int year;
int month;
int day;
};
struct dateAndTime
{
struct date sdate;
struct time stime;
};
struct dateAndTime clockKeeper(struct dateAndTime s)
{
struct dateAndTime nextTime;
struct time timeUpdate(struct time now);
struct date dateUpdate(struct date today);
int i=s.stime.hour;
nextTime.stime=timeUpdate(s.stime);
nextTime.sdate=s.sdate;
if(i!=0&&(s.stime.hour==0))
{
nextTime.sdate=dateUpdate(s.sdate);
}
return nextTime;
}
//日期更新
struct date dateUpdate(struct date today)
{
int numberOfDays(struct date d);
struct date tomorrow;
if(today.day!=numberOfDays(today))
{
tomorrow.day=today.day+1;
tomorrow.month=today.month;
tomorrow.year=today.year;
}
else if(today.month==12)
{
tomorrow.day=1;
tomorrow.month=1;
tomorrow.year=today.year+1;
}
else
{
tomorrow.day=1;
tomorrow.month=today.month+1;
tomorrow.year=today.year;
}
return tomorrow;
}
//查找一个月日期数的函数
int numberOfDays(struct date d)
{
int days;
int isLeapYear(struct date d);
const int daysPerMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(d)==1&&d.month==2)
{
days=29;
}
else
{
days=daysPerMonth[d.month-1];
}
return days;
}
//判定是不是瑞年
int isLeapYear(struct date d)
{
int leapYearFlag;
if((d.year%4==0&&d.year%100!=0)||d.year%400==0)
{
leapYearFlag=1;
}
else
leapYearFlag=0;
return leapYearFlag;
}
//时间更新
struct time timeUpdate(struct time now)
{
++now.second;
if(now.second==60)
{
now.second=0;
++now.minute;
if(now.minute==60)
{
now.minute=0;
++now.hour;
if(now.hour==24)
{
now.hour=0;
}
}
}
return now;
}
int main(void)
{
struct dateAndTime s1={{2006,05,9},{23,59,59}};
struct date today={2006,2,28};
struct date tomorrow=dateUpdate(today);
printf("%i%i%i\n",tomorrow.year,tomorrow.month,tomorrow.day);
struct dateAndTime clockKeeper(struct dateAndTime s);
struct dateAndTime upToNow=clockKeeper(s1);
printf("The pre dateAndTime is %.4i/%.2i/%.2i %.2i:%.2i:%.2i.\n",s1.sdate.year,s1.sdate.month,s1.sdate.day,s1.stime.hour,s1.stime.minute,s1.stime.second);
printf("The pre dateAndTime is %.4i/%.2i/%.2i %.2i:%.2i:%.2i.\n",upToNow.sdate.year,upToNow.sdate.month,upToNow.sdate.day,upToNow.stime.hour,upToNow.stime.minute,upToNow.stime.second);
return 0;
}
测试结果:
另一个测试:
不过这里还存在一定的问题,就是日期不能更新,怪。还没发现问题是什么