万年历

万年历

 

// 已知1980年1月1日是星期二!
#include < iostream.h >
#include
< process.h >
#include
< string .h >

struct  Date
{
 
int  year;
 
int  month;
 
int  date;
 
int  day;
 
char  week[ 10 ];
}
;

char   * dweek[ 7 ] = { " Sun " , " Mon " , " Tues " , " Weds " , " Thur " , " Fri " , " Sat " } ;
Date showday(Date pd)
{
 
int  i = 0 ,j = 0 ,k = 0 ,time,score;
 i
= (pd.year - 1980 ) * 365 + (pd.year - 1980 ) / 4 - (pd.year - 1980 ) / 100 + (pd.year - 1980 ) / 400 ;
 i
= i % 7 + 1 ;
 k
= (pd.date - 1 ) % 7
 
if ((pd.year % 100 != 0 ) && (pd.year % 4 == 0 ) || (pd.year % 400 == 0 ))
    
{
     
int  dday1[ 12 ] = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;
     
if (pd.month == 2 )   
   
{
        pd.day
= 29 ;
     
if (pd.date > pd.day)
         exit(
0 );
       cout
<< " 该月的天数为: " << pd.day << endl;   
     }

     
else
     
{
    pd.day
= dday1[pd.month - 1 ];
    
if (pd.date > dday1[pd.month - 1 ])
     exit(
0 );
    cout
<< " 该月的天数为: " << pd.day << endl;
   }

    
for (time = 0 ;time < pd.month - 1 ;time ++ )
     j
+= dday1[time];
  j
= j % 7 ;   
  score
= i + j + k + 2 ;
  
if (score < 7 )
    strcpy(pd.week,dweek[score]); 
  
else
  
{
   
while (score >= 7 )
   
{
    score
-= 7 ;
   }

  strcpy(pd.week,dweek[score]); 
  }
    
  cout
<< " 该天的星期数为: " << pd.week << endl;   
  }
 
    
else
    
{
    
int     dday2[ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;
  
if (pd.month == 2 )   
   
{
        pd.day
= 28 ;
     
if (pd.date > pd.day)
         exit(
0 );
       cout
<< " 该月的天数为: " << pd.day << endl;   
     }

     
else
     
{
    pd.day
= dday2[pd.month - 1 ];
    
if (pd.date > dday2[pd.month - 1 ])
     exit(
0 );
    cout
<< " 该月的天数为: " << pd.day << endl;
   }

    
for (time = 0 ;time < pd.month - 1 ;time ++ )
     j
+= dday2[time];
  j
= j % 7 ;   
  score
= i + j + k + 2 ;
  
if (score < 7 )
    strcpy(pd.week,dweek[score]); 
  
else
  
{
   
while (score >= 7 )
   
{
    score
-= 7 ;
   }

  strcpy(pd.week,dweek[score]); 
  }
    
  cout
<< " 该天的星期数为: " << pd.week << endl;
  }

return  pd;
}


void  main()
{
    Date obymd;
    obymd.year
= 1980 ;
    obymd.month
= 1 ;
    obymd.date
= 1 ;
    strcpy(obymd.week,dweek[
2 ]);
    cout
<< " 请输入年,月,日: " << "   " ;
    cin
>> obymd.year >> obymd.month >> obymd.date;
    showday(obymd);
}

你可能感兴趣的:(万年历)