• C语言实现简单万年历


    需求:用C语言实现简单万年历

    #include <stdio.h>
    #include<stdlib.h>
    int year, month;
    int run[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
    int ping[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    char week[7][10] = { "SUN","MON","TUE","WED","THU","FRI","SAT" };
     
    bool IsLeap(int year)
    {
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))    //判断闰年
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    void PrintStar()
    {
        printf("			万年历			
    ");
        printf("****************************************************
    ");
        for (int i = 0; i < 7; i++)
        {
            printf("%s	", week[i]);
        }
        printf("
    ");
    }
    int DaySum(int year, int month)
    {
        int sum = 0;
        for (int i = 1990; i < year; i++)
        {
            if (IsLeap(i))
            {
                sum += 366;
            }
            else
            {
                sum += 365;
            }
        }
        for (int i = 0; i < month - 1; i++)
        {
            if (IsLeap(year))
            {
                sum += run[i];
            }
            else
            {
                sum += ping[i];
            }
        }
        return sum;
    }
     
    void PrintCal(int sum, int year, int month)
    {
        int nRes, nTmp;
        nRes = sum % 7+1;//前面空几格
        nTmp = 7 - nRes;//从第几格开始打印
        for (int i = 0; i < nRes; i++)
        {
            printf("	");
        }
        if (IsLeap(year))
        {
            for (int i = 1; i < run[month - 1]+1; i++)
            {
                printf("%d	", i);
                if (nTmp == i || (i - nTmp) % 7 == 0)
                {
                    printf("
    ");
                }
            }
            printf("
    ");
        }
        else
        {
            for (int i = 1; i < ping[month - 1]+1; i++)
            {
                printf("%d	", i);
                if (nTmp == i || (i - nTmp) % 7 == 0)
                {
                    printf("
    ");
                }
            }
            printf("
    ");
        }
     
    }
    int main(void)
    {
        while (1)
        {
            scanf("%d %d", &year, &month);
            PrintStar();
            PrintCal(DaySum(year, month), year, month);
            printf("****************************************************
    ");
        }
        
        return 0;
     
     
    }

    运行效果如下:

  • 相关阅读:
    名信片定做网站
    [置顶] 数据库开发常识
    <IMG>中UserMap的用法
    阿斯钢iojeg9uhweu9erhpu9hyw49
    慎得慌二u赫然共和任务i个屁
    阿斯钢iojeg9uhw8uhy平
    2014校园招聘笔、面经历总结---华为双选会
    PDCA模型的学习
    阿斯钢 弇7人后偶尔一个味5而已
    【HTML】让<pre>标签文本自动换行
  • 原文地址:https://www.cnblogs.com/zzj420133722/p/12900612.html
Copyright © 2020-2023  润新知