Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. enum month {Jan=1, Feb , Mar , Apr, May, June, July, Aug, Sep, Oct, Nov, Dec};
  5. enum day {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat};
  6.  
  7. /* function calculating and return the day of the week.
  8. input: day in the week, month, day for checking.
  9. output: day in the week after calculating.
  10. */
  11. int day_of_the_week (int month, int day, int weekDay)
  12. {
  13. int dayOfTheWeek = ((day + (weekDay-1))%7);
  14. return dayOfTheWeek;
  15. }
  16. /* the function checking the invalid inputs.
  17. input: month, day and weekday.
  18. output: it tells us that we have a mistake and we need to change it.
  19. */
  20. int checkInput (int month, int day, int weekDay)
  21. {
  22. int return_=0;
  23.  
  24. while (month>12 || day>31 || weekDay>7 || month<1 || day<1 || weekDay<1)
  25. {
  26. printf("Invalid input, try again\n");
  27. printf("Enter month to check:\n");
  28. scanf("%d", &month);
  29. printf("Enter day to check:\n");
  30. scanf("%d", &day);
  31. printf("Enter the weekday of the 1st of the month:(1-sunday, 2-monday, etc)\n");
  32. scanf("%d", &weekDay);
  33. }
  34.  
  35. while (day > 28 && month == 2)
  36. {
  37. printf("Invalid input, try again\n");
  38. printf("Enter month to check:\n");
  39. scanf("%d", &month);
  40. printf("Enter day to check:\n");
  41. scanf("%d", &day);
  42. printf("Enter the weekday of the 1st of the month:(1-sunday, 2-monday, etc)\n");
  43. scanf("%d", &weekDay);
  44. }
  45.  
  46. while (day > 31 && (month == 12 || month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10))
  47. {
  48. printf("Invalid input, try again\n");
  49. printf("Enter month to check:\n");
  50. scanf("%d", &month);
  51. printf("Enter day to check:\n");
  52. scanf("%d", &day);
  53. printf("Enter the weekday of the 1st of the month:(1-sunday, 2-monday, etc)\n");
  54. scanf("%d", &weekDay);
  55. }
  56.  
  57. while (day > 30 && (month == 4 || month == 6 || month == 9 || month ==11 ))
  58. {
  59. printf("Invalid input, try again\n");
  60. printf("Enter month to check:\n");
  61. scanf("%d", &month);
  62. printf("Enter day to check:\n");
  63. scanf("%d", &day);
  64. printf("Enter the weekday of the 1st of the month:(1-sunday, 2-monday, etc)\n");
  65. scanf("%d", &weekDay);
  66. }
  67.  
  68. printf("The %d.%d will be ", day,month);
  69.  
  70. return_= day_of_the_week(month,day,weekDay);
  71.  
  72. return return_;
  73.  
  74. }
  75.  
  76.  
  77. int main(void)
  78. {
  79.  
  80. int month =0;
  81. int day =0;
  82. int weekDay =0;
  83.  
  84.  
  85. printf("Hello! Welcome to the day calculator!\n");
  86. printf("Enter month to check:\n");
  87. scanf("%d", &month);
  88. printf("Enter day to check:\n");
  89. scanf("%d", &day);
  90. printf("Enter the weekday of the 1st of the month:(1-sunday, 2-monday, etc)\n");
  91. scanf("%d", &weekDay);
  92.  
  93. int retInput = checkInput( month,day, weekDay );
  94.  
  95. if ( retInput == Sun )
  96. {
  97. printf("Sunday.\n");
  98. }
  99.  
  100. else if ( retInput == Mon )
  101. {
  102. printf("Monday.\n");
  103. }
  104.  
  105. else if ( retInput == Tue )
  106. {
  107. printf("Tuesday.\n");
  108. }
  109. else if ( retInput == Wed )
  110. {
  111. printf("Wednesday.\n");
  112. }
  113. else if ( retInput == Thu )
  114. {
  115. printf("Thursday.\n");
  116. }
  117. else if ( retInput == Fri)
  118. {
  119. printf("Friday.\n");
  120. }
  121. else if ( retInput == Sat )
  122. {
  123. printf("Saturday.\n");
  124. }
  125.  
  126. getch();
  127. return(0);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement