/*Compiler playing up using turbo c++ 4*/
#include <stdio.h>
int isleapyear(int year)
{
if(year%4 == 0 && year % 100 != 0 || year % 400 == 0)
{
return 1;
}
else
return 0;
}
int isvaliddate(int day,int month,int year)
{
int maxnumdays;
if(day<0) return 0;
switch(month)
{
case 2:
maxnumdays=28;
if(isleapyear(year)) maxnumdays++;
break;
case 4:
case 6:
case 9:
case 11:
maxnumdays=30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
maxnumdays=31;
break;
default:
return 0;
}
if(day>maxnumdays) return 0;
return 1;
}
int _tmain(int argc, tchar* argv[], tchar* envp[])
{
int day,month,year;
int result;
if(argc!=2)
{
printf("Usage: %s DD:MM:YYYY\n",argv[0]);
return 1;
}
if(sscanf(argv[1],"%02d:%02d:%04d",&day,&month,&year)!=3)
{
printf("Bad input. Must be DD:MM:YYYY\n");
return 1;
}
if((year<1800)||(year>1999))
{
printf("Year must be in range [1800;1999]\n");
return 1;
}
if(isvaliddate(day,month,year)==0)
{
printf("%d:%d:%d is not a valid date\n",day,month,year);
return 1;
}
result=year%100; // 1. Take the last two digits of the year.
result*=5; // 2. Add a quarter of this, neglecting any remainder.
result/=4;
result+=day; // 3. Add the day of the month.
switch(month)
{
case 1: // For January add 1 (if leapyear add 0)
if(isleapyear(year)==0) result++;
break;
case 2: // febuary add 4 (if leapyear add 3)
result+=4;
if(isleapyear(year)) result--;
break;
case 9: // september add 6
case 12: // december add 6
result++;
case 6: // june add 5
result++;
case 3: // march add 4
case 11: // november add 4
result++;
case 8: // august add 3
result++;
case 5: // may add 2
result++;
case 10: // october add 1
result++;
// default:
// case 4: // april add 0
// case 7: // july add 0
}
if(year<1900) result+=2; // 5. For 1800 - 1899 add 2
printf("%d:%d:%d is a ",day,month,year);
switch(result%7) // 6. Divide the result by 7 and the remainder gives the day of the week:
{
case 0:
printf("Saturday"); // 0 = saturday.
break;
case 1:
printf("Sunday"); // 1 = sunday
break;
case 2:
printf("Monday"); // 2 = monday
break;
case 3:
printf("Tuesday"); // 3 = tuesday
break;
case 4:
printf("Wednesday"); // 4 = wednesday
break;
case 5:
printf("Thursday"); // 5 = thursday
break;
case 6:
printf("Friday"); // 6 = friday
}
printf("day\n");
return 0;
}