Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct date
- {
- int month;
- int day;
- int year;
- };
- int calc_date_number(struct date a_date)
- {
- long f;
- long g;
- long N;
- if (a_date.month <= 2) {
- f = a_date.year - 1;
- g = a_date.month + 13;
- }
- else
- {
- f = a_date.year;
- g = a_date.month + 1;
- }
- printf("f = %ld\n", f);
- printf("g = %ld\n", g);
- printf("day = %d\n", a_date.day);
- N = (1461 * f / 4) + (153 * g / 5) + a_date.day;
- printf("calc_date_number: %ld\n", N);
- return N;
- }
- int calc_day_number(long int number)
- {
- return (number - 621049) % 7;
- }
- const char* display_day_of_week(int day_of_week)
- {
- const char* days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- return days[day_of_week];
- }
- int main()
- {
- struct date a_date;
- long days;
- long day_number;
- const char* dayofweek;
- int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- printf("Welcome to the Date to Day-of-Week program.\n");
- printf("\nThe program will give the day of the week for any date from 1/1/1900\n\n");
- while (1)
- {
- printf("Enter date (mm/dd/yyyy): ");
- scanf("%d/%d/%d", &a_date.month, &a_date.day, &a_date.year);
- if (a_date.month < 1 || a_date.month > 12)
- {
- printf("Invalid month, please re-enter date.\n");
- continue;
- }
- else if (a_date.day < 1 || a_date.day > 31)
- {
- printf("Invalid day, please re-enter date.\n");
- continue;
- }
- else if (a_date.year < 1900) {
- printf("Invalid year, please re-enter date. Year must be greater than 1900\n");
- continue;
- }
- days = calc_date_number(a_date);
- day_number = calc_day_number(days);
- dayofweek = display_day_of_week(day_number);
- printf("%d/%d/%d falls on a %s.\n", a_date.month, a_date.day, a_date.year, dayofweek);
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement