万年历程序。编写程序打印任意给定的年份的日历或者某年某月的月历或者查询某一天是星期几。C语言
时间: 2024-03-23 15:43:33 浏览: 33
好的,以下是一个基于 C 语言的万年历程序:
```c
#include <stdio.h>
// 计算某年某月的天数
int getDays(int year, int month) {
int days;
switch (month) {
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = 31;
break;
}
return days;
}
// 计算某年某月1号是星期几
int getWeekday(int year, int month) {
int weekday;
if (month == 1 || month == 2) {
month += 12;
year--;
}
weekday = (1 + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
return weekday;
}
// 打印某年某月的月历
void printMonth(int year, int month) {
int days, weekday, i;
char *months[] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
printf("%s %d\n", months[month], year);
printf("Su Mo Tu We Th Fr Sa\n");
days = getDays(year, month);
weekday = getWeekday(year, month);
for (i = 0; i < weekday; i++) {
printf(" ");
}
for (i = 1; i <= days; i++) {
printf("%2d ", i);
if ((i + weekday) % 7 == 0) {
printf("\n");
}
}
if ((i + weekday - 1) % 7 != 0) {
printf("\n");
}
}
// 打印某年的日历
void printYear(int year) {
int i;
for (i = 1; i <= 12; i++) {
printMonth(year, i);
}
}
// 查询某一天是星期几
void queryWeekday(int year, int month, int day) {
char *weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int weekday = (getWeekday(year, month) + day - 1) % 7;
printf("%d-%d-%d is %s\n", year, month, day, weekdays[weekday]);
}
int main() {
int year, month, day;
int choice;
printf("1. Print a calendar for a given year\n");
printf("2. Print a calendar for a given month\n");
printf("3. Query the weekday of a given date\n");
printf("Please enter your choice (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the year: ");
scanf("%d", &year);
printYear(year);
break;
case 2:
printf("Enter the year: ");
scanf("%d", &year);
printf("Enter the month: ");
scanf("%d", &month);
printMonth(year, month);
break;
case 3:
printf("Enter the year: ");
scanf("%d", &year);
printf("Enter the month: ");
scanf("%d", &month);
printf("Enter the day: ");
scanf("%d", &day);
queryWeekday(year, month, day);
break;
default:
printf("Invalid choice!\n");
break;
}
return 0;
}
```
程序运行后会先让用户选择要执行的操作,然后根据用户的选择打印出日历或查询某一天的星期几。其中,`getDays()` 函数用于计算某年某月的天数,`getWeekday()` 函数用于计算某年某月1号是星期几,`printMonth()` 函数用于打印某年某月的月历,`printYear()` 函数用于打印某年的日历,`queryWeekday()` 函数用于查询某一天是星期几。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)