c语言编写的日历

输入年份如2013,显示2013年的日历。

思路:

1.查找每个月1号是星期几(这里利用了1990年1月1号是星期一)

  计算年份如2013年1月1号到1990年1月1号有Days天,Day%7得到星期索引WeekDay

2.每个月日历打印六行内容,每行七个日期,不是日历内容打印空格

#include <stdio.h>

#define BOOL int
#define TRUE 1
#define FALSE 0

int GetWeekDay(int year, int month, int day);    /* 获取某一年,某一月,某一天是星期几 */
void PrintCalendar(int year);                    /* 打印第year年的日历 */
BOOL IsLeap(int year);                            /* 判断是否为闰年 */

int main()
{
    int year;
//    char* week[] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; /* 字符指针数组 */
    
//    int WeekDay = GetWeekDay(2000, 1, 1);

//    printf("今天是星期:%s\n", week[WeekDay]);


    printf("请输入要查询的年份:\n");

    scanf("%d", &year);

    PrintCalendar(year);

    return 0;
}
/*-------------------------------------------------------------------------
    功能:获取year年,month月,day天是星期几 这里利用了1900年1月1号是星期一
    输入:年份year,月份month日期day
    输出:星期索引
---------------------------------------------------------------------------*/
int GetWeekDay(int year, int month, int day)
{
    int count, Week_Index,i;
    int Day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};                            /* 每月的天数 */
    int MonthAdd[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};                    /* 每月前所有月份的天数之和 */
    

    count = MonthAdd[month - 1];                                                                /* 月份month前面所有月份的天数 */
    count = count + (year - 1900) * 365;            
    count = count + day;                                                                        /* 与1900年1月1号相差多少天 */
    if(month > 2 && IsLeap(year))                                                                /* 月份month超过2月份 且是闰年 */
        count ++;
    for(i = 1900; i < year; i ++)
    {
        if(IsLeap(i))                                                                            /* 如果是闰年 */
        {
            count ++;
        }
    }

    Week_Index = count % 7;

    return Week_Index;
}
/*------------------------
    功能:打印year年的日历
    输入:year年份
    输出:year年的日历
--------------------------*/
void PrintCalendar(int year)
{
    int i, j, k;
    int WeekDay;
    int Day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int MonthDays;

    for(i = 1; i < 13; i ++)                        /* 依次打印每个月份的日历 */
    {
        int temp = 1;
        MonthDays = Day[i];
        if(IsLeap(year) && i == 2)                    /* 闰年第二个月为29天 */
            MonthDays = 29;
        WeekDay = GetWeekDay(year, i, 1);            /* 获取每个月1号 星期索引 */
        printf("%d月\n", i);
        printf("日\t一\t二\t三\t四\t五\t六\n");
        for(j = 1; j <= 42; j ++)                    /* 每个月日历打印六行 */
        {
            if(WeekDay != 0)
            {
                printf("\t");
                WeekDay --;
                if(j % 7 == 0)
                    printf("\n");
                continue;
            }    
            if(MonthDays > 0)                        /* 每个月的日历 */
            {
                printf("%d\t", temp);                    
                temp ++;
                if(j % 7 == 0)
                        printf("\n");
                MonthDays --;
            }
            else
                printf("\t");            
        }
        printf("\n");
    }
}
/*--------------------------
    功能:判断year是否为闰年
    输入:年份year
    输出:闰年TRUE平年FALSE
---------------------------*/
BOOL IsLeap(int year)
{
    BOOL result;
    if(((year % 100 == 0) && (year %400 == 0)) || ((year % 100 != 0) && (year % 4 == 0)))    /* 闰年 */
    {
        result = TRUE;
    }
    else
        result = FALSE;

    return result;
}

 

Please call me JiangYouDang!
原文地址:https://www.cnblogs.com/luckygxf/p/3494696.html