判断当前时间的下一秒是多少


写的比较粗陋,编程能力还需要多多磨练啊

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);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值