[ back to toc ]

simplified code

Date: 2001/12/06 20:40

Q:
Hello;

I have this code that will give me the next day based upon what the user
has entered for day, month and year, and checks for leap year. As you can
see, the code is quite lenghty. I was wondering if you could suggest a
way to simplify or shorten it. The code is below. Thank you.

#include<stdio.h>

enum month{jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
typedef enum month month;

void next_day(month m, int day, int year)
{

switch (m)
{
case jan:
if(day>= 31)
printf("The next day would be: February 1 &d\n", year);
if(day < 31)
printf("The next day would be: January %d %d\n", day + 1,
year);
break;
case feb:
if(day >= 29)
{
if(year %4 == 0 || year % 400 == 0)
printf("The next day would be: March 1 %d This is a leap
year.\n", year);
}
if(day < 28)
printf("The next day would be: February %d %d\n", day + 1,
year);
break;
case mar:
if(day>= 31)
printf("The next day would be: April 1 &d\n", year);
if(day < 31)
printf("The next day would be: March %d %d\n", day + 1, year);
break;
case apr:
if(day>= 30)
printf("The next day would be: May 1 &d\n", year);
if(day < 30)
printf("The next day would be: April %d %d\n", day + 1, year);
break;
case may:
if(day>= 31)
printf("The next day would be: June 1 &d\n", year);
if(day < 31)
printf("The next day would be: May %d %d\n", day + 1, year);
break;
case jun:
if(day>= 30)
printf("The next day would be: July 1 &d\n", year);
if(day < 30)
printf("The next day would be: July %d %d\n", day + 1, year);
break;
case jul:
if(day>= 31)
printf("The next day would be: August 1 &d\n", year);
if(day < 31)
printf("The next day would be: July %d %d\n", day + 1, year);
break;
case aug:
if(day>= 31)
printf("The next day would be: September 1 &d\n", year);
if(day < 31)
printf("The next day would be: August %d %d\n", day + 1, year);
break;
case sep:
if(day>= 30)
printf("The next day would be: October 1 &d\n", year);
if(day < 30)
printf("The next day would be: September %d %d\n", day + 1,
year);
break;
case oct:
if(day>= 31)
printf("The next day would be: November 1 &d\n", year);
if(day < 31)
printf("The next day would be: October %d %d\n", day + 1, year);
break;
case nov:
if(day>= 30)
printf("The next day would be: December 1 &d\n", year);
if(day < 30)
printf("The next day would be: November %d %d\n", day + 1,
year);
break;
case dec:
if(day>= 31)
printf("The next day would be: January 1 &d\n", year);
if(day < 31)
printf("The next day would be: December %d %d\n", day + 1,
year);
break;
}
}

int main()
{
month m;
int day, year;
system("cls");
printf("\n\nEnter the Month: ");
scanf("%d", &m);
printf("\n\nEnter the Day: ");
scanf("%d", &day);
printf("\n\nEnter the Year: ");
scanf("%d", &year);
printf("You entered: %d %d %d\n", m, day, year);
next_day(m, day, year);
return 0;
}

A:
Visit http://scriptbasic.com and see the code I have implemented for the
BASIC interpreter to handle the YEAR, MONTH and alike functions.

Regards,
Peter

[ back to toc ]