/* Mat Fishers program for reading and sorting dates */

#include <stdio.h>

int date1[11],date2[11],dates[4][20]; /* Setting up all the variables */
int limits[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; /* The number of days in each month */
int a,b,c,d1,d2,e,f,g,h,i,j,m,m1,m2,n,p,q,s,v,x,y1,y2,z;
int days(int d1, int d2, int m1, int m2, int y1, int y2);

int main(void)
 {  
  do {
  do {
  b = 1;
  
  for (i = 0; i < 11; i++)
    date1[i] = 0;
    
  d1 = 0;
  m1 = 0;
  y1 = 0;
  
  for (i = 0; i < 11; i++)     /* This will take the first date and insert it into the matrix date1 with each character split up  */
  {
    date1[i] = getchar();
    if (date1[i] == '\n')
      break;
  }
  if (date1[0] == '\n')
  {
    j = 1;
    break;
  }
   
  /* In the first part of this progream I have taken out entering the second
     date this is so that the program can check for errors at the end of each
     date entered. */
/*  for (i = 0; i < 11; i++)      Same as above but for the second date 
  {
    date2[i] = getchar();
    if (date2[i] == '\n')
      break;
  }
  if (date2[0] == '\n')
  {
    j = 1;
    break;
  } */
  

  for (i = 0; i < 11; i++)     /* Tests to see if the right characters are used */
  {
    m = date1[i];
/*  n = date2[i]; */
    s = 1;
/*  p = 1; */
    for (x = 48; x <= 57; x++)
    {
     if (m == x || m == 47 || m == 45 || m == 10 || m == 0)  /* Checks the correct characters in ascii format */
      s = 0;
/*   if (n == x || n == 47 || n == 45 || n == 10 || n == 0)
      p = 0; */
    }
    if (s == 1)
      {fprintf(stderr, "Wrong character used\n");b=0;break;}
    } 
  
  /* the next bunch of if test is just looking at the matrix and converting
     the answers into numbers they are testing to see if you put one or two
     digits in. It inserts the date, month and year into d1 m1 y1 respectivley.
     The minus 48 part is to get the numbers to work out right, I don't
     know why this is so but it works! */  
  
  if (date1[1] == '/' || date1[1] == '-')
  {
    d1 = date1[0] - 48;
    if (date1[3] == '/' || date1[3] == '-')
    {
      m1 = date1[2] - 48;
      y1 = ((date1[4] - 48) * 1000)+((date1[5] - 48) * 100)+((date1[6]  - 48) * 10) + (date1[7] - 48);
    }
    if (date1[4] == '/' || date1[4] == '-')
    {
      m1 = ((date1[2] - 48) * 10) + (date1[3] - 48);
      y1 = ((date1[5] - 48) * 1000)+((date1[6] - 48) * 100)+((date1[7] - 48) * 10) + (date1[8] - 48);
    }
   }
  if (date1[2] == '/' || date1[2] == '-')
  {
    d1 = ((date1[0] - 48) * 10) + (date1[1] - 48);
    if (date1[4] == '/' || date1[4] == '-')
    {
      m1 = date1[3] - 48;
      y1 = ((date1[5] - 48) * 1000)+((date1[6] - 48) * 100)+((date1[7] - 48) * 10) + (date1[8] - 48);
    }
    if (date1[5] == '/' || date1[5] == '-')
    {
      m1 = ((date1[3] - 48) * 10) + (date1[4] - 48);
      y1 = ((date1[6] - 48) * 1000)+((date1[7] - 48) * 100)+((date1[8] - 48) * 10) + (date1[9] - 48);
    }
   }
   
  /* The next set of if tests are the same as before but for the second date 
     also no needed in this version of the program */  
/*  if (date2[1] == '/' || date2[1] == '-')
  {
    d2 = date2[0] - 48;
    if (date2[3] == '/' || date2[3] == '-')
    {
      m2 = date2[2] - 48;
      y2 = ((date2[4] - 48) * 1000)+((date2[5] - 48) * 100)+((date2[6]  - 48) * 10) + (date2[7] - 48);
    }
    if (date2[4] == '/' || date2[4] == '-')
    {
      m2 = ((date2[2] - 48) * 10) + (date2[3] - 48);
      y2 = ((date2[5] - 48) * 1000)+((date2[6] - 48) * 100)+((date2[7] - 48) * 10) + (date2[8] - 48);
    }
   }
  if (date2[2] == '/' || date2[2] == '-')
  {
    d2 = ((date2[0] - 48) * 10) + (date2[1] - 48);
    if (date2[4] == '/' || date2[4] == '-')
    {
      m2 = date2[3] - 48;
      y2 = ((date2[5] - 48) * 1000)+((date2[6] - 48) * 100)+((date2[7] - 48) * 10) + (date2[8] - 48);
    }
    if (date2[5] == '/' || date2[5] == '-')
    {
      m2 = ((date2[3] - 48) * 10) + (date2[4] - 48);
      y2 = ((date2[6] - 48) * 1000)+((date2[7] - 48) * 100)+((date2[8] - 48) * 10) + (date2[9] - 48);
    }
   } */

  /* This next part checks to see if the inputed data is a valid date */
  if (y1 > 10000)
    {fprintf(stderr, "Year is too big\n");b=0;}
  if (y1 <= 0)
    {fprintf(stderr, "Year is too small\n");b=0;}
  if (m1 > 12)
    {fprintf(stderr, "Month is too big\n");b=0;}
  if (m1 <= 0)
    {fprintf(stderr, "Month is too small\n");b=0;}
  v = 1;                                      /* This part is to make sure that on a leap year the 29th of feb isnt too many days */
  if (limits[m1] < d1)
     v = 0;
  if ((y1%4) == 0 && m1 == 2 && d1 == 29)
     v = 1;
  if (v == 0)
    {fprintf(stderr, "There isnt that many days in that month\n");b=0;}
  if (d1 <= 0)
    {fprintf(stderr, "The day is too small\n");b=0;}
    
  } while(!b);
  
  /* This part takes the correct dates and inserts them into the matrix dates */
  
  if (j)  /* This test is to see if just enter is pressed so it exists and calculates */
   break;
  e++;
  dates[1][e] = d1;
  dates[2][e] = m1;
  dates[3][e] = y1;
  
  
  } while(!h);
   
  do {     /* This part looks at the dates matrix and sorts them into order */
  q = 0;
  for (p = 1; p < e; p++)
  {
    d1 = dates[1][p];
    m1 = dates[2][p];
    y1 = dates[3][p];
    d2 = dates[1][p+1];
    m2 = dates[2][p+1];
    y2 = dates[3][p+1];
    c = 0;                /* I needed to insert this as c is a global variable and it was getting the worng amount of days */
    c = days(d1,d2,m1,m2,y1,y2);
    if (c < 0 || y1 > y2)   /* It wasnt doing to well with sorting the year and as its the biggest variable ie if its out then so will be everything else, it can just be tested on its own */
    {
       dates[1][p] = d2;
       dates[2][p] = m2;
       dates[3][p] = y2;
       dates[1][p+1] = d1;
       dates[2][p+1] = m1;
       dates[3][p+1] = y1;
    }
  }
  
  /* This part checks to see if the data has been sorted or not if it hasnt then it is
     looped back and tried to be sorted again. */
    
  for (p = 1; p < e; p++)
  {
    d1 = dates[1][p];
    m1 = dates[2][p];
    y1 = dates[3][p];
    d2 = dates[1][p+1];
    m2 = dates[2][p+1];
    y2 = dates[3][p+1];
    c = 0;
    c = days(d1,d2,m1,m2,y1,y2);
    if (c >= 0 && y1 <= y2)
      q = 1;
    else
    { 
      q = 0;
      break;
    }
   }
  } while(!q);
  
  /* This will just print out the results */
 
  for (i = 1; i <= e; i++)
  {
    printf("%i/%i/%i\n",dates[1][i],dates[2][i],dates[3][i]);
  }
  system("PAUSE");
  return 0;
  }
  
int days(d1,d2,m1,m2,y1,y2)
{
  c = 0; /* To make sure of the global variable */
  /* The next part adds however many days are needed for different years */
  for (i = 0; i <= ((y2 - y1) - 1); i++)
  {
    if (((y1 + i)%4) != 0)
     c = c + 365;
    else
     c = c + 366;
  }
  
  /* This part looks to see if the months are the same and then just works out
  the differnce in days */
  if(m2 == m1)
  {
    c = c + (d2 - d1);
  }
  
  /* The next part looks to see which is the greater month then calculates
     the amount of days between them */
  else
  {
   if (m1 < m2)
    {
      if ((y1%4) == 0 && m1 == 2)
      c = c + limits[m1] + 1 - d1;
      else
      c = c + limits[m1] - d1;
      ++m1;
      do {
       if(m1 == m2)
         break;
       if ((y1%4) == 0 && m1 == 2)
       c = c + limits[m1] +1;
       else
       c = c + limits[m1];
       ++m1;
     } while(m1 == m2);
     c = c + d2;
    }
   if (m1 > m2)
    {
      if ((y2%4) == 0 && m2 == 2)
      c = c - limits[m2] + 1 + d2;
      else
      c = c - limits[m2] + d2;
      ++m2;
      do {
       if (m1 == m2)
         break;
       if ((y2%4) == 0 && m2 == 2)
       c = c - limits[m2] + 1;
       else
       c = c - limits[m2];
       ++m2;
     } while(m1 == m2);
     c = c - d1;
    }
   }
   /* Returns the remaining days in plus or minus form */
   return c;
}

