[ create a new paste ] login | about

Link: http://codepad.org/g9j514Dw    [ raw code | output | fork ]

C, pasted on Nov 25:
#include <stdio.h>

/* 関数プロトタイプ */
int dayofweek(int year, int month);
int daysinmonth(int year, int month);
int daysinyear(int year);
void showcal(int dow, int days);

int main()
{
  int year, month; /* 年と月 */
  int dow; /* その月の1日の曜日 */
  int dim; /* その月の日数 */

  printf ("Year?:1873 \n");
  //scanf ("%d", &year);
  year = 1873;
  printf ("Month?:5 \n");
  //scanf ("%d", &month);
  month = 5;
  
  dow = dayofweek(year, month);
  dim = daysinmonth(year, month);

  showcal(dow, dim); /* カレンダー表示 */

  return 0;
}

/* カレンダーを表示する */
void showcal (int dow, int days)
{
  int i, j, d;

  printf ("Su Mo Tu We Th Fr Sa\n");

  d = 1;

  /* 最初の週の表示*/
  for (i = 0; i < dow; i++) {
    printf ("   ");
  }
  for (; i < 7; i++) {
      printf (" %d ", d);
      d++;
  }
  printf("\n");

  /* 二週目以降の表示 */
  for (j = 0; d <= days; j++) {
    for (i = 0; i < 7 && d <= days; i++) {
      if (d < 10)
printf (" %d ", d);
      else
printf ("%d ", d);
      d++;
    }
    printf("\n");
  }
}

/* 指定された月の日数をかえす */
int daysinmonth( int year, int month)
{
  int dim; /* 月の日数 */

  dim = 31; /* 普通 31日 */

  if (month == 4 || month == 6 || month == 9 || month == 11)
    dim = 30; /* 4,6,9,11月は30日 */

  if (month == 2) {
    if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) 
      dim = 29; /* うるう年 */
    else
      dim = 28; /* 普通の年 */
  }

  return dim;
}

/* 指定された年の日数をかえす */
int daysinyear( int year)
{
  int diy; /* 年の日数 */

  if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) 
    diy = 366; /* うるう年 */
  else
    diy = 365; /* 普通の年 */

  return diy;
}


/* 指定された月の初日の曜日を返す */
/* 0:日曜日, 1:月曜日, ..., 6:土曜日 */
int dayofweek (int year, int month) {
	int dow;
	int days; /* 2000年1月1日と指定された月の初日の間の経過日数 */
	int y, m;
  	
	//specialize
	if(year == 2000 && month == 1)
	{
		return 6;
	}
		
	//random case
	if(month !=1)
	{
		return (dayofweek(year, month-1) + daysinmonth(year, month-1) % 7) % 7;
	}
	else
	{
		if(year < 2000)
		{
			return ((dayofweek(year+1, 1) - daysinyear(year) % 7)+7) % 7;
		}
		else
		{
			return ((dayofweek(year-1, 1) + daysinyear(year-1) % 7)+7) % 7;
		}
	}
	return dow;
}


Output:
1
2
3
4
5
6
7
8
Year?:1873 
Month?:5 
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 


Create a new paste based on this one


Comments: