2012704-(輸入一個年月日,判斷這是這一年的第幾天)

#include<stdio.h>
int main()
{
	int year,month,day,m2,m=0;
	while(3!=scanf("%d%d%d",&year,&month,&day))
		getchar();
//進行數據校驗
	if(month > 12 || month < 1)
	{
		puts("please input the month between 1 and 12!!!");
		return -1;
	}
//判斷是否爲閏年
	if(year % 100 == 0 && year % 4 != 0 || year % 400 ==0)
	{	
		printf("this is a leap year!!\n");
		m2=29;
	}
	else
	{
		puts("not a leap year!!");
		m2=28;
	}
	switch(month)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if(day > 31 ||  day< 1)
			{
				puts("please input the day between 1 and 31!!!");
				return -1;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if(day > 30 || day < 1)
			{
				puts("please input the day between 1 and 30!!!");
				return -1;
			}
			break;
		case 2:
			if(day > m2 || day <1)
			{
				printf("please input the day between 1 and %d\n",m2);
				return -1;
			}
			break;
		default :
			break;
	
	}
//核心代碼
	switch(month)
	{
		case 12: m+=31;
		case 11: m+=30;
		case 10: m+=30;
		case 9: m+=31;
		case 8: m+=31;
		case 7: m+=30;
		case 6: m+=31;
		case 5: m+=30;
		case 4: m+=31;
		case 3: m+=m2;
		case 2: m+=31;
		case 1: m+=day;
				break;
		default :
			  	break;
	}

	printf("this is the %d day of the year\n",m);
}
相關文章
相關標籤/搜索