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
|
#include <iostream>
#include <iomanip>
enum week_day { SUN, MON, TUE, WED, THU, FRI, SAT }; // 0,1,2,3,4,5,6
void display_month( int num_days, week_day first_day )
{
#ifndef NDEBUG
std::cout << "\nnum_days == " << num_days << " first_day == " << first_day << '\n' ;
#endif // NDEBUG
const int width = 5 ;
std::cout << "\nSun Mon Tue Wed Thu Fri Sat\n";
// skip (print spaces) till we get to the first day in the month
for( int i = 0 ; i < first_day ; ++i ) std::cout << std::setw(width) << ' ' ; ;
for( int day = 1; day <= num_days; ++day ) // from the first day onwards
{
std::cout << std::setw(2) << day << std::setw(width-2) << ' ' ;
if( (day+first_day) % 7 == 0 ) std::cout << '\n' ; // if saturday, print a new line
}
std::cout << "\n\n" ;
}
int main()
{
display_month( 31, THU ) ;
display_month( 30, SUN ) ;
display_month( 28, SAT ) ;
}
|