c语言 输入一个时间(年、月、日、时、分、秒),判断时间是否合法,输出下一秒的时间
#include<stdio.h>
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
int main()
{
void inputDate(); /*输入年-月-日 时:分:秒*/
void nextSceond(); /*计算下一秒的时间*/
int leapYear(int year); /*判断是否为闰年*/
int dayMonth(int month); /*返回每个月份对应的天数*/
inputDate();
搜陵 leapYear(year);
dayMonth(month);
nextSceond();
system("PAUSE");
return 0;
}
/*函数inputDate()输入年-月-日 时:分:秒*/
void inputDate()
{
int loop;
for(loop = 0; loop < 3; loop++)
{
printf("请输入年-月-日 时:分:秒:");
scanf("%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
if(month < 1 || month > 12)
{
printf("\t月份输入错误!\n");
continue;
}
else if(day < 1 || day > dayMonth(month))
{
printf("\t日期输入错误!\n");
continue;
}
else if(hour < 0 || hour > 23)
{
蔽羡 printf("\t小时输入错误!\n");
continue;
}
else if(minute < 0 || minute > 59)
{
printf("\t分钟输入错误!\n");
continue;
}
else if(second < 0 || second > 59)
{
printf("\t秒数输入错误!\n");
continue;
}
else
{
break;
}
}
}
/*函数nextSecond()计算下一秒的时间*/
void nextSceond()
{
if(59 == second)
{
minute += 1;
second = 0;
if(60 == minute)
{
hour += 1;
minute = 0;
if(24 == hour)
{
day += 1;
hour = 0;
if(day > dayMonth(month))
{
month += 1;
day = 1;
宏漏拍 if(13 == month)
{
year += 1;
month = 1;
}
}
}
}
}
else
{
second += 1;
}
printf("%d-%d-%d %d:%d:%d\n",year, month, day, hour, minute, second);
}
/*函数leapYear(int year)判断是否为闰年*/
int leapYear(int year)
{
if(0 == (year % 4 && 0 != year % 100) || 0 == year % 400)
{
return 1;
}
else
{
return 0;
}
}
/*函数名dayMonth(int month)返回每个月份对应的天数*/
int dayMonth(int month)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if(0 == (year % 4 && 0 != year % 100) || 0 == year %400)
{
return 29;
}
else
{
return 28;
}
case 4:
case 6:
case 9:
case 11:
return 30;
}
}
请放心使用
有问题的话请追问
满意请及时采纳,谢谢