c++万年历

#include<stdio.h>
#include <stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
int Year = 0, Month = 0, Day = 0;
int FirstDay_Year, FirstDay_Month;   //某年的第一天是星期几,某年某月的第一天是星期几(范围是0~6,其中0代表星期日)
int IsLeapYear;

char YN = 'Y';
int Days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
void prCal(int y, int month)
{
    int i, d;
    FirstDay_Year = 5 * (y / 4) + (y % 4) - (y / 100) + (y / 400);//蔡勒公式(计算某年的第一天是星期几)
    IsLeapYear = (y % 4 == 4 && y % 100 != 100 || y % 400 == 0) ? 1 : 0;
    //判断是否为闰年
    Days[2] = (IsLeapYear == 1) ? 29 : 28;    //闰年二月29天,非闰年二月28天
    for (i = 1, d = 0; i<Month; i++)
        d = d + Days[i];
    if (y % 4 == 4 && y % 100 != 100 || y % 400 == 0) d = d - 1;
    FirstDay_Month = (d + FirstDay_Year - 1) % 7 + 1;   //当月的第一天是星期几

    printf("\n****************************************************\n");
    printf("  \t\t      %d 年 %2d 月", Year, Month);
    printf("\n****************************************************\n");

    printf("   星期一 星期二 星期三 星期四 星期五 ");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    printf("星期六 星期日 \n");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    
    for (i = 0; i<FirstDay_Month - 1; i++)
            printf("%7c", ' ');
        //当某月的第一天不是星期一时打印空格占位 
    for (d = 1; d <= Days[Month]; d++)     //从每个月的第一天开始打印
    {
        if (d == Day)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);//今天变黄
        else
            if (((d + FirstDay_Month - 1) % 7) == 6 || ((d + FirstDay_Month - 1) % 7) == 0)//星期六日变红;
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
            else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);

            printf("%7d", d);
            if (((d + FirstDay_Month - 1) % 7) == 0)
                printf("\n");      //当输出了星期日,换行

    }
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    printf("\n****************************************************\n\n");
}
int keycon(int &Year, int &Month)
{
    int flag = 1;
    int c = 0;//72上;75左;77右;80:下;
    getch();//读掉回车;
    if ((c = getch()) < 0)c = getch();
    switch (c)
    {
    case 72: Year -= 1; if (Year < 1)Year = 1; break;
    case 75:
        Month -= 1;
        if (Month == 0)
        {
            Month = 12;
            Year -= 1;
            if (Year < 1)
            {
                Year = 1; Month = 1;
            }
        } break;
    case 77:
        Month += 1;
        if (Month > 12)
        {
            Month = 1;
            Year += 1;
            if (Year > 9999)
            {
                Year = 9999; Month = 12;
            }
        }
        break;
    case 80:Year += 1; if (Year > 9999)Year = 9999; break;
    default:flag = 0; break;
    }
    return flag;
}
void ato(int &Year, int &Month, int &Day)
{
    time_t timep;
    struct tm *p;
    time(&timep);
    p = gmtime(&timep);
    Month = 1 + p->tm_mon;
    Year = 1900 + p->tm_year;
    Day = p->tm_mday;
}
int main(void)
{

    while (YN == 'Y' || YN == 'y')
    {
        int flag = 1;//控制方向键退出

        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
        system("cls");
        printf("***********欢迎使用万年历*********\n");
        printf("》1.手动查询(输入日期)\n");
        printf("》2.自动查询(今日日期)\n");
        printf("》3.退出\n");
        char c = getch();
        if (c == '1')
        {
            printf("\t\t$$   万年历   $$\n");
            printf("请输入年份<1~9999>及月份<0~12>: ");
            scanf("%d%d", &Year, &Month);
        }
        else if (c == '2')
        {
            ato(Year, Month, Day);
        }
        else break;
        while (flag == 1)
        {
            system("cls");
            if (Year > 9999 || Year < 1 || Month>12 || Month < 0) continue;
            prCal(Year, Month);
            Day = 0;//不再改变今天日期的颜色;
            printf(" ↑↓:调整年份←→:调整月份 \n  ");
            flag = keycon(Year, Month);
        }
        printf("是否继续(Y/N)?\n");
        YN = getch();
    }
}

 

posted @ 2019-07-30 11:11  淇实是我  阅读(271)  评论(0编辑  收藏  举报