C语言“打印日历”的程序是什么?

C语言“打印日历”的程序是什么?,第1张

#include \x0d\x0avoid main() \x0d\x0a{ \x0d\x0aint i,j,k,year,week\x0d\x0aint day[]={31,28,31,30,31,30,31,31,30,31,30,31}\x0d\x0achar month[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"}\x0d\x0a\x0d\x0aprintf("Input year you want to print:")\x0d\x0ascanf("%d",&year)\x0d\x0a\x0d\x0aif((year%4==0&&year%100!=0)||(year%400==0)) day[1]++//判断是否闰年\x0d\x0aweek=(35+year+year/4-year/100+year/400)%7//计算当年的1月1日是周几的公式 \x0d\x0a\x0d\x0afor(i=0i=7) \x0d\x0a{ \x0d\x0aprintf("\n")\x0d\x0aweek=week%7\x0d\x0a} \x0d\x0a} \x0d\x0aprintf("\n")\x0d\x0a} \x0d\x0a}

#include <conio.h>

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

const monthDay[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}

int isLeap(int year)

{

    if(year % 4) return 0

    if(year % 400) return 1

    if(year % 100) return 0

    return 1

}

int getWeek(int year, int month, int day)

{

    int c, y, week

    if(month == 1 || month == 2) //判断month是否为1或2 

    {

        year--

        month+=12

    }

    c= year / 100

    y = year - c * 100

    week = (c / 4) - 2 * c + (y + y / 4) + (13 * (month + 1) / 5) + day - 1

    while(week < 0) {week += 7}

    week %= 7

    return week

}

void display(int year, int month)

{

    int monthDays, weekFirst, i

    monthDays = monthDay[month] + (month==2 ? isLeap(year) : 0)

    weekFirst = getWeek(year, month, 1)

    system("cls")

    printf("             -------%4d年----%2d月-------\n", year, month)

    printf("  星期日  星期一  星期二  星期三  星期四  星期五  星期六\n")

    for(i=0 i<weekFirst i++) printf("        ")

    for(i=1 i<=monthDays i++)

    {

        printf("%8d", i)

        weekFirst++

        if(weekFirst>=7) {printf("\n") weekFirst=0}

    }

}

void main()

{

    int year, month, chr

    time_t timer

    struct tm *tblock

    timer = time(NULL)

    tblock = localtime(&timer)

    year = tblock->tm_year + 1900

    month = tblock->tm_mon +1

    while(1)

    {

        display(year, month)

        chr = getch()

        if(chr == 0xe0)

        {

            chr = getch()

            if(chr == 0x4b) /* 方向键(←) */

            {

                month --

                if(month<1) {month = 12 year--}

            }

            else if(chr == 0x4d) /* 方向键(→) */

            {

                month ++

                if(month>12) {month = 1 year++}

            }

        }

        else if(chr == 'q' || chr == 'Q') break

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11927635.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存