码迷,mamicode.com
首页 > 其他好文 > 详细

一年中的第几天

时间:2016-01-06 13:53:50      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

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

/*
	输入年月日,输出该日期是当年的第几天。
	year大于1900
*/

int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int isGoodYear(int year);
int isGoodMonth(int month);
int isGoodDay(int year,int month,int day);
int isLeapYear(int year);
int dayOfYear(int year, int month, int day);

int main(){
	int year, month, day;
	while (fflush(stdin), scanf("%d%d%d", &year, &month, &day) != EOF){
		if (isGoodYear(year) && isGoodMonth(month) && isGoodDay(year, month, day))	//判断输入是否合法
			printf("%d\n", dayOfYear(year, month, day));
		else
			printf("Please input a right date.\n");
	}
	system("pause");
	return 0;
}

int dayOfYear(int year, int month, int day){//当年第几天
	int total = 0;
	for (int i = 0; i < month - 1; ++i)
		total += monthDays[i];
	total += day;
	if (isLeapYear(year) && (month > 2))
		total++;
	return total;
}

int isLeapYear(int year){
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		return 1;
	else
		return 0;
}

int isGoodYear(int year){
	if (year >= 1900)
		return 1;
	else
		return 0;
}

int isGoodMonth(int month){
	if ((month >= 1) && (month <= 12))
		return 1;
	else
		return 0;
}

int isGoodDay(int year, int month, int day){
	switch (month)
	{
	case 2:
		if (isLeapYear(year))
			if ((day >= 1) && (day <= 29))
				return 1;
			else if ((day >= 1) && (day <= 28))
				return 1;
			else
				return 0;
	case 4:
	case 6:
	case 9:
	case 11:
		if ((day >= 1) && (day <= 30))
			return 1;
		else
			return 0;
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		if ((day >= 1) && (day <= 31))
			return 1;
		else
			return 0;
	default:
		printf("month error in day judge.\n");
		return 0;
	}
}

  

一年中的第几天

标签:

原文地址:http://www.cnblogs.com/mutaohengheng/p/5104922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!