天花板編程手把手計劃-第1期-第7天-打卡

題目

請編程實現一個功能,輸入任意一個日期,計算出那一天是星期幾。這道題沒有任何限制,你可以設計任何自己喜歡的交互形式,可以使用任何自己能想到的算法。

解題思路

因為之前寫過打印任意年份日歷的題,所以這道題相對難度較小。
我們這次思考如何寫一個讓使用者感覺舒服的程序把。

源碼

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void getDate(int *pYear, int *pMonth, int *pDay);//獲取日期
int checkDate(int year, int month, int day);//檢查輸入日期是否合格
int calcWeekDay(int year, int month, int day);//計算當天星期數
int leapYear(int year);//計算是否為閏年,是返回1,否返回0;參數(欲計算的年份)
int yearWeekDays(int year);//返回本年首天周幾的函數;參數(欲計算的年份)

int g_monthDays[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

void main()
{
    int year, month, day;
    int weekday;
    getDate(&year, &month, &day);
    weekday = calcWeekDay(year, month, day);
    /*打印結果*/
    printf("\n\n\t\t\t\t\t\t  %d.%02d.%02d星期%d\n", year, month, day, weekday);
    printf("\n\n\t\t\t\t\t輸入<ENTER>再次查詢,其他任意鍵退出\n");
    fflush(stdin);   //獲取字符前先清空緩沖區,也可以使用 rewind(stdin);
    if (getch() == 13)//如果輸入是enter清屏再次查詢,否則退出查詢
    {
        system("cls");
        main();
    }
    else
    {
        exit(0);
    }
}

void getDate(int *pYear, int *pMonth, int *pDay)
{
    while (1)
    {
        printf("\n\n\n\n");
        printf("\t\t\t\t**************************************************\n");
        printf("\t\t\t\t              任意日期查詢星期數工具            \n\n");
        printf("\t\t\t\t                    年:");
        scanf("%d", pYear);
        printf("\t\t\t\t                    月: ");
        scanf("%d", pMonth);
        printf("\t\t\t\t                    日: ");
        scanf("%day", pDay);
        printf("\t\t\t\t**************************************************\n");
        if (checkDate(*pYear, *pMonth, *pDay))//如果輸入合格跳出循環
        {
            break;
        }
        else
        {
            system("cls");
            printf("\n\n\n\t\t\t\t\t   輸入日期不合格,請重新輸入\n");
        }
    }
}

int checkDate(int year, int month, int day)//檢查輸入是否合格
{
    if (year > 0 && month >= 1 && month <= 12 && day <= 31)
    {
        if (month == 2)//如果輸入是二月是否合格
        {
            if (leapYear(year))
            {
                if (day <= 29)
                {
                    return 1;
                }
            }
            else 
            {
                if (day <= 28)
                {
                    return 1;
                }
            }
        }
        else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            if (day <= 31)
            {
                return 1;
            }
        }
        else
        {
            if (day <= 30)
            {
                return 1;
            }
        }

    }
    return 0;
}

int calcWeekDay(int year, int month, int day)
{
    int yearWeekDay, result;
    int days = 0;
    for (int i = 0; i < month - 1; i++)
    {
        days += g_monthDays[i];
    }
    days += day;
    if (leapYear(year) && month > 2)
    {
        days++;
    }
    yearWeekDay = yearWeekDays(year);
    result = yearWeekDay + (days - 1) % 7;
    result %= 7;
    return result;
}

int leapYear(int year)
{
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        return 1;
    else
        return 0;
}

int yearWeekDays(int year)
{
    return ((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7 + 1;
}

執行結果

1
2
3

總結

現在再看上一期的21天代碼訓練營已經都能看懂了,試試繼續跟著寫那一個系列的程序把。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容