#define _CRT_SECURE_NO_WARNINGS
//#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#define WEEK "日 月 火 水 木 金 土"

char disp[8][80];

void strfmt(void* dest, char* fmt, int i)
{
	char		buf[8];

	sprintf(buf, fmt, i);
	memcpy(dest, buf, strlen(buf));
}

void cal(int year, int month)
{
	struct tm	tm;
	struct tm*	ptm;
	time_t		timer;
	int		line;
	int		i;

	for (i = 0; i < 8; i++) {
		memset(disp[i], ' ', 79);
		disp[i][79] = '\0';
	}
	strfmt(disp[0], "%d", year);
	strfmt(disp[0] + 8, "%d月", month);
	memcpy(disp[1], WEEK, strlen(WEEK));

	memset(&tm, 0, sizeof tm);
	tm.tm_year	= year - 1900;
	tm.tm_mon	= month - 1;
	tm.tm_mday	= 1;
	timer = mktime(&tm);
	line = 2;
	for (i = 1; i <= 31; i++) {
		ptm = localtime(&timer);
		if (ptm->tm_mon != month - 1) {
			break;
		}
		if (ptm->tm_wday == 0 && 1 < i) {
			line++;
		}
		strfmt(disp[line] + ptm->tm_wday * 3, "%2d", i);
		timer += 24 * 60 * 60;
	}

	for (i = 0; i < 8; i++) {
		printf("%s\n", disp[i]);
	}
}

int main()
{
	cal(2012, 6);
	return 0;
}
