Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.74 KB | None | 0 0
  1. #include <stdio.h> /*This is a standard input output header file.
  2. C's functionality comes from the use of libraries.
  3. These header files contain information such as functions, declarations and macros.*/
  4. #include <time.h> /*This is a time header file that defines functions to allow the manipulation of date and time.*/
  5.  
  6. /*The purpose of this program is to allow the user to input two dates between the years 1-10000,
  7. and the program will then output the difference in days between these two dates.
  8. This is similar to the first program, however it now takes into consideration leap years.*/
  9. main () /*This is the main function where the program is executed*/
  10. {
  11.  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.*/
  12.  int d1,m1,y1; /*These are the variable declarations of the first day, month and year. These are integer variable types. */
  13.  int d2,m2,y2; /*These are the variable declarations of the second day, month and year. These are also integer variable types.*/
  14.  int error=1; /*This is a variable being used to check if the inputs from scanf contain errors.*/
  15.  char sep1, sep2, sep3, sep4; /*These are the variable declarations of the separations between the dates. These are also character variable types.*/
  16.  
  17. while(error==1){
  18. /*This is the beginning of the while loop. When it is true(non zero) the code inside the while loop is executed.
  19. When it is false(zero) the loop is terminated.*/
  20.  
  21. scanf("%d%c%d%c%d", &d1,&sep1,&m1,&sep2,&y1);
  22. /*This scanf function will read the input from the standard input(keyboard).
  23. In this case, it reads the first date including the separators(/ or -)*/
  24. scanf("%d%c%d%c%d", &d2,&sep3,&m2,&sep4,&y2);
  25. /*This scanf function will read the input from the standard input(keyboard).
  26. In this case, it reads the second date including the separators(/ or -)*/
  27.  
  28.    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)))
  29.        error=0; /*This if statement ensures that if it is a leap year, then the number of days in February is equal to 29.)*/
  30.    else if ((y2%4==0 && y2%100!=0 && d2==29 && m2==2) || (d2==29 && m2==2 && y1%400==0))
  31.        error=0; /*This if statement ensures that if it is a leap year, then the number of days in February is equal to 29.)*/
  32.    else if (daysofeachmonth[m1-1]<d1 || d1==0)
  33.        printf("The date you entered is invalid, please enter a valid date.\n");
  34.        /*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)
  35.        it will print the chosen sentence to the standard output(screen)*/
  36.    else if (daysofeachmonth[m2-1]<d2 || d2==0)
  37.        fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
  38.         /*This if statement is the same as the previous one, but for the second date*/
  39.    else if ((1>m1 && m1>12) || (1>m2 && m2>12))
  40.        fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
  41.        /*This if statement ensures that if the month entered is less than 1 or
  42.        greater than 12 it will print the chosen sentence to the standard output(screen)*/
  43.    else if ((0>=y1 && y1>=10000) || (0>=y2 && y2>=10000))
  44.        fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
  45.        /*This if statement ensures that if the year is less than or equal to 0 or greater than 10000
  46.        it will print the chosen sentence to the standard output(screen)*/
  47.    else if ((sep1!=sep2 && sep1!="/") || (sep1!=sep2 && sep1!="-"))
  48.        fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
  49.         /*This if statement ensures that if the separators for the first date are not equal to each other and equal to either / or -
  50.           it will print the chosen sentence to the standard output(screen)*/
  51.    else if ((sep3!=sep4 && sep3!="/") || (sep3!=sep4 && sep3!="-"))
  52.        fprintf(stderr, "The date you entered is invalid, please enter a valid date.\n");
  53.        /*This if statement ensures that if the separators for the second date are not equal to each other and equal to either / or -
  54.           it will print the chosen sentence to the standard output(screen)*/
  55.    else
  56.    error=0; /*This is where the while loop is terminated*/
  57. }
  58.  struct tm date1; /*This is a structure used to hold the first time and date*/
  59.  struct tm date2; /*This is a structure used to hold the second time and date*/
  60.  time_t tempdate1, tempdate2; /*This is a variable type for storing the calendar time*/
  61.  double days;
  62.  
  63.  date1.tm_hour = 0;  date1.tm_min = 0; date1.tm_sec = 0;
  64.  date1.tm_mday = d1; date1.tm_mon = m1-1; date1.tm_year = y1-1900;
  65.  date1.tm_isdst = 0;
  66.   /*This sets up the first date where the time is 0:0:0 d/m1-1/y1-1900.
  67.  The reason for m1-1, is because the array is actually from 0-11 and not 1-12.
  68.  The reason for y1-1900 is because tm_year is the years since 1900.*/
  69.  date2.tm_hour = 0;  date2.tm_min = 0;  date2.tm_sec = 0;
  70.  date2.tm_mday = d2; date2.tm_mon = m2-1; date2.tm_year = y2-1900;
  71.  date2.tm_isdst = 0;
  72.   /*This sets up the second date in the same way as the first date was set up.*/
  73.  
  74.  tempdate1 = mktime(&date1); /*This takes the structure and converts into a time object.*/
  75.  tempdate2 = mktime(&date2); /*This takes the structure and converts into a time object.*/
  76.  
  77.  days = difftime(tempdate2, tempdate1) / 86400;
  78.  /* This finds the difference in seconds between the two dates.
  79.   The value is then divided by 86400 (The seconds in 1 day) to convert it into number of days.*/
  80.  
  81.  printf ("There are %d days between the two dates.\n", (int)days);
  82.  /* This prints the number of days between the two dates to the standard output (screen).*/
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement