challenge 年間カレンダー

nを入力としてn年の年間カレンダーを返すプログラムを作ってください
少なくとも日曜日と土曜日が判別出来るようにしてください
出力は標準出力でもファイルでも構いません
デザインは各自のお好みで

出力例1:
(y-calendar 2008)=>
#=Saturday, @=Sunday
2008/1 1 2 3 4 #5 @6 7 ...
2008/2 1 #2 @3 4 5 6 7 ...
...
2008/12 1 2 3 4 5 #6 @7 ...

出力例2:
(y-calendar 2008)=>
        M T W T F S S M
2008/ 1   1 2 3 4 5 6 7 ...
2008/ 2         1 2 3 4 ...
...
2008/12 1 2 3 4 5 6 7 8 ...

出力例3:
(y-calendar 2008)は2008.htmlを出力する
2008.htmlの中身
----
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>2008 calendar</title>
<style type="text/css">
* {font-family: monospace;}
span {margin: 0px 3px;}
span.sunday {color:red;font-weight:bold;}
span.saturday {color:blue;font-weight:bold;}
dd ul li{display:inline;}
</style>
</head>
<body>
<h1>2008 calendar</h1>
<dl>
<dt>2008/1</dt>
<dd><ul>
<li><span class="weekday">1</span></li>
<li><span class="weekday">2</span></li>
<li><span class="weekday">3</span></li>
<li><span class="weekday">4</span></li>
<li><span class="saturday">5</span></li>
<li><span class="sunday">6</span></li>
...
</ul></dd>
...
</dl>
</body>
</html>
----

Posted feedbacks - C

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

(中略)

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

と、こんな感じで表示します☆
 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
#include<stdio.h>

int f(int y, int m, int d){
  if(m < 3)
    y--, m += 12;
  return y * 365 + y / 4 - y / 100 + y / 400 + (m + 1) * 306 / 10 + d - 428;
}

int main(void){
  int y , m , d , i , j;
  printf("n = ");
  scanf("%d", &y);
  for(i = 0; i < 12; i += 3){
    for(m = i + 1 ; m < i + 4; m++)
      printf("%16d /%3d   ", y, m);
    printf("\n");
    for(j = 0; j < 3; j++)
      printf(" Su Mo Tu We Th Fr Sa   ");
    printf("\n");
    for(j = 0; j < 42; j += 7, printf("\n"))
      for(m = i + 1; m < i + 4; m++, printf("   "))
        for(d = j - f(y, m, 1) % 7 + 1; d < j - f(y, m, 1) % 7 + 8; d++)
          if(d < 1 || d > f(y, m + 1, 1) - f(y, m, 1))
            printf("   ");
          else
            printf("%3d", d);
    printf("\n");
  }
  return 0;
}

C言語復習中です. 


% ./a.out 2008
Calendar 2008 : #=Sun @=Sat
Jan | 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 
Feb | 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 
Mar | @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 
Apr | 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 
May | 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 
Jun | #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 
Jul | 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 
Aug | 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 
Sep | 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 
Oct | 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 
Nov | @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 
Dec | 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 
 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
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
#include<time.h>

int main(int argc, char *argv[])
{
    int d = -1;
    char date[6], date_m[6];
    time_t date_tmp;
    struct tm *date_t;
    int year = atoi(argv[1]);
    
    time(&date_tmp);
    date_t = localtime(&date_tmp);
    
    date_t->tm_year = year - 1900;
    date_t->tm_mon = 0;
    date_t->tm_mday = 1;

    printf("Calendar %d : #=Sun @=Sat", year);

    while (year == date_t->tm_year + 1900) {
        if (d < date_t->tm_mon) {
            printf("\n");
            d = date_t->tm_mon;
            strftime(date_m, 6, "%b", date_t);
            printf("%s | ",  date_m);
        }
        
        if (date_t->tm_wday == 0) {
            printf("#");
        } else if (date_t->tm_wday == 6) {
            printf("@");
        }
        strftime(date, 6, "%d", date_t);
        printf("%s ",  date);
        date_t->tm_mday += 1;
        date_tmp = mktime(date_t);
        date_t = localtime(&date_tmp);
    }
    putchar('\n');
}

Index

Feed

Other

Link

Pathtraq

loading...