Advertisement
Yonka2019

Untitled

Jan 12th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. enum eDay { //days
  4.     sunday = 1,
  5.     monday,
  6.     tuesday,
  7.     wednesday,
  8.     thursday,
  9.     friday,
  10.     saturday
  11. };
  12. enum eMonth { //months
  13.     january = 1,
  14.     february,
  15.     march,
  16.     april,
  17.     may,
  18.     juny,
  19.     july,
  20.     august,
  21.     september,
  22.     november,
  23.     december
  24. };
  25. int main()
  26. {
  27.     enum eDay day = 0;
  28.     enum eMonth month = 0;
  29.     int firstDayMonth = 0;
  30.    
  31.     printf("Hello! Welcome to the day calculator!\n");
  32.  
  33.     while(1) //work till (break;)
  34.     {
  35.         printf("Enter month to check: (1-Jan, 2-Feb, etc) ");
  36.         scanf("%d", &month);
  37.  
  38.         printf("Enter day to check: ");
  39.         scanf("%d", &day);
  40.         if (checkInput(day, month) == 1)
  41.         {
  42.             do
  43.             {
  44.                 printf("Enter the weekday of the 1st of the month: (1-Sunday, 2-Monday, etc) ");
  45.                 scanf("%d", &firstDayMonth);
  46.  
  47.             } while (firstDayMonth < 1 || firstDayMonth > 7);
  48.            
  49.             switch(dayInWeek(day, month, firstDayMonth)) //for each month - each text
  50.             {
  51.                 case sunday:
  52.                     printf("The %02d.%02d will be a Sunday");
  53.                     break;
  54.                 case monday:
  55.                     printf("The %02d.%02d will be a Monday");
  56.                     break;
  57.                 case tuesday:
  58.                     printf("The %02d.%02d will be a Tuesday");
  59.                     break;
  60.                 case wednesday:
  61.                     printf("The %02d.%02d will be a Wednesday");
  62.                     break;
  63.                 case thursday:
  64.                     printf("The %02d.%02d will be a Thursday");
  65.                     break;
  66.                 case friday:
  67.                     printf("The %02d.%02d will be a Friday");
  68.                     break;
  69.                 case saturday:
  70.                     printf("The %02d.%02d will be a Saturday");
  71.                     break;
  72.                            
  73.             }
  74.             break; //all is done, stop program (leave loop)
  75.         }
  76.         else //error in checkInput() method
  77.         {
  78.             printf("Invalid input, try again\n");
  79.         }
  80.     }
  81. }
  82. /*
  83. Calculates the dayName according given DATE (day, month, first day in this month)
  84. input: day, month, first day in the month
  85. output: the dayName in the given month in the given day
  86. */
  87. int dayInWeek(enum eDay day, enum eMonth month, int firstDayInMonth)
  88. {
  89.     int result = 0;
  90.     result = (firstDayInMonth + --day) % 7;
  91.     if (result == 0)
  92.     {
  93.         result = 7;
  94.     }
  95.     return result;
  96. }
  97. /*
  98. Checks if the day correct according the month (example: in february the day limit is 28)
  99. input: month, day in this month
  100. output: 1 - if valid date, 0 - if invalid
  101. */
  102. int checkInput(enum eDay d, enum eMonth m)
  103. {
  104.     //check month validity
  105.     if(m >= 1 && m <= 12)
  106.     {
  107.         //check day validity
  108.         if(d >= 1 && d <= 31)
  109.         {
  110.             if((d >= 1 && d <= 30) && (m == 4 || m == 6 || m == 9 || m == 11)) //check for april juny september and november if the date day between 1-30
  111.             {
  112.                 return 1;   //valid date
  113.             }
  114.             else if((d >= 1 && d <= 31) && (m == 1 || m ==3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)) //check for january, march, may, july, august, october, december if the date day between 1-31
  115.             {
  116.                 return 1;   //valid date
  117.             }
  118.             else if((d >= 1 && d <= 28) && (m == 2)) //check for february if the date day between 1-28
  119.             {
  120.                 return 1;   //valid date
  121.             }
  122.             else
  123.             {
  124.                 return 0;   //invalid day
  125.             }
  126.         }
  127.         else
  128.         {
  129.             return 0;   //day is invalid
  130.         }
  131.     }
  132.     else
  133.     {
  134.         return 0; //month is invalid
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement