RenHao

Calendar

Jul 26th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5.  
  6.  
  7. int firstday = 1 ;
  8. int year=0;
  9. int Febuary=0;
  10.  
  11. char *weekdays[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  12. char *months[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",};
  13.  
  14.  
  15. void prtspace(int first)
  16. {
  17.     int i ;
  18.    
  19.     for(i=0;i<first%7;i++)
  20.         printf("    ");
  21. }
  22.  
  23. int firstcal(int y)
  24. {
  25.     int i;
  26.     y = y-1900;
  27.     int flag=0;
  28.     for(i=1;i<=y;i++)
  29.     {
  30.         flag += (!(i%4) && i%100 || !(i%400))? 366 : 365 ;
  31.         //printf("=%d  %d=\n",i,flag%365);
  32.     }
  33.     firstday = (firstday+flag)%7;
  34.     return firstday;
  35.    
  36. }
  37.  
  38. void leapjudge(int y)
  39. {
  40.     if(!(y%400) || (y%100) && !(y%4))  
  41.     {
  42.         Febuary = 29 ;
  43.         printf("This year %d is leap year\n",y);
  44.     }
  45.     else
  46.     {
  47.         Febuary = 28;
  48.         printf("This year %d is not leap year\n",y);
  49.     }
  50.    
  51. }
  52.  
  53. int main(int argc, char *argv[])
  54. {
  55.     int i,j;
  56.     int k;
  57.    
  58.     int Days[12] = {31,Febuary,31,30,31,30,31,31,30,31,30,31};
  59.     printf("A calender of which year :");
  60.     scanf("%d",&year);
  61.  
  62.     //閏年二月日數
  63.     leapjudge(year);
  64.    
  65.     //該年一月的空白日數
  66.     firstcal(year);
  67.    
  68.    
  69.     for(i=0;i<12;i++)
  70.     {
  71.         printf("%d\n",Days[i]);
  72.         printf("\t%4s\n",*(months+ i));
  73.         for(k=0;k<7;k++)
  74.         {
  75.             printf("%4s",*(weekdays+ k));
  76.            
  77.         }
  78.         printf("\n");
  79.        
  80.         prtspace(firstday);
  81.        
  82.         for(j=1;j<=Days[i];j++)
  83.         {
  84.             printf(" %3d",j);
  85.             firstday =  (++firstday)%7;
  86.             if(!firstday)
  87.             printf("\n");
  88.            
  89.         }
  90.         printf("\n");
  91.        
  92.     }
  93.    
  94.    
  95.     system("PAUSE");
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment