c 语言 编写万年历程序,输入年、月,输出该月的日历表。输入年,输出该年的日历表。
时间: 2024-02-28 18:53:55 浏览: 183
好的,以下是一份C语言代码,可以实现输入年、月份,输出该月的日历表:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int get_weekday(int year, int month, int day)
{
int h;
int q = day;
int m = month;
int K = year % 100;
int J = year / 100;
h = (q + 13 * (m + 1) / 5 + K + K / 4 + J / 4 + 5 * J) % 7;
return h;
}
bool is_leap_year(int year)
{
if (year % 4 == 0 && year % 100 != 0)
return true;
else if (year % 400 == 0)
return true;
else
return false;
}
void print_month_calendar(int year, int month)
{
int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char *month_name[] = {"", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
if (month == 2 && is_leap_year(year))
days_in_month[2]++;
int first_day = get_weekday(year, month, 1);
printf("%s %d\n", month_name[month], year);
printf("Mo Tu We Th Fr Sa Su\n");
int day_counter = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
if (i == 0 && j < first_day)
printf(" ");
else if (day_counter > days_in_month[month])
printf(" ");
else
printf("%2d ", day_counter++);
}
printf("\n");
}
}
void print_year_calendar(int year)
{
for (int i = 1; i <= 12; i++) {
print_month_calendar(year, i);
printf("\n");
}
}
int main()
{
int year, month;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
if (month >= 1 && month <= 12)
print_month_calendar(year, month);
else {
printf("月份输入有误,请输入1-12之间的数字!\n");
exit(EXIT_FAILURE);
}
return 0;
}
```
这个程序使用了与Python代码相同的蔡勒公式来计算星期几,使用了一个数组来存储每个月的天数,使用了一个字符串数组来存储月份名称。在打印日历的过程中,我们可以使用两层循环,外层循环是6行(一个月中最多只有6行),内层循环是7列(一周有七天)。
如果您需要输出某一年的日历表,可以使用以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int get_weekday(int year, int month, int day)
{
int h;
int q = day;
int m = month;
int K = year % 100;
int J = year / 100;
h = (q + 13 * (m + 1) / 5 + K + K / 4 + J / 4 + 5 * J) % 7;
return h;
}
bool is_leap_year(int year)
{
if (year % 4 == 0 && year % 100 != 0)
return true;
else if (year % 400 == 0)
return true;
else
return false;
}
void print_month_calendar(int year, int month)
{
int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char *month_name[] = {"", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
if (month == 2 && is_leap_year(year))
days_in_month[2]++;
int first_day = get_weekday(year, month, 1);
printf("%s %d\n", month_name[month], year);
printf("Mo Tu We Th Fr Sa Su\n");
int day_counter = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
if (i == 0 && j < first_day)
printf(" ");
else if (day_counter > days_in_month[month])
printf(" ");
else
printf("%2d ", day_counter++);
}
printf("\n");
}
}
void print_year_calendar(int year)
{
for (int i = 1; i <= 12; i++) {
print_month_calendar(year, i);
printf("\n");
}
}
int main()
{
int year;
printf("请输入年份:");
scanf("%d", &year);
print_year_calendar(year);
return 0;
}
```
这个程序使用了一个循环,从1到12输出每个月的日历。通过这个程序,您就可以实现一个简单的万年历啦!
阅读全文