#include
#include
int get_date(int, int, int);
int calc_year(int);
int main(){
int m, day, year, our_day;
cout<<"\nEnter any date after 1900 as three sets of numbers.\n";
cout<<"(month, day, year). Example: 7 29 1971 for July 29, 1971\n";
cout<<"Entering invalid dates will exit the program.\n";
cout<<"Enter date: ";
cin>>m>>day>>year;
while((m != 13)&(day !=32)&(year != 0000)){
//Invalid dates exits the loop
our_day = get_date(m, day, year);
//Passes the three values to get_date function
//Turns a numberical month into a nice
//named month when printed
switch(m){
case 1: cout<<"January "; break;
case 2: cout<<"February "; break;
case 3: cout<<"March "; break;
case 4: cout<<"April "; break;
case 5: cout<<"May "; break;
case 6: cout<<"June "; break;
case 7: cout<<"July "; break;
case 8: cout<<"August "; break;
case 9: cout<<"September "; break;
case 10: cout<<"October "; break;
case 11: cout<<"November "; break;
case 12: cout<<"December "; break;
}
cout<<" "<
switch(our_day){
case 0: cout<<"Monday\n"; break;
case 1: cout<<"Tuesday\n"; break;
case 2: cout<<"Wednesday\n"; break;
case 3: cout<<"Thursday\n"; break;
case 4: cout<<"Friday\n"; break;
case 5: cout<<"Saturday\n"; break;
case 6: cout<<"Sunday\n"; break;
}
cout<<"\n\nEnter date: ";
cin>>m>>day>>year;
}
cout<<"\nYou entered an invalid date! End Program!"<
return 0;
}
//-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
//GET_DATE function. This function takes in the month, day and year
//and using the month and day, fugures out the numerical value of the
//day, i.e., January 1 is day 1, December 31 is day 365.
//The function uses the year to adjust for leapyears, adding a day
//on leapyears if the the date is after February.
int get_date(int the_m, int the_day, int the_year){
//initial values for months of the year
int jan = 0, febr = 31, mar = 59, apr = 90,
may = 120, june = 151, july = 181, aug = 212,
sept = 243, octo = 273, nov = 304, dec = 334;
int the_month, the_date;
if(3 > the_m){
if(2 == the_m){
the_date = febr + the_day;
}
else{
if(1 == the_m){
the_date = jan + the_day;
}
}
}
else{
switch(the_m){
case 3: the_month = mar; break;
case 4: the_month = apr; break;
case 5: the_month = may; break;
case 6: the_month = june; break;
case 7: the_month = july; break;
case 8: the_month = aug; break;
case 9: the_month = sept; break;
case 10: the_month = octo; break;
case 11: the_month = nov; break;
case 12: the_month = dec; break;
}
if((0 == the_year%4) &! (0 == the_year%100)){
the_date = the_day + (the_month + 1);
}
else{
if(0 == the_year%400){
the_date = the_day + (the_month + 1);
}
else{
the_date = the_day + the_month;
}
}
}
int first_day;
first_day = calc_year(the_year);
int this_day;
this_day = the_date + first_day;
while(this_day >= 7){
this_day = this_day - 7;
}
this_day = this_day - 1;
if(this_day == -1){
this_day = 6;
}//Make the day before Monday (0 - 1) be Sunday
return this_day;
}
//-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
//CALC_YEAR figures out the first day of each year
//and reports it back
int calc_year(int the_year){
int count_day = 0; //start on Monday
//1900 started on a Monday, so don't enter the loop
for(int i = 1901; i <= the_year; i++){
count_day++;
if(count_day >= 7){
count_day = count_day - 7;
}//reset at the end of each week
if((0 == ((i-1)%4)) &! (0 == ((i-1)%100))){
count_day = count_day + 1; //adjust for leapyear
}
else{
if(0 == ((i-1)%400)){
count_day = count_day + 1;}
}
//Every 4 years is a leapyear, i.e., years divisible by 4.
//Years divisible by 100 are not leapyears, this corrects
//For the fact that a leapday is not actually a full day.
//Years divisible by 400 are leapyears.
//Example: 1900, not a leapyear. 2000 a leapyear
}//close for loop
if(count_day == 7){
count_day = 0;} //Make the day after Sunday (6 + 1) become Monday
return count_day;
}
|