Lab2: A Simple Calendar Program


Due Date: Sun, 01 Feb 2009 by 23:59


Background

This assignment requires you to mimic the cal (calendar) UNIX command. With no arguments, cal prints the current month. With the year as a command-line argument, cal prints a nicely formatted (3 months per line) calenadar for that year. Unlike cal, you won't have to do the fancy multi-column formatting (although if you have lots of free time, or are incredibly anal, be our guest). 3-column formatting, as cal does will earn you a 10% bonus. WARNING - potential black hole/time sink! Make sure you have a working program that is correct for the baseline specification before attempting the extra credit!


Specification

Your program accepts two kinds of command-line arguments

    my-cal year

prints the entire calendar for that year, with the individual months formatted like cal, but with only one month per line.

    my-cal year start-month num-months

prints the calendar starting with the specified month in the specified year, for as many months as indicated, one month per line. Thus, if I entered my-cal 2008 3 2, it would print the months of March and April for 2008.

The output should be neat and readable and, with the caveat that you don't need multiple months in columnar format, should look a lot like cal. Should the user invoke the program with an inappropriate number of command-line arguments, you should print a usage help similar to

    Usage: my-cal year  or my-cal year start-month num-months

You may assume integer inputs to the command-line (of course, we know they're stored as "strings").


Implementation Hints

I encourage you to run cal to see what its output looks like. Yours should provide similar spacing (think format specifiers). Also, the following code can be used to determine on what day of the week (0 is Sunday, 1 is Monday...) a given year starts (I figured I'd save you the Google...):

    cen = (3 - (year / 100 % 4)) * 2;
    yr = year % 100;
    d = (yr / 12 + yr % 12 + yr % 12 / 4 + cen) % 7;
    if (is_leap(year))  /* you have to write this function - sorry! */
	d += 7;
    else
	d += 1;
    d %= 7;

You should not need to use arrays for this assignment. Just think about what you need to know for each month once you know the day of the week that the year begins...


Handing in your Solution

Same like last time. Early is Saturday midnight, (+10% bonus), Sunday midnight is on time, Monday midnight is the end of the late period (-10% penalty).

You will need to hand in a text file that answers the 4 questions like the last lab.

You have to hand your lab to
/afs/qatar.cmu.edu/usr9/yeng/15123/Lab-handin/andrew_id/Lab2/
I won't accept the program by email.