#include <time.h> // 判断是否为闰年 int is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } // 计算某年某月的天数 int get_days_in_month(int year, int month) { int days[] = {31, 28 + is_leap_year(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return days[month - 1]; } // 计算某年某月第一天是星期几 int get_weekday(int year, int month) { struct tm time = {0}; time.tm_year = year - 1900; time.tm_mon = month - 1; time.tm_mday = 1; mktime(&time); return time.tm_wday; } // 显示日历 void show_calendar(int year, int month) { int days = get_days_in_month(year, month); int weekday = get_weekday(year, month); printf("%d年%d月\n", year, month); printf("日 一 二 三 四 五 六\n"); for (int i = 0; i < weekday; i++) { printf(" "); } for (int i = 1; i <= days; i++) { printf("%2d ", i); if ((i + weekday) % 7 == 0) { printf("\n"); } } printf("\n"); } int main() { time_t now = time(NULL); struct tm *tm_now = localtime(&now); int year = tm_now->tm_year + 1900; int month = tm_now->tm_mon + 1; show_calendar(year, month); while (1) { printf("请输入年份和月份(例如:2022 1):"); scanf("%d %d", &year, &month); if (year < 1900 || year > 9999 || month < 1 || month > 12) { printf("输入错误,请重新输入!\n"); continue; } show_calendar(year, month); } return 0; }
时间: 2024-02-02 14:03:01 浏览: 25
这是一个 C 语言程序,主要实现了一个日历的功能。程序中定义了四个函数,分别是判断是否为闰年的函数 is_leap_year()、计算某年某月的天数的函数 get_days_in_month()、计算某年某月第一天是星期几的函数 get_weekday() 和显示日历的函数 show_calendar()。程序主函数中调用了这些函数来实现日历的功能。
程序的主要逻辑是先获取当前的年份和月份,然后调用 show_calendar() 函数来显示当前月份的日历。接着程序会进入一个死循环,等待用户输入年份和月份,然后调用 show_calendar() 函数来显示用户输入的月份的日历。如果用户输入的年份或月份不合法,程序会提示用户重新输入。
相关问题
#include<stdio.h> int is_leap_year(int year){ return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int main(){ int year; printf("请输入年份:"); scanf("%d",year); return 0;}判断闰年怎么修改
你需要在 `main()` 函数中调用 `is_leap_year()` 函数,并根据它的返回值输出结果。可以按照下面的代码修改:
```
#include <stdio.h>
int is_leap_year(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
int year;
printf("请输入年份:");
scanf("%d", &year);
if (is_leap_year(year)) {
printf("%d年是闰年\n", year);
} else {
printf("%d年不是闰年\n", year);
}
return 0;
}
```
这样,用户输入年份后,程序就会根据 `is_leap_year()` 函数的返回值判断是否为闰年,并输出相应的结果。
#include <stdio.h> #include <stdlib.h> #include <time.h> // 判断是否为闰年 int is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } // 计算某年某月的天数 int get_days_in_month(int year, int month) { int days[] = {31, 28 + is_leap_year(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return days[month - 1]; } // 计算某年某月第一天是星期几 int get_weekday(int year, int month) { struct tm time = {0}; time.tm_year = year - 1900; time.tm_mon = month - 1; time.tm_mday = 1; mktime(&time); return time.tm_wday; } // 显示日历 void show_calendar(int year, int month) { int days = get_days_in_month(year, month); int weekday = get_weekday(year, month); printf("%d年%d月\n", year, month); printf("日 一 二 三 四 五 六\n"); for (int i = 0; i < weekday; i++) { printf(" "); } for (int i = 1; i <= days; i++) { printf("%2d ", i); if ((i + weekday) % 7 == 0) { printf("\n"); } } printf("\n"); } int main() { time_t now = time(NULL); struct tm *tm_now = localtime(&now); int year = tm_now->tm_year + 1900; int month = tm_now->tm_mon + 1; show_calendar(year, month); while (1) { printf("请输入年份和月份(例如:2022 1):"); scanf("%d %d", &year, &month); if (year < 1900 || year > 9999 || month < 1 || month > 12) { printf("输入错误,请重新输入!\n"); continue; } show_calendar(year, month); }
下面是该代码的流程图:
![日历程序流程图](https://i.imgur.com/VWv7xtp.png)
该程序主要分为三个函数和一个主函数:
1. `is_leap_year(int year)`:用于判断某一年是否为闰年,返回值为布尔类型。
2. `get_days_in_month(int year, int month)`:用于计算某一年某一月的天数,返回值为整型。
3. `get_weekday(int year, int month)`:用于计算某一年某一月的第一天是星期几,返回值为整型。
4. `show_calendar(int year, int month)`:用于显示某一年某一月的日历,无返回值。
5. `main()`函数:主函数,用于获取当前时间并显示当月的日历,然后通过循环不断读取用户输入的年份和月份,调用`show_calendar(int year, int month)`函数显示对应的日历。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)