Advertisement
Guest User

doomsday.c

a guest
Mar 27th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. int dayOfWeek (int doomsday, int leapYear, int month, int day) {
  2. int dayOfWeek=0;
  3. int anchor=0;
  4. int moddifference=0;
  5.  
  6. //Determine the anchor day based on the month
  7. if ((month==1 && leapYear==FALSE)||month==10){
  8. anchor = doomsday - JANOCTDOOM;
  9. } else if ((month==1 && leapYear==TRUE)|| month==7) {
  10. anchor = doomsday - JANJULDOOML;
  11. } else if ((month==2 && leapYear==TRUE) || month==8){
  12. anchor = doomsday - AUGFEBLDOOM;
  13. } else if (month==3||month==11||(month==2 && leapYear==FALSE)){
  14. anchor = doomsday - MARNOVFEBDOOM;
  15. } else if (month==9||month==12){
  16. anchor = doomsday - SEPDECDOOM;
  17. } else if (month==4 || month==6){
  18. anchor = doomsday - month;
  19. } else if (month==5){
  20. anchor = doomsday - MAYDOOM;
  21. }
  22. // "wrap" the anchor around if it goes below 0 so that it is still valid
  23. if (anchor<THURSDAY){
  24. anchor=anchor+DAYS_PER_WEEK;
  25. }
  26. 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)
  27. moddifference=DAYS_PER_WEEK-day; //inverts the difference
  28. dayOfWeek=anchor-moddifference; // uses the inverted difference to find the dayofweek by subracting it from anchor
  29. if (dayOfWeek<THURSDAY){ // Corrects the dayOfWeek if it is a number above or below the weeks values and "wraps it around"
  30. dayOfWeek+=DAYS_PER_WEEK;
  31. } else if (dayOfWeek>WEDNESDAY){
  32. dayOfWeek-=DAYS_PER_WEEK;
  33. }
  34. return (dayOfWeek); // returns dayofweek to main function for verification
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement