Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /*This is a standard input output header file.
- C's functionality comes from the use of libraries.
- These header files contain information such as functions, declarations and macros.*/
- #include <time.h> /*This is a time header file that defines functions to allow the manipulation of date and time.*/
- /*The purpose of this program is to allow the user to input two dates between the years 1-10000,
- and the program will then output the difference in days between these two dates.
- This is similar to the first program, however it now takes into consideration leap years.*/
- main () /*This is the main function where the program is executed*/
- {
- int daysofeachmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31}; /*This is a one-dimensional array that stores the days of each month.*/
- int d1,m1,y1; /*These are the variable declarations of the first day, month and year. These are integer variable types. */
- int d2,m2,y2; /*These are the variable declarations of the second day, month and year. These are also integer variable types.*/
- int error=1; /*This is a variable being used to check if the inputs from scanf contain errors.*/
- char sep1, sep2, sep3, sep4; /*These are the variable declarations of the separations between the dates. These are also character variable types.*/
- while(error==1){
- /*This is the beginning of the while loop. When it is true(non zero) the code inside the while loop is executed.
- When it is false(zero) the loop is terminated.*/
- scanf("%d%c%d%c%d", &d1,&sep1,&m1,&sep2,&y1);
- /*This scanf function will read the input from the standard input(keyboard).
- In this case, it reads the first date including the separators(/ or -)*/
- scanf("%d%c%d%c%d", &d2,&sep3,&m2,&sep4,&y2);
- /*This scanf function will read the input from the standard input(keyboard).
- In this case, it reads the second date including the separators(/ or -)*/
- if (((y1%4==0 && y1%100!=0||y1%400==0) && d1==29 && m1==2) || (d1==29 && m1==2 && (y2%400==0||y2%4==0 && y2%100!=0)))
- error=0; /*This if statement ensures that if it is a leap year, then the number of days in February is equal to 29.)*/
- else if ((y2%4==0 && y2%100!=0 && d2==29 && m2==2) || (d2==29 && m2==2 && y1%400==0))
- error=0; /*This if statement ensures that if it is a leap year, then the number of days in February is equal to 29.)*/
- else if (daysofeachmonth[m1-1]<d1 || d1==0)
- printf("The date you entered is invalid, please enter a valid date.\n");
- /*This if statement ensures that if the day is equal to 0 or greater than the value stored in the array (for the corresponding month)
- it will print the chosen sentence to the standard output(screen)*/
- else if (daysofeachmonth[m2-1]<d2 || d2==0)
- fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
- /*This if statement is the same as the previous one, but for the second date*/
- else if ((1>m1 && m1>12) || (1>m2 && m2>12))
- fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
- /*This if statement ensures that if the month entered is less than 1 or
- greater than 12 it will print the chosen sentence to the standard output(screen)*/
- else if ((0>=y1 && y1>=10000) || (0>=y2 && y2>=10000))
- fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
- /*This if statement ensures that if the year is less than or equal to 0 or greater than 10000
- it will print the chosen sentence to the standard output(screen)*/
- else if ((sep1!=sep2 && sep1!="/") || (sep1!=sep2 && sep1!="-"))
- fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
- /*This if statement ensures that if the separators for the first date are not equal to each other and equal to either / or -
- it will print the chosen sentence to the standard output(screen)*/
- else if ((sep3!=sep4 && sep3!="/") || (sep3!=sep4 && sep3!="-"))
- fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
- /*This if statement ensures that if the separators for the second date are not equal to each other and equal to either / or -
- it will print the chosen sentence to the standard output(screen)*/
- else
- error=0; /*This is where the while loop is terminated*/
- }
- struct tm date1; /*This is a structure used to hold the first time and date*/
- struct tm date2; /*This is a structure used to hold the second time and date*/
- time_t tempdate1, tempdate2; /*This is a variable type for storing the calendar time*/
- double days;
- date1.tm_hour = 0; date1.tm_min = 0; date1.tm_sec = 0;
- date1.tm_mday = d1; date1.tm_mon = m1-1; date1.tm_year = y1-1900;
- date1.tm_isdst = 0;
- /*This sets up the first date where the time is 0:0:0 d/m1-1/y1-1900.
- The reason for m1-1, is because the array is actually from 0-11 and not 1-12.
- The reason for y1-1900 is because tm_year is the years since 1900.*/
- date2.tm_hour = 0; date2.tm_min = 0; date2.tm_sec = 0;
- date2.tm_mday = d2; date2.tm_mon = m2-1; date2.tm_year = y2-1900;
- date2.tm_isdst = 0;
- /*This sets up the second date in the same way as the first date was set up.*/
- tempdate1 = mktime(&date1); /*This takes the structure and converts into a time object.*/
- tempdate2 = mktime(&date2); /*This takes the structure and converts into a time object.*/
- days = difftime(tempdate2, tempdate1) / 86400;
- /* This finds the difference in seconds between the two dates.
- The value is then divided by 86400 (The seconds in 1 day) to convert it into number of days.*/
- printf ("There are %d days between the two dates.\n", (int)days);
- /* This prints the number of days between the two dates to the standard output (screen).*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement