Advertisement
LegoDrifter

AUD 1 - Dates

Mar 2nd, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. struct date {
  3.     int year;
  4.     int month;
  5.     int day;
  6. };
  7. typedef struct date date;
  8.  
  9. int compare(date d1,date d2){
  10.     if(d1.year>d2.year)
  11.             return 1;
  12.     else if(d1.year<d2.year)
  13.             return -1;
  14.     else if(d1.month>d2.month)
  15.             return 1;
  16.     else if(d1.month<d2.month)
  17.             return -1;
  18.     else if(d1.day>d2.day)
  19.             return 1;
  20.     else if(d1.day<d2.day)
  21.             return -1;
  22.     else return 0;
  23. }
  24.  
  25. int days(date d1,date d2){
  26.     int days=d1.day-d2.day;
  27.     days+=(d1.month-d2.month)*30;
  28.     days+=(d1.year-d2.year)*365;
  29.     return days;
  30. }
  31.  
  32. void read(date *d){
  33.     scanf("%d.%d.%d",&d->day,&d->month,&(*d).year);
  34. }
  35. void print(date *d){
  36. printf("%02d.%02d.%02d\n",d->day,d->month,d->year);
  37. }
  38. int main()
  39. {
  40.     date d1;
  41.     date d2;
  42.     read(&d1);
  43.     read(&d2);
  44.     int res=compare(d1,d2);
  45.     if(res==0)
  46.     {
  47.      printf("The dates are equal \n");
  48.     }
  49.     if(res>1)
  50.     {
  51.         printf("The difference in days is %d days",days(d1,d2));
  52.     }
  53.     else
  54.     {
  55.         printf("The difference in days is %d days",days(d2,d1));
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement