1.义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
时间: 2023-12-03 13:53:06 浏览: 34
好的,让我来回答您的问题。请看下面的代码:
```c
#include <stdio.h>
struct Date {
int year;
int month;
int day;
};
int isLeapYear(int year) { // 判断是否为闰年
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int getDayCount(struct Date date) { // 计算该日在本年中是第几天
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dayCount = 0;
for (int i = 0; i < date.month - 1; ++i) { // 先计算前面月份的天数
dayCount += monthDays[i];
}
dayCount += date.day; // 再加上这个月的天数
if (date.month > 2 && isLeapYear(date.year)) { // 如果是闰年,2月份要多加一天
++dayCount;
}
return dayCount;
}
int main() {
struct Date date = {2022, 9, 8}; // 示例日期
int dayCount = getDayCount(date);
printf("%d-%d-%d is the %dth day of %d.\n", date.year, date.month, date.day, dayCount, date.year);
return 0;
}
```
欢迎您对代码进行 review 和提问,我会尽可能地回答。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)