1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Date Program

Discussion in 'C' started by Peter_APIIT, Apr 15, 2007.

  1. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Hello every member of go4expert forum. I have something to share with you all. I have successfully developed a date program which prompt a user to enter a date(12/12/2005) and return the day of week(tuesday).
    Code:
    #ifndef CONVERTER_H_
    #define CONVERTER_H_
    
    struct U_Days
    {
    	int days;
    }u_days;
    
    struct U_Months
    {
    	int months;
    }u_months;
    
    struct U_Years
    {
    	int years;
    }u_years;
    
    
    // ----------------------------------------------------
    
    #endif /*CONVERTER_H_*/
    
    
    
    
    // Wrong Calculations for year 2003. 
    // This program uses to calcualte the date enter by user
    // and return the days of week. 
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "Converter.h"
    
    int Days_Of_Week(const struct U_Days *, const struct U_Months *, const struct U_Years *);
    
    int main(int argc, char * argv[]) 
    {
    	// 1.1.2003 == Wednesday;
    	char *Date[7]= {"Wednesday", 
    					"Thursday", 
    					"Friday", 
    			   		"Saturday", 
    					"Sunday",
    					"Monday",
    					"Tuesday"
    					};
    	
    // ---------------------------------------------------
    	
    	int result;				
    
    	struct U_Days *days_ptr;
    	struct U_Months *months_ptr;
    	struct U_Years *years_ptr;
    	
    	days_ptr = &u_days;
    	months_ptr = &u_months;
    	years_ptr = &u_years;
    
    // --------------------------------------------------
    
    	do
    	{
    		printf("Enter a day in integer: ");
    		scanf("%d", &days_ptr->days);
    
    		printf("Enter a month in integer: ");
    		scanf("%d", &months_ptr->months);
    
    		printf("Enter a year in integer: ");
    		scanf("%d", &years_ptr->years);
    
    		result = Days_Of_Week(days_ptr, months_ptr, years_ptr);
    		printf("\nThe date your enter equal to days of week is %s\n\n", Date[result]);
    	
    	}while( ((days_ptr->days >0) && (days_ptr->days <32)) || ((months_ptr->months >0) && (months_ptr->months <13)) );
    
        return 0;
        
    }
    
    int Days_Of_Week(const struct U_Days *days_ptr, const struct U_Months *months_ptr, const struct U_Years *years_ptr)
    {
    
    	int days_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    							 
    	int temp_days = 0;
    	int year = 2003, month, days;
    	int days_inside_months;
    
    // ------------------------------------------------------
    
    	if (years_ptr->years - year == 0)
    	{
    		for (month=0;month<months_ptr->months;month++)
    		{
    			temp_days = temp_days + days_of_month[month] ;
    		}
    		
    		days = days_of_month[months_ptr->months -1] - days_ptr->days;
    		temp_days = temp_days - days;
    	}
    	else
    	{
    		for (year=2003;year < years_ptr->years;year++)
    		{	
    			if (year % 4 ==0)
    			{
    				days_of_month[1] = 29;
    				for (month=0;month<12;month++)
    				{
    					temp_days = temp_days + days_of_month[month];
    				}
    			}
    			else
    			{
    				days_of_month[1] = 28;
    				for (month=0;month<12;month++)
    				{	
    					temp_days = temp_days + days_of_month[month];
    				}
    			}
    				
    		}
    	}
    
    	days = 0;
    	
    	if (years_ptr->years % 4 == 0 && years_ptr->years > 2003)
    	{
    		days_of_month[1] = 29;
    		for (month=0;month<months_ptr->months;month++)
    		{
    			days = days + days_of_month[month];
    		}
    		days_inside_months = days_of_month[months_ptr->months - 1] - days_ptr->days;
    		temp_days = temp_days + days - days_inside_months;
    	}
    	else
    	{
    		days_of_month[1] = 28;
    		if ( years_ptr->years == 2002)
    		{
    			for (month=0;month<months_ptr->months;month++)
    			{
    				temp_days = temp_days + days_of_month[month] ;
    			}
    			days = days_of_month[months_ptr->months -1] - days_ptr->days;
    			temp_days = temp_days - days - 1;
    		}
    		else
    		{
    			for (month=0;month<months_ptr->months;month++)
    			{
    				days = days + days_of_month[month];
    			}
    			days_inside_months = days_of_month[months_ptr->months - 1] - days_ptr->days;
    			temp_days = temp_days + days - days_inside_months;
    		}
    	}
    		
        printf("\nTotal of days is %d", temp_days);
    
    
    	if (temp_days % 7 == 1)
    	{
    		return 0;
    	}
    
    	else if (temp_days % 7 == 2)
    	{
    		return 1;
    	}
    
    	else if (temp_days % 7 == 3)
    	{
    		return 2;
    	}
    
    	else if (temp_days % 7 == 4)
    	{
    		return 3;
    	}
    
    	else if (temp_days % 7 == 5)
    	{
    		return 4;
    	}
    
    	else if (temp_days % 7 == 6)
    	{	
    		return 5;
    	}
    
    	else if (temp_days % 7 == 0)
    	{
    		return 6;
    	}
    	
    }
    I hope this can help others programmer.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Instead of having to go through such a code I would like to be having some more doc as to what it does and its my personal opinion.
     
  3. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    I will do that later ans post back here.

    Any criticsm, opinion or recommemdations is greatly appreciated.

    Thanks for your(shabbir) opinion.
     

Share This Page