/** * Assignment #3, Program 2 * Program to calculate day number in a year * Author: Mark Allen Weiss * SSN: 000-00-0000 * Course: COP-2210 Section 8 * Date: October 7, 1998 * * CERTIFICATION OF SOLE AUTHORSHIP * I certify that this work is solely my own and * that none if it is the work of any other person. * I further certify that I have not provided any * assistance to other students enrolled in COP-2210 that * would render their CERTIFICATION OF SOLE AUTHORSHIP * invalid. I understand that violations of this pledge * may result in severe sanctions. * * * * ----------------------------------- * (Mark Allen Weiss) <--- Put your name here, and sign above */ #include #include "apstring.h" // Program constants const int JANUARY = 1; const int FEBRUARY = 2; const int MARCH = 3; const int APRIL = 4; const int MAY = 5; const int JUNE = 6; const int JULY = 7; const int AUGUST = 8; const int SEPTEMBER = 9; const int OCTOBER = 10; const int NOVEMBER = 11; const int DECEMBER = 12; // Function declarations void display_instructions( ); int get_input( apstring message ); int compute_day_number( int month, int day, int year ); void print_answer( int day_number ); int days_to_add( int month, int which_month, int days_in_month ); bool is_leap_year( int year ); int main( ) { // Inputs int month; int day; int year; // Outputs int day_number; // Read values display_instructions( ); month = get_input( "month" ); day = get_input( "day" ); year = get_input( "year" ); // Compute answers day_number = compute_day_number( month, day, year ); // Print the answers print_answer( day_number ); return 0; } /** * Print instructions. */ void display_instructions( ) { cout << "This program will compute the day number (1 to 366)" << endl; cout << "in a year for a date, given three parameters:" << endl; cout << "\t1. The month (1-12)" << endl; cout << "\t2. The day of the month (1-31)" << endl; cout << "\t3. The year (1600-)" << endl; cout << "Dates are not checked for validity." << endl; } /** * Print answers. */ void print_answer( int day_number ) { cout << endl; cout << "The entered day is day number " << day_number << endl; } /** * Read an input; message is used to give a prompt. */ int get_input( apstring message ) { int value_read; cout << "Please enter the " << message << ": "; cin >> value_read; return value_read; } /** * Return the number of days to add to the running total. * If month is past which_month, return days_in_month; otherwise * return 0. */ int days_to_add( int month, int which_month, int days_in_month ) { if( month > which_month ) return days_in_month; else return 0; } /** * Compute day number. */ int compute_day_number( int month, int day, int year ) { int total_days = day; total_days += days_to_add( month, JANUARY, 31 ); if( is_leap_year( year ) ) total_days += days_to_add( month, FEBRUARY, 29 ); else total_days += days_to_add( month, FEBRUARY, 28 ); total_days += days_to_add( month, MARCH, 31 ); total_days += days_to_add( month, APRIL, 30 ); total_days += days_to_add( month, MAY, 31 ); total_days += days_to_add( month, JUNE, 30 ); total_days += days_to_add( month, JULY, 31 ); total_days += days_to_add( month, AUGUST, 31 ); total_days += days_to_add( month, SEPTEMBER, 30 ); total_days += days_to_add( month, OCTOBER, 31 ); total_days += days_to_add( month, NOVEMBER, 30 ); total_days += days_to_add( month, DECEMBER, 31 ); return total_days; } /** * Returns true if and only if a leap year. * Leap year if: divisible by 4, but not by 100, unless divisible by 400. */ bool is_leap_year( int year ) { return ( year % 400 == 0 ) || ( year % 4 == 0 && year % 100 != 0 ); }