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

Another method for printing the date

Discussion in 'C++' started by bashamsc, May 9, 2008.

  1. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai

    Another method for printing the date from starting to ending



    This method is useful if u r dealing with dates.If u want to do some operation using the date and then fetch another date and do vice versa it is useful.
    Code:
    #include<iostream>
    #include<malloc.h>
    using namespace std;
    
    int  *getdate(int  , int  , int  );
    
    main()
    {
    	
    	cout<<"Enter the starting date , month , year "<<endl;
    	int sd,sm,sy,ed,em,ey;
    	cin>>sd>>sm>>sy;
    	cout<<"Enter the ending date , month , year "<<endl;
    	cin>>ed>>em>>ey;
    	int a[3];
    	a[0]=sd,a[1]=sm,a[2]=sy;
    	while(1)
    	{
    		int *p;
    		cout<<"Day  "<<a[0]<<"  Month  "<<a[1]<<"  Year  "<<a[2]<<endl;
    		/*
    		if(a[2]==ey)
    		if(a[1]==em)
    		if(a[0]==ed)
    		break;
    		*/
    		if(a[2]==ey&&a[1]==em&&a[0]==ed)
    			break;
    		
    		p = getdate(a[0],a[1],a[2]);
    		a[0] = p[0] , a[1] = p[1] , a[2] = p[2];
    	}
    }
    
    int *getdate(int  a ,int  b ,int  c)
    {
    	int i;
    	static int *A;
    	A = new int[3];
    	A[0] = a , A[1] = b , A[2] = c;
    	if(b==2)
    	{//1
    		if(c%4==0)
    		{
    			for(i=1;i<=29;i++)
    			{
    				if(a==i&&i<29)
    				{
    					A[0]=A[0]+1;
    				}
    			}
    			if(a==29)
    			{
    				A[0]=1;
    				A[1]=A[1]+1;
    			}
    		}
    		if(c%4!=0)
    		{
    			for(i=1;i<=28;i++)
    			{
    				if(a==i&&i<28)
    				{
    					A[0]=A[0]+1;
    				}
    			}
    			if(a==28)
    			{
    				A[0]=1;
    				A[1]=A[1]+1;
    			}
    		}
    		
    	}//1
    	
    	if(b<8&&b!=2)
    	{//2
    		if(b%2!=0)
    		{
    			for(i=1;i<=31;i++)
    			{
    				if(a==i&&i<31)
    				{
    					A[0]=A[0]+1;
    				}
    			}
    			if(a==31)
    			{
    				A[0]=1;
    				A[1]=A[1]+1;
    			}
    			
    		}
    		
    		if(b%2==0)
    		{
    			for(i=1;i<=30;i++)
    			{
    				if(a==i&&i<30)
    				{
    					A[0]=A[0]+1;
    				}
    			}
    			if(a==30)
    			{
    				A[0]=1;
    				A[1]=A[1]+1;
    			}
    		}
    	}//2
    	
    	if(b>=8)
    	{//2
    		if(b%2!=0)
    		{//3
    			for(i=1;i<=30;i++)
    			{
    				if(a==i&&i<30)
    				{
    					A[0]=A[0]+1;
    				}
    			}
    			
    			if(a==30)
    			{
    				A[0]=1;
    				if(b!=12)
    					A[1]=A[1]+1;
    				if(b==12)
    				{
    					A[1]=1;
    					A[2]=A[2]+1;
    				}
    			}
    			
    		}//3
    		
    		if(b%2==0)
    		{//4
    			for(i=1;i<=31;i++)
    			{
    				if(a==i&&i<31)
    				{
    					A[0]=A[0]+1;
    				}
    			}
    			
    			if(a==31)
    			{
    				A[0]=1;
    				if(b!=12)
    					A[1]=A[1]+1;
    				if(b==12)
    				{
    					A[1]=1;
    					A[2]=A[2]+1;
    				}
    			}
    		}//4
    	}//2
    
    	return 0;
    }
    Anyone can implement this code in c also and u need to change the headers only nothing than that.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83

Share This Page