Next date

iZuckonit posted @ 2012年3月23日 16:48 in C/C++ with tags c next date , 1745 阅读

enter a date , output the next date

 

#include "stdio.h"
#include "stdlib.h"

typedef struct
{
	int year;
	int month;
	int day;
}Date;

int days[2][12] = {
	{31,28,31,30,31,30,31,31,30,31,30,31},
	{31,29,31,30,31,30,31,31,30,31,30,31}
};


int is_leap_year(int year)
{
	return ((year%4 == 0 && year%100 != 0) || year%400 == 0);
}


int is_error_date(Date date)
{
	return (date.day < 1 || date.day > days[is_leap_year(date.year)][date.month - 1]
		|| date.month < 1 || date.month > 12 || date.year < 1);
}


Date next_day(Date cur)
{
	int m_days = days[is_leap_year(cur.year)][cur.month - 1];
	int n_day = cur.day + 1;
	// printf("%d\n", n_day);
	// printf("%d\n", m_days);

	if (n_day > m_days){
		cur.day = 1;
		cur.month++;
		if (cur.month > 12){
			cur.month = 1;
			cur.year++;
		}
	}
	else{
		cur.day++;
	}

	return cur;
}


int main(int argc, char const *argv[])
{
	Date date;
	Date next;

	scanf("%d%d%d",&date.year,&date.month,&date.day);
	if (is_error_date(date)){
		printf("error date\n");
		exit(0);
	}

	next = next_day(date);
	printf("%d-%d-%d\n", next.year,next.month,next.day);

	return 0;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter