Calendar

  1. On Unix, there is a command "cal" to print out the calendar of a specific month. For example, "cal 1 2014" prints out the calendar of January 2014.
    
        January 2014
    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
    
    
  2. Develop a function cal(month_days, n) to do the following tasks:
    1. Suppose the 1st day in a month is the nth day in a week (assume Sunday is the 0th, Monday is the 1st, Wednesday is the 3rd, and so on.)
    2. Print 3*n spaces.
    3. Using a for-loop to print 1, 2, 3, ..., month_days (the possible values of month_days are 28, 29, 30, 31, depending on which month it is). If the date printed out is Saturday, output a newline (\n) character.
  3. For example, cal(31, 0) shows the following
    
      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
    
    
    while cal(29, 5) shows the following:
    
                     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
    
    
  4. Since we know how to calculate what day the first day in a month will be, you can directly import that function to calculate the day in a week D, then invoke the function cal(month_days, D).
  5. You may also output the name of the month, and also weekdays, to make it looks more informative. The output may look like this:
    
    Input a month (YYYY.MM) -- 2014.11
    November 2014
    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
    
    
Flowchart