Program #4 - Computer Science

advertisement
Sp13
Introduction to Computer Science
Program #4
Due: midnight 3/22/13
Objective:
1. Practice top-down design to solve a problem
2. Demonstrate the use of the functions and parameters to support the top-down design process.
Premise:
You are asked to write a program that will print out a calendar for a particular month given the year.
Problem:
1. Prompt the user for a year, which should be an integer between 1900 and 2999. (if the integer is out of
range, re-prompt the user for a legal year and re-read; repeat until you get a legal year.)
2. Similarly, prompt the user for a month, which should be an integer between 1 and 12 (re-prompt and reread as necessary until you get a legal month.)
3. Print the calendar for the given month. Include the name of the month, the year, and the days of the week
(see sample output below.)
4. Find out whether or not the user would like to display another calendar. The answer should be read as a
single character; anything but a ‘y’ or ‘Y’ means to stop processing. The user should have to press enter
(or return) after the character to have it read.
Implementation:
Basic algorithm: Given a year and a month, figure out the day of the week that the calendar for that month
begins, and figure out how many days the month has. Once these facts are known, it should be fairly easy to
print the calendar for that month. Your program should include at least the following functions:
 A function that returns an integer value in a specified range. (You use it to get valid months and
years.) The legal range should be supplied as arguments to the function. For example, if the function
is called valid_int, then valid_int(1900,2999) returns only after an integer between 1900 and 2999
inclusive is entered (this implies user input inside the function.)
 A function that takes a month and year as arguments and returns the day of the week on which the
first of that month falls. You can use Zeller’s Congruence for this calculation. The formula is :
d = ([2.6M – 0.2] + D + Y + [Y/4] + [C/4] – 2C) modulo 7
where
d is given as 0 = Sunday, 1 = Monday, …, 6 = Saturday;
D is the day of the month;
M is the month number, March = 1, … December = 10, and January and
February are months 11 and 12 of the previous year;
C is the century;
Y is the year within the century;
[x] is the largest integer not greater than x.
Example:
25 February 1960 has D = 25, M = 12, C = 19, Y = 60
 A function that returns the number of days in a month (given the month and year).
 A predicate function that takes a year number and returns true (1) if the year is a leap year, else it
returns false (0). With the exception of century years, leap years are those years divisible by four.
Century years (1900, 2000, …) are leap years if and only if they are divisible by 400.
 A function that (given a valid month, year, and the day of the week that the month begins on) prints
the calendar for the given month.
Grading:
You will be graded on (1) the correctness and format of your output, (2) the clarity, neatness and
documentation of your code, and (3) the proper use of functions as outlined above. Do not use any portion of
C++ that we have not yet discussed in class.
Sample:
Calendar program begins.
Please enter a year (1900-2999): 1995
Please enter a month (1-12): 3
March 1995
S M T W T
1 2
5 6 7 8 9
12 13 14 15 16
19 20 21 22 23
26 27 28 29 30
F
3
10
17
24
31
S
4
11
18
25
Do you want to enter another date? Y
Please enter a year (1900-2999): 1895
*** You must enter an integer in the range 1900 through 2999: 1994
Please enter a month (1-12): 14
*** You must enter an integer in the range 1 through 12: 12
December 1994
S M T W T
1
4 5 6 7 8
11 12 13 14 15
18 19 20 21 22
25 26 27 28 29
F
2
9
16
23
30
S
3
10
17
24
31
Do you want to enter another date? n
Goodbye!
Submission:
The documentation standards as mentioned in the handout are to be followed. You should submit your
program as a C++ file named prog4.cpp to the subversion server by 11:59:59 pm on the due date. Use the
repository to submit the assignment. The repository link is
https://dev.cs.uakron.edu/svn/cs209sp13/810/students/UANETID
1. Create a folder ‘Project4’ in ’Projects’
2. Create file prog4.cpp.
3. Upload the file to the folder ‘Project4’.
4. Commit your changes.
Warnings:
1. Zeller’s Congruence (before doing the mod 7) can result in a negative value. You must determine a
reasonable way to convert this to the right positive number before you apply the mod operation.
2. You will need to use a lot of test cases to be sure that your program works correctly for each possible
case. This does not mean that you need to check all of the legal years; it does mean that you need to be
sure that you can handle one of each case.
3. When you apply the rule ‘January and February are months 11 and 12 of the previous year’, you need to
decrement the year. What if the year is 00? What must happen to the century?
4. Checking a program involves knowing the right answer for your test cases. I gave you two test cases to
get you started; you will need more. A good source is something called a ‘perpetual calendar’. Look for
one in the library or on the Web.
5. If you come across a particularly good (read “challenging”) test case, let me know and I’ll inform the rest
of the class.
Download