Windowsでかれんだー

Linuxのcalに感動して発作的に作った. 暇つぶし. 年数を入力してカレンダーを表示する以上の機能は無い.

ソースコード

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <boost/format.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>

bool is_leap_year( int const year )
{
	if( year % 4 == 0 && ( year % 100 != 0 || \
		year % 100 == 0 && year % 400 == 0 ) )
		return true;
	return false;
}


//任意の月をカレンダーのフォーマットで
//作成
std::array< std::string, 8 > calc_month( int const day, \
	int const first_day, int const month )
{
	using namespace std;

	//2 + 6
	array< string, 8 > output;

	output[ 0 ] += ( boost::format( "        %2d月       " ) % month ).str();
	output[ 1 ] += boost::format( "日 月 火 水 木 金 土" ).str();

	int col = 2;

	//first_day日目にまで空白でうめる
	for( int i = 0; i < first_day % 7; ++i )
	{
		output[ 2 ] += "   ";
	}

	for( int i = 1; i <= day; ++i )
	{
		output[ col ] += ( boost::format( "%2d" ) % i ).str();

		if( ( first_day + i ) % 7 == 0 )
		{
			++col;
		}
		else
		{
			output[ col ] += " ";
		}
	}

	//スペースで埋める
	output[ 6 ].resize( output[ 0 ].size() + 1, ' ' );
	output[ 7 ].resize( output[ 0 ].size() + 1, ' ' );

	return output;
}

void show_year_cal( int const year )
{
	using namespace std;

	std::array< std::array< std::string, 8 >, 12 > months;

	//各々の月の日数
	int const days[] = {
		31, 28, 31, 30, \
		31, 30, 31, 31, \
		30, 31, 30, 31 };

	boost::gregorian::date first_of_year( year, 1, 1 );
	auto const first_week_of_day = first_of_year.day_of_week().as_number();
	months[ 0 ] = calc_month( days[ 0 ], first_week_of_day, 1 );

	auto const leap = ( is_leap_year( year ) ? 1 : 0 );

	months[ 1 ] = calc_month( days[ 1 ] + leap, first_week_of_day + days[ 0 ], 2 );

	auto days_count = first_week_of_day + days[ 0 ] + days[ 1 ] + leap;

	for( int i = 2; i < months.size(); ++i )
	{
		months[ i ] = calc_month( days[ i ], days_count, i + 1 );
		days_count += days[ i ];
	}

	cout << boost::format( \
		"                             %4d" ) % year << endl;

	for( int i = 0; i < 4; ++i )
	{
		for( int j = 0; j < months[ i ].size(); ++j )
		{
			for( int k = 0; k < 3; ++k )
			{
				cout << months[ i * 3 + k ][ j ];
				cout << "  ";
			}
			cout << endl;
		}
	}
}

int main()
{
	//0-6 : 日-土

	int year = 2013;

	if( ( std::cin >> year ).fail() )
		return 0;
	if( year < 1 || year > 2200 )
		return 0;

	show_year_cal( year );
	return 0;
}

たぶん入力はちょっと
甘いところある&少し月表示がずれてる.