Advertisement
Guest User

Untitled

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