用c语言代码实现万年历查询程序 功能要求: (1)提供菜单方式选择 (2)输入年份、月份、日期,计算得到的是这一天据今天有多少天,星期几; (3)输入公历的年月日,输出农历年月日;
时间: 2024-05-11 15:16:00 浏览: 103
好的,我知道您提出的要求,并将为您提供回答。下面是实现万年历查询程序的C语言代码。
```
#include <stdio.h>
// 声明函数
void menu();
int is_leap_year(int year);
int get_day_of_year(int year, int month, int day);
int get_weekday(int year, int month, int day);
int get_year_days(int year);
void get_lunar_date(int year, int month, int day);
int get_year_days(int year);
int get_lunar_month_days(int year, int month);
void get_lunar_month_day(int year, int month, int day);
int main() {
int year, month, day;
int choice;
while (1) {
menu();
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入日期:");
scanf("%d", &day);
int day_of_year = get_day_of_year(year, month, day);
int weekday = get_weekday(year, month, day);
printf("这一天据今天有 %d 天\n", day_of_year);
printf("星期 %d\n", weekday);
break;
case 2:
printf("请输入公历的年月日,例如:2022 3 3\n");
scanf("%d %d %d", &year, &month, &day);
printf("公历 %d 年 %d 月 %d 日对应的农历日期为:", year, month, day);
get_lunar_date(year, month, day);
printf("\n");
break;
case 3:
printf("感谢您使用本程序,再见!\n");
return 0;
default:
printf("无效的选项,请重新输入!\n");
break;
}
}
}
// 输出菜单
void menu() {
printf("\n===== 万年历查询程序 =====\n");
printf("1. 查询这一天据今天有多少天,星期几\n");
printf("2. 查询公历日期对应的农历日期\n");
printf("3. 退出程序\n");
}
// 判断是否是闰年
int is_leap_year(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
} else {
return 0;
}
}
// 获取当天是这一年的第几天
int get_day_of_year(int year, int month, int day) {
int days = 0;
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days += 31;
break;
case 4:
case 6:
case 9:
case 11:
days += 30;
break;
case 2:
if (is_leap_year(year)) {
days += 29;
} else {
days += 28;
}
break;
}
}
days += day;
return days;
}
// 获取该天是星期几
int get_weekday(int year, int month, int day) {
int k = year % 100;
int j = year / 100;
int q = day;
int m = month;
int h = (q + 26 * (m + 1) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;
return (h + 6) % 7 + 1;
}
// 获取该年共有多少天
int get_year_days(int year) {
int days = 0;
for (int i = 1; i <= 12; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days += 31;
break;
case 4:
case 6:
case 9:
case 11:
days += 30;
break;
case 2:
if (is_leap_year(year)) {
days += 29;
} else {
days += 28;
}
break;
}
}
return days;
}
// 获取农历日期
void get_lunar_date(int year, int month, int day) {
int days = 0;
int lunar_year = year;
int lunar_month = 1;
int lunar_day = 1;
while (1) {
days += get_year_days(lunar_year);
if (days > get_day_of_year(year, month, day)) {
days -= get_year_days(lunar_year);
break;
}
lunar_year++;
}
for (int i = 1; i <= 12; i++) {
int days_of_month = get_lunar_month_days(lunar_year, i);
if (days >= days_of_month) {
days -= days_of_month;
lunar_month++;
} else {
break;
}
}
lunar_day += days;
get_lunar_month_day(lunar_year, lunar_month, lunar_day);
}
// 获取农历年份中该月有多少天
int get_lunar_month_days(int year, int month) {
int month_day = 29;
if ((year & 0x03) == 0 && month == 2) {
month_day = 30;
} else if ((year & 0x03) != 0 && month == 3) {
month_day = 30;
}
if ((0x8000 >> (month - 1)) & 0x0f) {
month_day++;
}
return month_day;
}
// 获取农历年月日
void get_lunar_month_day(int year, int month, int day) {
char *g_month[] = {"*","正","二","三","四","五","六","七","八","九","十","冬","腊"};
char *g_day[] = {"*","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
char *zodiac[] = {"猴","鸡","狗","猪","鼠","牛","虎","兔","龙","蛇","马","羊"};
printf("%s年", zodiac[(year - 4) % 12]);
printf("%s月", g_month[month]);
printf("%s", g_day[day]);
}
```
以上就是实现万年历查询程序的C语言代码,您可以根据需要对代码进行适当的修改和调整。希望能够帮到您!
阅读全文