输出1900年之后任意一年的日历

题目:输出1900年之后任意一年的日历。原题出自《C语言的科学与艺术》P132

/*  File name:calendar.c
    Function:generate a calendar for a year
    Time:2018.04.08.00:02
    the source code from book《The art and science of C》,eric roberts
    edited by qjx  
*/

#include <stdio.h>

#define Sunday 0    //easy to qiumo(%)
#define Monday 1
#define Tuesday 2
#define Wednesday 3
#define Thursday 4
#define Friday 5
#define Saturday 6

void GiveInstruction(void);        //tell the input rules
int GetYearFromUser(void);
void PrintCalendar(int year);
void PrintCalendarMonth(int month,int year);    //the essence of this program
void IndentFirstLine(int weekday); //The spacebar of the first row of every month
char *MonthName(int month); //this function as string(char) function can transmit string to display
int MonthDays(int month,int year); //transmit the days of every month
int IsLeapYear(int year); //judge the input number is leapyear or not

main()
{
    int Year;
    GiveInstruction();
    Year=GetYearFromUser();
    PrintCalendar(Year);
}

void GiveInstruction(void)    //tell the user how to do
{
    printf("This program displays a calendar for a full\n");
    printf("year. The year must not be before 1900.\n");
}

int GetYearFromUser(void)
{
    int year;
    while(1){
        printf("Which year?");
        scanf("%d",&year);
        if(year>1900) return (year); //while the input is right,function will return
        printf("The year must be at least 1900.\n"); //otherwise,waring and input again
    }
}

void PrintCalendar(int year)
{
    int month;
    for(month=1;month<=12;month++)
    {
        PrintCalendarMonth(month,year); //print the every month is the essence of program
        printf("\n");
    }
}

void PrintCalendarMonth(int month,int year)
{
    int weekday,nDays,day;
    printf("   %s %d\n",MonthName(month),year); //the head of the monthly calendar
    printf(" Su Mo Tu We Th Fr Sa\n");
    nDays=MonthDays(month,year); //get the days of month,which will call IsLeapYear function
    weekday=FirstDayOfMonth(month,year); //know first day of every month is which one of weekday
    IndentFirstLine(weekday); //print the spacebar before the first day
    for(day=1;day<=nDays;day++)
    {
        printf(" %2d",day); //printf the day
        if(weekday==Saturday) printf("\n");
        //if saturday,line feed;if not,the final weekday will line feed
        weekday=(weekday+1)%7; //calculate next day
    }
    if (weekday!=Sunday) printf("\n");
    //if not sunday,line feed;if sunday,the former weekday is sturday,has already line feed
}

void IndentFirstLine(int weekday) //The spacebar of the first row of every month
{
    int i;
    for(i=0;i<weekday;i++)
    {
        printf("   ");
    }
}

char *MonthName(int month) //transmit the string of months
{
    switch(month)
    {
        case 1:return ("January");
        case 2:return ("February");
        case 3:return ("March");
        case 4:return ("April");
        case 5:return ("May");
        case 6:return ("June");
        case 7:return ("July");
        case 8:return ("August");
        case 9:return ("September");
        case 10:return ("October");
        case 11:return ("November");
        case 12:return ("December");
        default:return ("Illegal month");
    }
}

//calculate the numbers of every month,notice need to call IsLeapYear function
int MonthDays(int month,int year)
{
    switch(month)
    {
        case 2:
            if(IsLeapYear(year))
                return 29;
            return 28;
        case 4: case 6: case 9: case 11:
            return 30;
        default:
            return 31;
    }
}

int FirstDayOfMonth(month,year) //know first day of every month is which one of weekday
{
    int weekday,i;
    weekday=Monday;
    for(i=1900;i<year;i++) //first according to the year
    {
        weekday=(weekday+365)%7; // % can get the weekday
        if(IsLeapYear(i))  weekday=(weekday+1)%7;
    }
    for(i=1;i<month;i++) //second according to the month
    {
        weekday=(weekday+MonthDays(i,year))%7;
    }
    return weekday;
}

int IsLeapYear(int year) //judge the input number is leapyear or not
{
    return ((year%400==0)||((year%4==0)&&(year%100!=0)));
}


C语言中,打印一年日历通常需要使用一些循环和条件判断来处理月份的数变化。以下是一个简单的示例,展示如何打印2023日历: ```c #include <stdio.h> #include <stdlib.h> // 函数来获取给定月份的数 int days_in_month(int month, int year) { switch (month) { case 2: if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return 29; // 考虑闰 else return 28; case 1, 3, 5, 7, 8, 10, 12: return 31; // 非闰都是31 default: return 30; // 4, 6, 9, 11月非闰30 } } void print_calendar(int year) { for (int i = 1; i <= 12; i++) { printf("%d\n", year); printf(" "); for (int j = 1; j <= 7; j++) { printf("%2d ", j); // 两位数显示 } printf("\n"); int day_of_week = (year + (i - 1) / 12) * 26 + (i + 2) / 10 + (i % 10 > 4 ? 1 : 0); // 根据蔡勒公式计算星几 int start_day = (day_of_week - 1 + 7) % 7; // 转换到一周中的第一 for (int k = 1; k < start_day; k++) { printf(" "); // 填充空白 } printf("%d", year % 100); // 显示份的最后两位 for (int l = 1; l <= days_in_month(i, year); l++) { if (l == start_day + 1) { printf("*"); // 初始标记 } else { printf("%2d ", l); } } printf("\n"); } } int main() { int year = 2023; print_calendar(year); return 0; } ``` 这个程序首先定义了一个函数`days_in_month`来获取指定月份的数,然后通过`print_calendar`函数按行打印日历,这是一个简化版本,没有考虑到更复杂的格式美化和闰规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吉大秦少游

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

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

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

打赏作者

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

抵扣说明:

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

余额充值