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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <ctime>
static const std::string month_name[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } ;
int get_month() ; // Jan = 0, ... Dec = 11
int get_year() ; // 1971 - 2999
int offset( int year, int month ) ; // Sun == 0
int days_in_month( int year, int month ) ;
void display_month( int ndays, int offset ) ;
int main()
{
std::cout << "program to display calendar for a month\n\n" ;
const int month = get_month() ;
const int year = get_year() ;
std::cout << "\n\n\n " << month_name[month] << ' ' << year << "\n\n" ;
display_month( days_in_month(year,month), offset(year,month) ) ;
}
int get_month() // Jan = 0, ... Dec = 11
{
std::cout << "enter month [ " ;
for( const auto& m : month_name ) std::cout << m << ' ' ;
std::cout << "]: " ;
std::string month ;
std::cin >> month ;
if( month.size() > 2 )
{
month = month.substr(0,3) ; // we are interested in the first three characters
month[0] = std::toupper( month[0] ) ; // make the first character upper case
for( int i : { 1, 2 } ) month[i] = std::tolower( month[i] ) ; // and the rest lower case
for( int i = 0 ; i < 12 ; ++i ) if( month_name[i] == month ) return i ; // found the month
}
std::cout << "invalid month. try again\n" ;
return get_month() ; // try again
}
int get_year() // 1971 - 2999
{
int year ;
std::cout << "enter year [1971-2999]: " ; // unix time, 64-bit
if( std::cin >> year && year > 1971 && year < 3'000 ) return year ;
std::cout << "invalid year. try again\n" ;
// clear a possible failed state, then extract and discard the offending input
std::cin.clear() ; // http://en.cppreference.com/w/cpp/io/basic_ios/clear
std::cin.ignore( 1000, '\n' ) ; // http://en.cppreference.com/w/cpp/io/basic_istream/ignore
return get_year() ; // try again
}
int offset( int year, int month ) // Sun == 0
{
// http://en.cppreference.com/w/cpp/chrono/c/tm
std::tm calendar_time {} ; // initialise to all zeroes
calendar_time.tm_mday = 1 ;
calendar_time.tm_mon = month ;
calendar_time.tm_year = year - 1900 ;
// http://en.cppreference.com/w/cpp/chrono/c/mktime
const auto t = std::mktime( std::addressof(calendar_time) ) ;
// http://en.cppreference.com/w/cpp/chrono/c/localtime
return std::localtime( std::addressof(t) )->tm_wday ;
}
int days_in_month( int year, int month )
{
std::tm calendar_time {} ; // initialise to all zeroes
calendar_time.tm_mday = 28 ; // smallest number of days in any month
calendar_time.tm_mon = month ;
calendar_time.tm_year = year - 1900 ;
std::time_t t ;
int last_day ;
do // bump tm_mday till month rolls over
{
last_day = calendar_time.tm_mday ;
++calendar_time.tm_mday ;
t = std::mktime( std::addressof(calendar_time) ) ;
}
while( std::localtime( std::addressof(t) )->tm_mon == month ) ;
return last_day ; // return the tm_mday just before first of next month
}
void display_month( int ndays, int offset )
{
std::cout << " Sun Mon Tue Wed Thu Fri Sat\n"
"------------------------------------\n" ;
offset %= 7 ; // sanity check (defensive)
if (offset == 6) offset = 0;
for( int i = 0 ; i < offset; ++i ) std::cout << std::setw(5) << ' ' ;
for( int day = 1; day <= ndays; ++day )
{
std::cout << std::setw(5) << day;
if ( ++offset%7 == 0 ) std::cout << '\n' ;
}
if( offset%7 ) std::cout << '\n' ;
std::cout << "------------------------------------\n" ;
}
|