Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /* Structs */
- /* ------- */
- struct date
- {
- int month;
- int day;
- int year;
- };
- /* This function takes a date and returns a number representing the date entered */
- /* ----------------------------------------------------------------------------- */
- long int calc_date_number(struct date a_date)
- {
- long int f;
- long int g;
- long int 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;
- }
- N = (1461 * f / 4) + (153 * g / 5) + a_date.day;
- return N;
- }
- /* This function takes the number calculated in calc_date_number and returns a number */
- /* representing the day of the week (Sunday- Saturday */
- /* ---------------------------------------------------------------------------------- */
- int calc_day_number(long int number)
- {
- return (number - 621049) % 7;
- }
- /* This function takes the numeric representation and returns the English representation */
- /* of the day of the week */
- /* ------------------------------------------------------------------------------------- */
- const char* display_day_of_week(long int day_of_week)
- {
- const char* days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- return days[day_of_week];
- }
- /* Begin function main */
- /* ------------------- */
- int main()
- {
- /* Declare variables */
- /* ----------------- */
- struct date a_date;
- long int days;
- long int day_number;
- const char* dayofweek;
- long 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");
- /* Checks to see if year is a leap year. */
- /* ------------------------------------- */
- if((a_date.year%4==0 && a_date.year%100!=0) || (a_date.year%4==0 && a_date.year%400==0))
- daysPerMonth[1] = 29;
- while (1)
- {
- /* Prompt user for date in format mm/dd/yyyy entered. */
- /* -------------------------------------------------- */
- 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);
- /* Display the results. */
- /* -------------------- */
- printf("%d/%d/%d falls on a %s.\n\n", a_date.month, a_date.day, a_date.year, dayofweek);
- break;
- } /* End loop */
- /* Output ending message */
- /* --------------------- */
- printf("Thank you for using the Date to Day-of-Week program.\n");
- getchar();
- return 0;
- } /* End main */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement