Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* Structs */
  4. /* ------- */
  5.  
  6. struct date
  7. {
  8. int month;
  9. int day;
  10. int year;
  11. };
  12.  
  13. /* This function takes a date and returns a number representing the date entered */
  14. /* ----------------------------------------------------------------------------- */
  15.  
  16. long int calc_date_number(struct date a_date)
  17. {
  18. long int f;
  19. long int g;
  20. long int N;
  21.  
  22. if (a_date.month <= 2)
  23. {
  24. f = a_date.year - 1;
  25. g = a_date.month + 13;
  26. }
  27.  
  28. else
  29. {
  30. f = a_date.year;
  31. g = a_date.month + 1;
  32. }
  33.  
  34. N = (1461 * f / 4) + (153 * g / 5) + a_date.day;
  35.  
  36. return N;
  37. }
  38.  
  39. /* This function takes the number calculated in calc_date_number and returns a number */
  40. /* representing the day of the week (Sunday- Saturday */
  41. /* ---------------------------------------------------------------------------------- */
  42.  
  43. int calc_day_number(long int number)
  44. {
  45.  
  46. return (number - 621049) % 7;
  47. }
  48.  
  49. /* This function takes the numeric representation and returns the English representation */
  50. /* of the day of the week */
  51. /* ------------------------------------------------------------------------------------- */
  52.  
  53. const char* display_day_of_week(long int day_of_week)
  54. {
  55. const char* days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  56.  
  57. return days[day_of_week];
  58. }
  59.  
  60. /* Begin function main */
  61. /* ------------------- */
  62.  
  63. int main()
  64. {
  65. /* Declare variables */
  66. /* ----------------- */
  67.  
  68. struct date a_date;
  69. long int days;
  70. long int day_number;
  71. const char* dayofweek;
  72. long int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  73.  
  74. printf("Welcome to the Date to Day-of-Week program.\n");
  75. printf("\nThe program will give the day of the week for any date from 1/1/1900\n\n");
  76.  
  77. /* Checks to see if year is a leap year. */
  78. /* ------------------------------------- */
  79.  
  80. if((a_date.year%4==0 && a_date.year%100!=0) || (a_date.year%4==0 && a_date.year%400==0))
  81. daysPerMonth[1] = 29;
  82.  
  83. while (1)
  84. {
  85. /* Prompt user for date in format mm/dd/yyyy entered. */
  86. /* -------------------------------------------------- */
  87.  
  88. printf("Enter date (mm/dd/yyyy): ");
  89. scanf("%d/%d/%d", &a_date.month, &a_date.day, &a_date.year);
  90.  
  91. if (a_date.month < 1 || a_date.month > 12)
  92. {
  93. printf("Invalid month, please re-enter date.\n");
  94. continue;
  95. }
  96.  
  97. else if (a_date.day < 1 || a_date.day > 31)
  98. {
  99. printf("Invalid day, please re-enter date.\n");
  100. continue;
  101. }
  102.  
  103. else if (a_date.year < 1900) {
  104. printf("Invalid year, please re-enter date. Year must be greater than 1900\n");
  105. continue;
  106. }
  107.  
  108. days = calc_date_number (a_date);
  109. day_number = calc_day_number(days);
  110. dayofweek = display_day_of_week(day_number);
  111.  
  112. /* Display the results. */
  113. /* -------------------- */
  114.  
  115. printf("%d/%d/%d falls on a %s.\n\n", a_date.month, a_date.day, a_date.year, dayofweek);
  116. break;
  117.  
  118. } /* End loop */
  119.  
  120. /* Output ending message */
  121. /* --------------------- */
  122.  
  123. printf("Thank you for using the Date to Day-of-Week program.\n");
  124.  
  125. getchar();
  126.  
  127. return 0;
  128.  
  129. } /* End main */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement