编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒

/*************************************
编写一个函数,要求输入年月日时分秒,
输出该年月日时分秒的下一秒。
如输入2004年12月31日23时59分59秒,
则输出2005年1月1日0时0分0秒
**************************************/

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

/* define function */
void InputData(void);
int LeapYear(int year);
void NextSec(void);

/* month[0];leap year month[1]; common year */
int Amonth[2][13]={
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int leap = 0;
int year, month, day, hour, minute, second;
/*
* Function Name: LeapYear()
* Describe: judge the current year is leap year or not
* Paramete: int year:current year
* Return Value: 1-leap year; 0-not
*/
int LeapYear(int year)
{
if ( (year%4 == 0) && (year%100 != 0) ||
(year%400 == 0) )
{
return 1;
}
else
{
return 0;
}
}

/*
* Function Name: InputData()
* Describe: input data
* Paramete: void
* Return Value: void
*/
void InputData(void)
{
printf("Input year(Press Enter to end input):");
if (scanf("%d", &year) != 1 || (year < 1000))
{
puts("Input year error");
getchar();
exit(0);
}
leap = LeapYear(year);

printf("Input month:");
if (scanf("%d", &month) != 1 || month<1 || month>12)
{
puts("Input month error!");
getchar();
exit(0);
}

printf("Input Day:");
if (scanf("%d", &day) != 1 || day<1 || day>Amonth[leap][month])
{
puts("Input day error!");
getchar();
exit(0);
}

printf("Input Hour:");
if (scanf("%d", &hour) != 1 || hour<0 || hour>59)
{
puts("Input hour error!");
getchar();
exit(0);
}

printf("Input Minute:");
if (scanf("%d", &minute) != 1 || minute<0 || minute>59)
{
puts("Input minute error!");
getchar();
exit(0);
}

printf("Input Second:");
if (scanf("%d", &second) != 1 || second<0 || second>59)
{
puts("Input second error!");
getchar();
exit(0);
}
}

/*
* Function Name: NextSec()
* Describle: Count the next second by the current time
* Paramete; void
* ReturnValue: void
*/
void NextSec(void)
{
if (++second == 60)
{
second = 0;
minute++;
}
if (minute == 60)
{
minute = 0;
hour++;
}
if (hour == 24)
{
hour = 0;
day++;
}
if (day == Amonth[leap][month]+1)
{
day = 1;
month++;
}
if (month == 13)
{
month = 1;
year++;
}

printf("The next second is %d-%d-%d %d:%d:%d\n\n",
year, month, day, hour, minute, second);
}

int main(void)
{
InputData();

NextSec();

system("PAUSE");
return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值