写的比较粗陋,编程能力还需要多多磨练啊
struct SDate
{
int Leapday[12]; //闰年的每月天数
// int NoLeapDay[12]; //非闰年的每月天数
int year;
int month;
int day;
int hour;
int minute;
int second;
} sDate = { 31,29,31,30,31,30,31,31,30,31,30,31 };
bool isLeap(int year) //判断是否闰年
{
if (year%4==0 && year%100 || year%400==0)
return true;
else return false;
}
void getNextSecond(SDate *in, SDate &out) //获取当前秒的下一秒
{
if (!in) return;
memcpy(&out, in, sizeof(SDate));
if (in->second==59)
{
out.second=0;
if (in->minute==59)
{
out.minute=0;
if (in->hour==23)
{
out.hour=0;
if (in->day==in->Leapday[in->month-1])
{
out.day=1;
if (in->month==12)
{
out.month=1;
out.year+=1;
}
else out.month+=1;
}
else out.day+=1;
}
else out.hour += 1;
}
else out.minute +=1;
}
else out.second += 1;
}
bool fillData(SDate *in)
{
if (in==NULL) return false;
printf("请依次输入:年 月 日 时 分 秒");
while (1)
{
puts("年: ");
scanf("%d", &in->year);
if (in->year<=0) {
puts("年必须大于0,请重新输入年份:");
continue;
}
break;
}
while (1)
{
printf("月: ");
scanf("%d", &in->month);
if (in->month<1 || in->month >12) {
puts("月范围是<1-12>,请重新输入月份:");
continue;
}
break;
}
bool leap;
leap = isLeap(in->year);
if (leap)
;
else in->Leapday[1]=28;
while (1)
{
printf("日: ");
scanf("%d", &in->day);
if (in->day < 0 || in->day > in->Leapday[in->month-1]) {
puts("日子大小不对,请重新输入日:");
continue;
}
break;
}
while (1)
{
puts("时: ");
scanf("%d", &in->hour);
if (in->hour <0 || in->hour > 24) {
puts("小时数不对,请重新输入:");
continue;
}
break;
}
while (1)
{
printf("分: ");
scanf("%d", &in->minute);
if (in->hour <0 || in->minute > 60) {
puts("分钟数不对,请重新输入:");
continue;
}
break;
}
while (1)
{
printf("秒: ");
scanf("%d", &in->second);
if (in->second <0 || in->second > 60) {
puts("秒数不对,请重新输入:");
continue;
}
break;
}
}
void main()
{
SDate outDate;
fillData(&sDate);
getNextSecond(&sDate, outDate);
printf("下一秒为:%d 年 %d 月 %d 日 %d 时 %d 分 %d 秒\n", \
outDate.year, outDate.month, outDate.day, outDate.hour, outDate.minute, outDate.second);
}