创建一个 C 程序来确定 2021 年给定月份的第一个日期的星期几

Creating a C program that determines week day of 1st date of given month in 2021

我见过很多与我类似的问题,但没有找到任何有帮助的问题。 我想确定给定月份的第一个星期几。示例:2021 年 3 月 1 日或 2021 年 8 月 1 日的工作日。仅向用户询问月份,并给出 2021 年 1 月 1 日是星期五。到目前为止,这是我的代码:

#include <stdio.h>

char* MonthName(int m){

    switch(m){
        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 0;
    }
}

int MonthDays(int m){
    switch(m){
        case 1: 
            return (31);
        case 2: 
            return (28);
        case 3: 
            return (31);
        case 4: 
            return (30);
        case 5: 
            return (31);
        case 6: 
            return (30);
        case 7: 
            return (31);
        case 8: 
            return (31);
        case 9: 
            return (30);
        case 10: 
            return (31);
        case 11: 
            return (30);
        case 12: 
            return (31);
    }
}

char* WeekDay(){
    int m, d, x;
  
    x += 365;
    x += MonthDays(m);

    d = x%7;
    
    switch(d){
    //because the Jan 1 is a friday
    case 0:
        return "Friday";
    case 1:
        return "Saturday";
    case 2:
        return "Sunday";
    case 3:
        return "Monday";
    case 4:
        return "Tuesday";
    case 5:
        return "Wednesday";
    case 6:
        return "Thursday";
    
    }
}


int main(){
    int m; 
    char d;

    printf("Choose a month number from 1-12: ");
    scanf("%d", &m);

    printf("The day of %s 01, 2021 is %s\n", MonthName(m), WeekDay(d));
}

我知道我的代码有点长而且有点乱,但我是 C 编程的新手,我不想要任何高级的东西,因为实际上已经没有任何错误了。我希望不必修改任何 codes/switches,只需修改公式,因为我真的不知道如何获得星期几(请参阅函数 WeekDay(),特别是带有变量 x 的函数d).它打印一个工作日,但它不是正确的工作日。我也想合并 MonthDays 函数,并且大多数与我的问题类似的其他答案都没有使用一个月中的天数。

任何help/advice将不胜感激。谢谢!

在您的函数 WeekDay 中,参数被忽略,一些计算是使用未初始化的局部变量(具有不确定的值)x 完成的,调用 未定义的行为.

而不是这个:

char* WeekDay(){
    int m, d, x;

看来 x 应该是这样的论点:

char* WeekDay(int x){
    int m, d;

此外,从 main 传递给 WeekDayd 也未初始化。您应该在传递之前用适当的值对其进行初始化,或者将参数更改为其他适当的东西。

除了其他答案已经提供的建议外,这是您的 WeekDay() 功能中缺少的内容。

您需要从本质上总结从 1 月 1 日到 % 7 的天数才能得出答案。

所以,首先,将您 WeekDay() 的签名更改为

char* WeekDay(int m){

实际接受从用户读入的月份数。

以 2 月 1 日为例,即自 1 月开始以来的 31 天,即 上一个 月份的天数。将此逻辑扩展到随后的月份,归结为简单地将之前所有月份的天数相加。所以,

int d, x = 0;

for(int i = 2; i <= m; ++i) {
    x += MonthDays(i - 1);
}

d = x%7;

注意x被初始化为0。来到 for 循环,如前所述,总结了 个月的天数。请注意,循环的起始索引为 2,以避免为一月添加任何天数(以防传入 m 的值为 1)。

对于其他函数,MonthName() 函数不会 return 一个值,以防 m 与任何情况都不匹配。 Return 一些默认值(return NULL 或其他)。您可以将数组简化为:

char* days[] = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};

char* monthName[] = ....int numDaysInMonth[] = .... 类似,这只会让你在一个地方清理用户输入 m 并检查它是否在范围内 1-12然后相应地访问数组。记住要处理 0 索引数组,所以 int daysInJan = numDaysInMonth[0] 而不是 numDaysInMonth[1].

编辑: 这是 link to the complete working program. And I'm sharing the program here as per the things mentioned in the latter part of the answer. It might be a matter of convenience mostly at the end of the day but here 它的内容:

#include <stdio.h>

int validMonth(int m) {
    return m >= 1 && m <= 12;
}

char const* MonthName(int m){
    static char const* months[] = {
        NULL, /*For convenience,
         start with NULL for avoiding 0-indexing the months*/
        "January", "February", "March", "April", "May", "June", "July",
        "August", "Septemeber", "October", "Novemeber",  "December"
    };
    return months[m];
}

char const* WeekDay(int m){
    static char const* days[] = {
        /*Starting with the day for 1st January
         NOTE: Here 0 means same as Friday, so no need
         to add a dummy value at 0-index
        */
        "Friday", "Saturday", "Sunday", "Monday",
        "Tuesday", "Wednesday", "Thursday",
    };

    static const int numDaysinMonth[] = {
        /*For convenience start with -1 at the 0-index
        to avoid 0-indexing the months*/
        -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
    };
    
    int x = 0;
  
    for(int i = 2; i <= m; ++i) {
        x += numDaysinMonth[i - 1];
    }
    return days[x % 7];
}


int main(){
    int m; 
    do {
        printf("Choose a month number from 1-12: ");
        scanf("%d", &m);
        if(validMonth(m) != 1) {
            printf("%d doesn't map to any calendar month\n", m);
        } else {
            printf("The day of %s 01, 2021 is %s\n", MonthName(m), WeekDay(m));
        }
    } while (m != 12);
}