Can somebody test this code please

Printable View

• 10-09-2001
andy bee
Can somebody test this code please
/*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;
}
• 10-09-2001
SPOOK
I think the test for a leap year should be more like

if(year%4 == 0 && year % 100 != 0 || year % 400 == 0)
{
return 1;
}
else
return 0;

as the year being tested needs to match more than 1 of the criteria for an identification to be made
• 10-09-2001
Salem
printf("Usage: %s DD:MM:YYYY\n",argv[0]);

You missed of the string to print.

Do you have some input which you think doesn't work?
• 10-09-2001
andy bee
I have changed program above to hold suggestions.
Same error comes up as below :-

int _tmain(int argc, tchar* argv[], tchar* envp[])

error is:- line 49 : ) expected.

only shows this not much wrong i think.
• 10-09-2001
SPOOK
code
could you edit your post using code tags to make the logic easier to follow as the lack of indentation makes your intentions unclear, also could you write a sentance to explain what you want the program to do
• 10-09-2001
andy bee
Re: code
Quote:

Originally posted by SPOOK
could you edit your post using code tags to make the logic easier to follow as the lack of indentation makes your intentions unclear, also could you write a sentance to explain what you want the program to do

The following alogrithm can be used to find the day of the week for any date between 1st jan 1800 and 31st dec 1999.
1. Take the last two digits of the year.
2. Add a quarter of this, neglecting any remainder.
3. Add the day of the month.
4. For January add 1 (if leapyear add 0)
febuary add 4 (if leapyear add 3)
march add 4
april add 0
may add 2
june add 5
july add 0
august add 3
september add 6
october add 1
november add 4
december add 6
5. For 1800 - 1899 add 2
1900 onwards add 0
6. Divide the result by 7 and the remainder gives the day of the week:
1 = sunday, 2 = monday, 3 = tuesday, 4 = wednesday, 5 = thursday, 6 = friday and 0 = saturday.

For example :- 27TH AUGUST 1815 IS ;
15 + 3 + 27 + 3 + 2 = 50
50 / 7 = 7 remainder 1 = sunday.

I urgently need to write a program that will accept the date , using the format of DD:MM:YYYY. Then using the above method (only) calculate which day of week it falls on and display this day.
You can assume that the date input is valid.
• 10-09-2001
SPOOK
andy bee

check your message inbox