Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- enum eDay { //days
- sunday = 1,
- monday,
- tuesday,
- wednesday,
- thursday,
- friday,
- saturday
- };
- enum eMonth { //months
- january = 1,
- february,
- march,
- april,
- may,
- juny,
- july,
- august,
- september,
- november,
- december
- };
- int main()
- {
- enum eDay day = 0;
- enum eMonth month = 0;
- int firstDayMonth = 0;
- printf("Hello! Welcome to the day calculator!\n");
- while(1) //work till (break;)
- {
- printf("Enter month to check: (1-Jan, 2-Feb, etc) ");
- scanf("%d", &month);
- printf("Enter day to check: ");
- scanf("%d", &day);
- if (checkInput(day, month) == 1)
- {
- do
- {
- printf("Enter the weekday of the 1st of the month: (1-Sunday, 2-Monday, etc) ");
- scanf("%d", &firstDayMonth);
- } while (firstDayMonth < 1 || firstDayMonth > 7);
- switch(dayInWeek(day, month, firstDayMonth)) //for each month - each text
- {
- case sunday:
- printf("The %02d.%02d will be a Sunday");
- break;
- case monday:
- printf("The %02d.%02d will be a Monday");
- break;
- case tuesday:
- printf("The %02d.%02d will be a Tuesday");
- break;
- case wednesday:
- printf("The %02d.%02d will be a Wednesday");
- break;
- case thursday:
- printf("The %02d.%02d will be a Thursday");
- break;
- case friday:
- printf("The %02d.%02d will be a Friday");
- break;
- case saturday:
- printf("The %02d.%02d will be a Saturday");
- break;
- }
- break; //all is done, stop program (leave loop)
- }
- else //error in checkInput() method
- {
- printf("Invalid input, try again\n");
- }
- }
- }
- /*
- Calculates the dayName according given DATE (day, month, first day in this month)
- input: day, month, first day in the month
- output: the dayName in the given month in the given day
- */
- int dayInWeek(enum eDay day, enum eMonth month, int firstDayInMonth)
- {
- int result = 0;
- result = (firstDayInMonth + --day) % 7;
- if (result == 0)
- {
- result = 7;
- }
- return result;
- }
- /*
- Checks if the day correct according the month (example: in february the day limit is 28)
- input: month, day in this month
- output: 1 - if valid date, 0 - if invalid
- */
- int checkInput(enum eDay d, enum eMonth m)
- {
- //check month validity
- if(m >= 1 && m <= 12)
- {
- //check day validity
- if(d >= 1 && d <= 31)
- {
- 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
- {
- return 1; //valid date
- }
- 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
- {
- return 1; //valid date
- }
- else if((d >= 1 && d <= 28) && (m == 2)) //check for february if the date day between 1-28
- {
- return 1; //valid date
- }
- else
- {
- return 0; //invalid day
- }
- }
- else
- {
- return 0; //day is invalid
- }
- }
- else
- {
- return 0; //month is invalid
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement