#include <iostream>
usingnamespace std;
constint JANUARY = 1;
constint FEBRUARY = 2;
constint MARCH = 3;
constint APRIL = 4;
constint MAY = 5;
constint JUNE = 6;
constint JULY = 7;
constint AUGUST = 8;
constint SEPTEMBER = 9;
constint OCTOBER = 10;
constint NOVEMBER = 11;
constint DECEMBER = 12;
// Returns true if the given year is a leap year
bool is_leap_year(int year);
// Returns a value computed from the century of the year
int get_century_value(int year);
// Returns a value computed based on the years since the beginning of the century.
int get_year_value(int year);
// Returns a value (from a table) for the given month
int get_month_value(int month, int year);
int main()
{
int month =0;
int day = 0;
int year = 0;
int day_of_week = 0;
int days_in_month = 0;
bool dates_invalid=true;
do
{
cout << endl;
cout << " Please enter a month: ";
cin >> month;
cout << endl;
cout << " Please enter a day: ";
cin >> day;
cout << endl;
cout << " Please enter a year: ";
cin >> year;
//check for leap year
if(month == 9 || month == 4 || month == 6 || month == 11)
{
days_in_month = 30;
}
elseif(month == 2)
{
if( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
days_in_month = 29;
}
else
{
days_in_month = 28;
}
}
else
{
days_in_month = 31;
}
if( month < 1 || month > 12 || day <= 0 || day > days_in_month)
{
cout << endl;
cout << "You have entered an invalid date. Please try again." << endl;
dates_invalid=false;
cout << endl;
}
else
dates_invalid = true;
}
while (dates_invalid=false);
// Compute the day of the week
day_of_week = day + get_month_value(month, year) + get_year_value(year) + get_century_value(year);
day_of_week = day_of_week % 7;
cout << "\n The date " << month << "/" << day << "/" << year
<< " is a ";
if (day_of_week == 0)
{
cout << "Sunday.";
}
elseif (day_of_week == 1)
{
cout << "Monday.";
}
elseif (day_of_week == 2)
{
cout << "Tuesday.";
}
elseif (day_of_week == 3)
{
cout << "Wednesday.";
}
elseif (day_of_week == 4)
{
cout << "Thursday.";
}
elseif (day_of_week == 5)
{
cout << "Friday.";
}
elseif (day_of_week == 6)
{
cout << "Saturday.";
}
cout << endl;
return 0;
}
// Prompts the user to input a month, day and year
void get_input(int month, int day, int year)
{
cout << endl;
cout << " Please enter a month: ";
cin >> month;
cout << endl;
cout << " Please enter a day: ";
cin >> day;
cout << endl;
cout << " Please enter a year: ";
cout << endl;
cin >> year;
}
// Returns true if the given year is a leap year
bool is_leap_year(int year)
{
return (((year % 400) == 0) ||(((year % 4) == 0)
&& ((year % 100) != 0)));
}
// Returns a value computed from the century of the year
int get_century_value(int year)
{
int century;
int remainder;
century = year/100;
remainder = (century % 4);
return ((3 - remainder) * 2);
}
// Returns a value computed based on the years since
// the beginning of the century.
int get_year_value(int year)
{
int sinceCentury;
sinceCentury = year % 100;
return (sinceCentury + (sinceCentury/4));
}
// Returns a value (from a table) for the given month
int get_month_value(int month, int year)
{
int result;
if (month == JANUARY)
{
if (is_leap_year(year))
{
result = 6;
}
else
{
result = 0;
}
}
if (month == FEBRUARY)
{
if (is_leap_year(year))
{
result = 2;
}
else
{
result = 3;
}
}
elseif (month == MARCH)
{
result = 3;
}
elseif (month == APRIL)
{
result = 6;
}
elseif (month == MAY)
{
result = 1;
}
elseif (month == JUNE)
{
result = 4;
}
elseif (month == JULY)
{
result = 6;
}
elseif (month == AUGUST)
{
result = 2;
}
elseif (month == SEPTEMBER)
{
result = 5;
}
elseif (month == OCTOBER)
{
result = 0;
}
elseif (month == NOVEMBER)
{
result = 3;
}
elseif (month == DECEMBER)
{
result = 5;
}
return result;
}