判斷日期是該年的第幾天

weiwei_lol發表於2020-10-25

判斷日期是該年的第幾天

void date()
{
	int i,year,month,date,sum = 0;
	printf("請輸入年、月、日:");
	scanf("%d %d %d",&year,&month,&date);
	int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
	if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
	{
		for(i = 0;i <= month - 2;i++)
		{
			sum += b[i];
		}
	}
	else
	{
		for(i = 0;i <= month - 2;i++)
		{
			sum += a[i];
		}
	}
	sum = sum + date;
	printf("是該年的第%d天\n",sum);
}

相關文章