-
Simplified code
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;
}
-
Here's a stripped down version. Normally I don't use scanf, but I wanted to shorten the code a bit and parsing a string takes space ;) Also, please note that this isn't exact, just a starter.
Code:
int main(){
int dd, mm, yy;
puts("Enter the current date (mm/dd/yy):");
scanf("%d/%d/%d", &mm, &dd, &yy);
if((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0)
puts("This is a leap year");
else
puts("This is not a leap year");
if(mm == 12 && dd == 31){
mm = dd = 1;
printf("The next day will be: %d/%d/%d", mm, dd, ++yy);
}else
printf("The next day will be: %d/%d/%d", mm, ++dd, yy);
return 0;
}
-Prelude
-
I've shorten your next_day function a bit.
max is the max nuber of day of the month
also, test the code to see if everything works, I was a little bit lazy:p
Code:
#include<stdio.h>
#include<stdlib.h>
const int max_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
const char months[][12]={"January","February","March","April","May","June","July","August","Septmber","October","November","December"};
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)
{
int max;
if(m==feb)
{
if(day >= 28)
{
if(year %4 == 0 || year % 400 == 0)
max=29;
else
max=28;
}
else
max=28;
}
else
max=max_day[m-1];
if(day>= max)
printf("The next day would be: %s 1 %d\n", months[m],year);
if(day < max)
printf("The next day would be: %s %d %d\n", months[m-1],day + 1, year);
return;
}
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;
}
I hope this helps