Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int dayOfWeek (int doomsday, int leapYear, int month, int day) {
- int dayOfWeek=0;
- int anchor=0;
- int moddifference=0;
- //Determine the anchor day based on the month
- if ((month==1 && leapYear==FALSE)||month==10){
- anchor = doomsday - JANOCTDOOM;
- } else if ((month==1 && leapYear==TRUE)|| month==7) {
- anchor = doomsday - JANJULDOOML;
- } else if ((month==2 && leapYear==TRUE) || month==8){
- anchor = doomsday - AUGFEBLDOOM;
- } else if (month==3||month==11||(month==2 && leapYear==FALSE)){
- anchor = doomsday - MARNOVFEBDOOM;
- } else if (month==9||month==12){
- anchor = doomsday - SEPDECDOOM;
- } else if (month==4 || month==6){
- anchor = doomsday - month;
- } else if (month==5){
- anchor = doomsday - MAYDOOM;
- }
- // "wrap" the anchor around if it goes below 0 so that it is still valid
- if (anchor<THURSDAY){
- anchor=anchor+DAYS_PER_WEEK;
- }
- day=day%DAYS_PER_WEEK; //finds the day difference, that is how far the day is from the anchor day (ie tuesday is 1 from monday)
- moddifference=DAYS_PER_WEEK-day; //inverts the difference
- dayOfWeek=anchor-moddifference; // uses the inverted difference to find the dayofweek by subracting it from anchor
- if (dayOfWeek<THURSDAY){ // Corrects the dayOfWeek if it is a number above or below the weeks values and "wraps it around"
- dayOfWeek+=DAYS_PER_WEEK;
- } else if (dayOfWeek>WEDNESDAY){
- dayOfWeek-=DAYS_PER_WEEK;
- }
- return (dayOfWeek); // returns dayofweek to main function for verification
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement