![]() |
| | #1 |
| Registered User Join Date: Oct 2001
Posts: 13
| Simplified code 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; } |
| soonerfan is offline | |
| | #2 |
| Code Goddess Join Date: Sep 2001
Posts: 9,664
| 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;
}
__________________ My best code is written with the delete key. Last edited by Prelude; 12-05-2001 at 04:05 PM. |
| Prelude is offline | |
| | #3 |
| Banned Join Date: Aug 2001
Posts: 744
| 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 ![]() 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;
}
__________________ SVG is the future Last edited by maes; 12-05-2001 at 04:05 PM. |
| maes is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Enforcing Machine Code Restrictions? | SMurf | Tech Board | 21 | 03-30-2009 07:34 AM |
| Proposal: Code colouring | Perspective | General Discussions | 28 | 05-14-2007 07:23 AM |
| Values changing without reason? | subtled | C Programming | 2 | 04-19-2007 10:20 AM |
| Interface Question | smog890 | C Programming | 11 | 06-03-2002 05:06 PM |
| Who will map the scan code (inserted by VKD_Force_keys) to virtual key code? | Unregistered | Windows Programming | 0 | 02-21-2002 06:05 PM |