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

89 阅读1分钟

题目 第九章

结构体

...谭浩强第五版C语言习题

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

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

#include <stdio.h>
 
struct date {
	int year;
	int month;
	int day;
} d;
 
int monthDays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
int main() {
	int i, res = 0;
	
	printf("请输入年 月 日:");
	scanf("%d %d %d", &d.year, &d.month, &d.day);
	
	for(i = 1; i < d.month; i++) {
		res += monthDays[i];
	}
	res += d.day;
	
	if((d.year % 400 == 0) || (d.year % 4 == 0 && d.year % 100 != 0))	res += 1;
	
	printf("\n%d年%d月%d日是第%d天\n", d.year, d.month, d.day, res);
	
	return 0;
}