1、 使用C语言开发实现一个简单的万年历!包括任意年份的月历,任意日期的阳历转换成农历!搜索某天是星期几 某年是否闰年 打印输出某年的日历 输出今天所在的日历 农历查询 退出 整个代码过程
时间: 2024-02-19 07:02:51 浏览: 162
好的,下面是使用C语言实现一个简单的万年历的代码,包括任意年份的月历、任意日期的阳历转换成农历、搜索某天是星期几、某年是否闰年、打印输出某年的日历、输出今天所在的日历、农历查询和退出。
```
#include <stdio.h>
#include <stdlib.h>
// 定义农历数组
char *chinese_month_name[] = {
"正月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "冬月", "腊月"
};
char *chinese_day_name[] = {
"初一", "初二", "初三", "初四", "初五", "初六",
"初七", "初八", "初九", "初十", "十一", "十二",
"十三", "十四", "十五", "十六", "十七", "十八",
"十九", "二十", "廿一", "廿二", "廿三", "廿四",
"廿五", "廿六", "廿七", "廿八", "廿九", "三十"
};
// 定义全局变量
int year, month, day;
// 声明函数
void print_menu();
int is_leap_year(int year);
int get_days_of_month(int year, int month);
int get_weekday(int year, int month, int day);
void print_month_calendar(int year, int month);
void print_year_calendar(int year);
void print_today_calendar();
void convert_solar_to_lunar(int year, int month, int day);
void lunar_query();
int main() {
int choice;
print_menu();
while (1) {
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1: {
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
print_month_calendar(year, month);
break;
}
case 2: {
printf("请输入年份:");
scanf("%d", &year);
print_year_calendar(year);
break;
}
case 3: {
print_today_calendar();
break;
}
case 4: {
lunar_query();
break;
}
case 5: {
printf("感谢使用,再见!\n");
exit(0);
}
default: {
printf("输入错误,请重新输入!\n");
break;
}
}
printf("\n");
print_menu();
}
return 0;
}
// 打印菜单
void print_menu() {
printf("========================================\n");
printf("欢迎使用万年历!\n");
printf("1. 查询任意年份任意月份的日历\n");
printf("2. 查询某年的日历\n");
printf("3. 查询今天的日历\n");
printf("4. 查询农历信息\n");
printf("5. 退出\n");
printf("========================================\n");
}
// 判断是否闰年
int is_leap_year(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
// 获取某年某月的天数
int get_days_of_month(int year, int month) {
if (month == 2) {
return is_leap_year(year) ? 29 : 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
}
}
// 获取某天是星期几
int get_weekday(int year, int month, int day) {
if (month == 1 || month == 2) {
year--;
month += 12;
}
int century = year / 100;
year %= 100;
int weekday = year + year / 4 + century / 4 - 2 * century + (13 * (month + 1)) / 5 + day - 1;
return (weekday % 7 + 7) % 7;
}
// 打印某年某月的日历
void print_month_calendar(int year, int month) {
printf("%d年%d月的日历如下:\n", year, month);
printf("日 一 二 三 四 五 六\n");
int days_of_month = get_days_of_month(year, month);
int weekday_of_first_day = get_weekday(year, month, 1);
int i;
for (i = 0; i < weekday_of_first_day; i++) {
printf(" ");
}
for (i = 1; i <= days_of_month; i++) {
printf("%2d ", i);
if ((weekday_of_first_day + i) % 7 == 0) {
printf("\n");
}
}
printf("\n");
}
// 打印某年的日历
void print_year_calendar(int year) {
printf("%d年的日历如下:\n", year);
int i;
for (i = 1; i <= 12; i++) {
print_month_calendar(year, i);
}
}
// 打印今天的日历
void print_today_calendar() {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
int year = tm->tm_year + 1900;
int month = tm->tm_mon + 1;
int day = tm->tm_mday;
print_month_calendar(year, month);
}
// 转换阳历为农历
void convert_solar_to_lunar(int year, int month, int day) {
int lunar_year, lunar_month, lunar_day;
lunar_year = year;
lunar_month = 0;
lunar_day = 0;
int i;
for (i = 1900; i <= year; i++) {
int days_of_year = is_leap_year(i) ? 366 : 365;
if (i < year) {
lunar_day += days_of_year;
} else {
int j;
for (j = 1; j <= month - 1; j++) {
lunar_day += get_days_of_month(year, j);
}
lunar_day += day - 1;
}
}
int offset = lunar_day;
int chinese_month_days;
for (i = 1; i <= 12; i++) {
chinese_month_days = 29;
if (i == 2 && is_leap_year(lunar_year)) {
chinese_month_days = 30;
}
offset -= chinese_month_days;
if (offset < 0) {
break;
}
lunar_month++;
}
lunar_day = lunar_day - (chinese_month_days - offset) + 1;
printf("%d年%d月%d日是农历%d年%s%s\n", year, month, day, lunar_year, chinese_month_name[lunar_month], chinese_day_name[lunar_day - 1]);
}
// 查询农历信息
void lunar_query() {
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入日期:");
scanf("%d", &day);
convert_solar_to_lunar(year, month, day);
}
```
以上就是一个简单的万年历的实现,可以根据菜单选择不同的功能。
阅读全文