如何在正确的工作日下打印日期?

#include <stdio.h>
#include <math.h>


const char weekday_str[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
const char month_str[13][4] = {"", "JAN", "FEB", "MAR", "APR", "MAY", "JUN","JUL", "AGO", "SEP", "OCT", "NOV", "DEC"};
const int days_in_months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int isLeap(int year);
int weekDay(int y, int m, int d);
void calendar(int year, int month);

int main () {

    int mm,yyyy;
    puts(":");
    scanf("%d%*c%d", &mm, &yyyy);

    calendar(yyyy,mm);

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

int weekDay(int yr, int mh, int d){

    int y, c, m, w;

    if (mh == 1 || mh == 2)
        yr--;

    y = yr%100;
    c = (yr - y)/100;

    if (mh == 1 || mh == 2)
        m = mh + 10;
    if (mh<= 12 || mh>=3)
        m = mh - 2;

    w = ((d + floor(2.6*m - 0.2) + y + floor(0.25*y) + floor(0.25*c) - 2*c ));
    w = w%7;

    if(w < 0)
        w += 7;

return w;
}

void calendar(int year, int month){

    int days, wd;

    puts("===================================================");
    printf("                      %c%c%c/%d\n",month_str[month][0],month_str[month][1],month_str[month][2],year);
    puts("\nSUN     MON     TUE     WED     THU     FRI     SAT");

    if(month == 2)
    {
        if(isLeap(year)==1)
            days = 29;
        if(isLeap(year)==0)
            days = 28;
    }
    else
        days = days_in_months[month];

// i need to correct this part
    //print dates
    for (int d = 1; d <= 1 + weekDay(year,month,d) * 5; d++ )
        {
            printf("    ");
        }
    for (int i = 1; i <=days; i++)
    {
       wd = weekDay(year,month, i);
       printf("%.2d",i);

       if ((wd+1) % 7 == 0)
            printf("\n");
        else
            printf("      ");    
    }
    puts("\n===================================================");
}

当我输入2/2015时,它会给出无限循环。(现在我没有无限循环的问题)

例:   输入:2 2020   |输出:

===================================================
                    FEB/2020
SUN     MON     TUE     WED     THU     FRI     SAT
--      --      --      --      --      --      01
02      03      04      05      06      07      08
09      10      11      12      13      14      15
16      17      18      19      20      21      22
23      24      25      26      27      28      29
===================================================

输入:02 2015   |输出:

===================================================
                    FEB/2015
SUN     MON     TUE     WED     THU     FRI     SAT
01      02      03      04      05      06      07
08      09      10      11      12      13      14
15      16      17      18      19      20      21
22      23      24      25      26      27      28
===================================================
  • 我不知道如何使用结构来解决这一挑战。
  • 我只有输出有问题

更多详细信息,更多详细信息,更多详细信息,更多详细信息,更多详细信息,更多详细信息,更多详细信息,更多详细信息,更多详细信息,更多详细信息,更多详细信息

评论
  • ket
    ket 回复

    Unless I'm misunderstanding something, the only thing you are worried about is how to align the first partial week of numbers under the weekday headings. Since the first day of every month is the 1st, you can just concentrate on weekDay(year,month,1).

    Additionally, since the days are all separated by 8-characters (one normal tab width), you can simply insert 8-spaces for each weekday offset. So you essentially need to pad the first day for a total number of spaces:

    padding = weekDay(year,month,1) * 8;
    

    That allows you to eliminate your loop and simply use a field-width modifier that is set by padding as the first argument to printf when outputting a space, e.g.

           int days, wd, padding;     /* add padding variable */
           ...
    
    //     for (int d = 1; d <= 1 + weekDay(year,month,d) * 5; d++)
    //     {
    //         printf("    ");
    //     }
        if ((padding = weekDay(year,month,1) * 8))
            printf ("%*s", padding, " ");
    

    Or after computing the padding, you could simply loop that number of times outputting spaces, e.g.

        padding = weekDay(year,month,1) * 8;
        while (padding--)
            putchar (' ');
    

    两种方法的结果相同。

    使用/输出示例

    这似乎可以完成您正在尝试的操作,例如

    2020年3月

    $ ./bin/calendar
    enter mm yyyy : 3 2020
    ===================================================
                          MAR/20
    
    SUN     MON     TUE     WED     THU     FRI     SAT
    01      02      03      04      05      06      07
    08      09      10      11      12      13      14
    15      16      17      18      19      20      21
    22      23      24      25      26      27      28
    29      30      31
    ===================================================
    

    2020年4月:

    $ ./bin/calendar
    enter mm yyyy : 4 2020
    ===================================================
                          APR/20
    
    SUN     MON     TUE     WED     THU     FRI     SAT
                            01      02      03      04
    05      06      07      08      09      10      11
    12      13      14      15      16      17      18
    19      20      21      22      23      24      25
    26      27      28      29      30
    ===================================================
    

    2020年5月:

    $ ./bin/calendar
    enter mm yyyy : 5 2020
    ===================================================
                          MAY/20
    
    SUN     MON     TUE     WED     THU     FRI     SAT
                                            01      02
    03      04      05      06      07      08      09
    10      11      12      13      14      15      16
    17      18      19      20      21      22      23
    24      25      26      27      28      29      30
    31
    ===================================================
    

    If you want to add the "-- " placeholders for empty dates, then you could just loop that many times padding with the full string, e.g.

        padding = weekDay(year,month,1);
        while (padding--)
            fputs ("--      ", stdout);
    

    使用/输出示例

    使用占位符,您将具有:

    2020年2月:

    $ ./bin/calendar
    enter mm yyyy : 2 2020
    ===================================================
                          FEB/20
    
    SUN     MON     TUE     WED     THU     FRI     SAT
    --      --      01      02      03      04      05
    06      07      08      09      10      11      12
    13      14      15      16      17      18      19
    20      21      22      23      24      25      26
    27      28      29
    ===================================================
    

    2020年3月:

    $ ./bin/calendar
    enter mm yyyy : 3 2020
    ===================================================
                          MAR/20
    
    SUN     MON     TUE     WED     THU     FRI     SAT
    01      02      03      04      05      06      07
    08      09      10      11      12      13      14
    15      16      17      18      19      20      21
    22      23      24      25      26      27      28
    29      30      31
    ===================================================
    

    2020年4月:

    $ ./bin/calendar
    enter mm yyyy : 4 2020
    ===================================================
                          APR/20
    
    SUN     MON     TUE     WED     THU     FRI     SAT
    --      --      --      01      02      03      04
    05      06      07      08      09      10      11
    12      13      14      15      16      17      18
    19      20      21      22      23      24      25
    26      27      28      29      30
    ===================================================
    

    使用我的日历进行的验证似乎正确。如果您有任何问题,请告诉我。