一款简易的万年历(C语言)

 

这是一个C语言程序,用于打印指定年份或月份的日历。

首先定义了几个函数,用于判断闰年、获取某年某月的天数、计算1900年到指定年月的总天数以及打印日历。然后在main函数中,根据用户输入的选择,执行相应操作(显示某个月的日历或显示整个年份的日历)。

以下是程序各部分的简要说明:

  1. is_leap_year(int year):判断指定年份是否为闰年。
  2. get_month_days(int year, int month):获取指定年份和月份对应的天数。
  3. total_days_from_1900(int year, int month):计算从1900年到指定年月之间的总天数。
  4. print_calendar_header():打印日历表头。
  5. print_month_calendar(int year,int month):打印指定年份和月份的日历。
  6. print_year_calendar(int year):打印指定年份的整个日历。

在主函数main()中,根据用户输入的选项:

1.表示显示某个月的日历;

2.表示显示整个年份的日历;

调用相应函数完成任务。如果用户输入无效选项,则输出错误信息。

思路:

  1. 定义结构体保存日期信息,包括年、月、日、星期几等。
  2. 通过输入年份和月份,计算出该月的天数和该月第一天是星期几。
  3. 根据计算出的天数和星期几,输出该月的日历。
  4. 对于闰年的情况,需要特殊处理。
  5. 可以添加一些额外功能,比如查询某一天是星期几、查询某一年是否为闰年等。 代码实现:
#include <stdio.h>

// 判断是否为闰年
int is_leap_year(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// 获取某年某月的天数
int get_month_days(int year, int month) {
    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (is_leap_year(year) && month == 2) {
        return days[month -1] +1;
    }
    return days[month -1];
}

// 计算1900年到指定年月的总天数
int total_days_from_1900(int year, int month) {
    int total_days =0;

    for (int y =1900; y < year; ++y) {
        total_days += is_leap_year(y)?366:365;
    }

    for (int m=1; m<month; ++m){
        total_days +=get_month_days(year,m);
    }

    return total_days;
}

// 打印月历表头
void print_calendar_header() {
    printf("Sun Mon Tue Wed Thu Fri Sat");
}

// 打印一个月的日历
void print_month_calendar(int year,int month){
	printf("%4d Year %02d Month",year,month);
	print_calendar_header();
	int start_day=(total_days_from_1900(year,month)+1)%7;
	for(int i=0;i<start_day;++i){
		printf("    ");
	}
	for(int day=1;day<=get_month_days(year,month);++day){
		printf("%3d ",day);
		if((start_day+day)%7==0){
			printf(" ");
		}
	}
	printf("");
}

// 打印一整年的日历
void print_year_calendar(int year) {
    for (int month = 1; month <= 12; ++month) {
        print_month_calendar(year, month);
    }
}

int main() {
    int choice;
    printf("Please input your choice:\n");
    printf("1. Input year and month to display the month calendar\n");
    printf("2. Input year to display the whole year calendar\n");


    scanf("%d", &choice);

    if (choice == 1) {
        int year, month;
        printf("Please input the year:");
        scanf("%d", &year);
        printf("Please input the month:");
        scanf("%d", &month);
        print_month_calendar(year, month);
        
    } else if (choice == 2) {
        int year;
        printf("Please input the year:");
        scanf("%d", &year);
        print_year_calendar(year);

    } else {
        printf("Input error, please try again");
    }

	return 0;
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

+1MB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值