Simplified code

This is a discussion on Simplified code within the C Programming forums, part of the General Programming Boards category; Hello; I have this code that will give me the next day based upon what the user has entered for ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    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;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    Last edited by Prelude; 12-05-2001 at 04:05 PM.
    My best code is written with the delete key.

  3. #3
    Banned maes's Avatar
    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; 
    }
    I hope this helps
    Last edited by maes; 12-05-2001 at 04:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21