欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

定义一些结构体变量(包括年,月,日)。计算该日在本年中是第几天,注意闰年问题。

程序员文章站 2022-06-06 08:41:41
...

定义一些结构体变量(包括年,月,日)。计算该日在本年中是第几天,注意闰年问题。

#include <stdio.h>
#include <stdlib.h>
struct Date
{
	int year;
	int month;
	int day;
};
int days(struct Date date)
{
	int num = 0;
	int two = 0;
	if (date.year % 4 == 0)
	{
		if (date.year % 100 != 0)
		{
			two = 29;
		}
		else
		{
			two = 28;
		}
		if (date.year % 400 == 0)
		{
			two = 29;
		}
	}
	if (date.year % 400 == 0)
	{
		two = 29;
	}
	
	switch (date.month)
	{
	case 1: num = 0; break;
	case 2:	num = 31; break;
	case 3: num = 31 + two; break;
	case 4: num = 31+31 + two; break;
	case 5: num = 30+ 31 + 31 + two; break;
	case 6: num = 31+ 30 + 31 + 31 + two; break;
	case 7: num = 30+ 31 + 30 + 31 + 31 + two; break;
	case 8: num = 31+ 30 + 31 + 30 + 31 + 31 + two; break;
	case 9: num = 31+ 31 + 30 + 31 + 30 + 31 + 31 + two; break;
	case 10:num = 30+ 31 + 31 + 30 + 31 + 30 + 31 + 31 + two; break;
	case 11: num = 31+ 30 + 31 + 31 + 30 + 31 + 30 + 31 + 31 + two; break;
	case 12: num = 30+ 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 31 + two; break;
	}
	
	return num + date.day;
}
int main()
{
	struct Date a;
	scanf("%d%d%d", &a.year, &a.month, &a.day);
	printf("%d %d %d", a.year, a.month, a.day);
	printf("这一天是本年中的%d", cal(a));
	system("pause");
	return 0;
}

答案
定义一些结构体变量(包括年,月,日)。计算该日在本年中是第几天,注意闰年问题。