2016-10-18 111 views
-1

我試圖打印出特定年份和月份的日曆,但每個月都會收到相同的日曆。我試圖添加一個聲明,以查看年份是否爲閏年以增加天數,但它沒有區別。我是新來的c。請幫助任何建議。提前致謝。特定年份和月份的打印日曆

void print_month_calendar(int year, int month) 
{ 
    int day; 
    int daycode = ddaycode(year); 
    int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 

char *months[]= 
      { 
        " ", 
        "\n\n\nJanuary", 
        "\n\n\nFebruary", 
        "\n\n\nMarch", 
        "\n\n\nApril", 
        "\n\n\nMay", 
        "\n\n\nJune", 
        "\n\n\nJuly", 
        "\n\n\nAugust", 
        "\n\n\nSeptember", 
        "\n\n\nOctober", 
        "\n\n\nNovember", 
        "\n\n\nDecember" 
      }; 

    printf("%s", months[month]); 
    printf("\n\nSun Mon Tue Wed Thu Fri Sat\n"); 

    if((year%4==0 && year%100 !=0) || year%400==0) 
     days_in_month[2] = 29; 

    for (day = 1; day <= 1 + daycode * 5; day++) 
    { 
     printf(" "); 
    } 

    //Print all the dates for the month 
    for (day = 1; day <= days_in_month[month]; day++) 
    { 
     printf("%2d", day); 

     if ((day + daycode) % 7 > 0) 
      printf(" "); 
     else 
      printf("\n "); 
    } 
} 

int ddaycode(int year) 
{ 
    int daycode; 
    int d1, d2, d3; 

    d1 = (year - 1.)/ 4.0; 
    d2 = (year - 1.)/ 100.; 
    d3 = (year - 1.)/ 400.; 
    daycode = (year + d1 - d2 + d3) %7; 
    return daycode; 
} 
+2

你daycode計算沒有考慮月份考慮在內。 – Cimbali

回答

1

您必須知道本月的第一個工作日。您可以使用localtime轉至指定日期,使用mktime獲取struct tm包含工作日的數據。另外,您可以使用strftime來獲取月份名稱。例如:

void print_month_calendar(int year, int month) 
{ 
    time_t rawtime; 
    struct tm * timeinfo; 
    time(&rawtime); 
    timeinfo = localtime(&rawtime); 
    timeinfo->tm_year = year - 1900; 
    timeinfo->tm_mon = month - 1; 
    timeinfo->tm_mday = 1; 
    mktime(timeinfo); 

    //sunday is 1 ... saturday is 7 
    int weekday = 1 + timeinfo->tm_wday; 

    int days_in_month[] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 }; 
    if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) 
     days_in_month[2]++; 

    char monthname[30]; 
    strftime(monthname, sizeof(monthname), "%B", timeinfo); 

    printf(" %s %d\n", monthname, year); 
    printf(" Su Mo Tu We Th Fr Sa\n"); 

    int day = 1; 
    for (int i = 1; i <= 40; i++) 
    { 
     if (i < weekday) 
     { 
      printf(" "); 
     } 
     else 
     { 
      printf("%3d ", day); 
      if (day == days_in_month[month]) 
       break; 
      day++; 
      if ((i % 7) == 0) 
       printf("\n"); 
     } 
    } 

    printf("\n"); 
} 


int main() 
{ 
    print_month_calendar(2016, 10); 
    return 0; 
} 

ideone example

October 2016 
Su Mo Tu We Th Fr Sa 
          1 
    2 3 4 5 6 7 8 
    9 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28 29 
30 31 
+0

有趣的是:在我的Mac上,我得到了一個看起來像這樣的日曆:'October' - 'Su Mo Tu We Th Fr Sa' - '7' - (這個7在'Sa'下正確調整) - '8 9 10 11 12 13 14' - '15 16 17 18 19 20 21' - '22 23 24 25 26 27 28' - '29 30' - 問題是打印的第一個日期是第七天,只有30天的時間才印出來,而不是31天(我可以同情想要消滅萬聖節,但是......) –

+0

@JonathanLeffler我錯了,現在應該沒問題! –

+0

10月1日至6日發生了什麼?今年(2016年),10月1日是星期六,31日是星期一。 –

相關問題