飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 5038|回复: 1

[C/C++] 万年历

[复制链接]
  • TA的每日心情
    郁闷
    2017-1-19 11:18
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    发表于 2010-12-3 09:32:40 | 显示全部楼层 |阅读模式
    本帖最后由 komany 于 2010-12-8 12:16 编辑
    1. #include "stdio.h"
    2. void main()
    3. {
    4. int leap_year(int y);
    5. int count_leap(long year);
    6. int monthdays(int month,long year);
    7. void monthprint(long year,int month);
    8. int month=0;
    9. long int year=0;
    10. char choose;
    11. clrscr();
    12. do
    13. {printf ("\n\nPlease input one integer number as 'year'(0~3000):\n");
    14. scanf ("%ld",&year);
    15. if (year<0||year>3000) printf ("ERROR,please input again!");
    16. }
    17. while (year<0||year>3000);
    18. printf ("\n\n");

    19. do
    20. {printf ("please input the month(1~12)\n\n");
    21. scanf ("%d",&month);
    22. if (month<=0||month>12) printf ("please input again!");
    23. }
    24. while (month<=0||month>12);
    25. printf ("\n\n\n");

    26. printf("\t\t\t\t%ld\t%d\n\n",year,month);
    27. printf

    28. ("\n********************************************************************************\n");
    29. printf("\t\t Sun\t Mon\t Tue\t Wen\t Thu\t Fri\t Sat\n");
    30. monthprint(year,month);
    31. printf("\n\n");
    32. }

    33. int leap_year(int y)
    34. { int i;
    35. if (y%4==0&&y%100!=0||y%400==0) i=1;
    36. else i=0;
    37. return i;
    38. }
    39. //多谢N大的教导,下面/**/的里面是N大优化的代码
    40. /*int leap_year(int y)
    41. {
    42.     return (!(y%4)&&y%100 )|| !(y%400) ;
    43. }
    44. */

    45. int count_leap(long year)
    46. {
    47. int i=0,j,min,max;
    48. if(year>2006) {min=2006;max=year;}
    49. else {min=year+1;max=2006;}
    50. for(j=min;j<max;j++)
    51. if(leap_year(j)) i++;
    52. return i;
    53. }

    54. int monthdays(int month,long year)
    55. {
    56. int sum=0,i,j;
    57. if(year>=2006)
    58. { int t[12]={31,0,31,30,31,30,31,31,30,31,30,31};
    59. j=month-1;
    60. if(leap_year(year)) t[1]=29;
    61. else t[1]=28;
    62. for(i=0;i<j;i++)
    63. sum=sum+t[i];
    64. }
    65. else
    66. { int t1[12]={31,30,31,30,31,31,30,31,30,31,0,31};
    67. j=12-month;
    68. if(leap_year(year)) t1[10]=29;
    69. else t1[10]=28;
    70. for(i=0;i<=j;i++)
    71. sum=sum+t1[i];
    72. }

    73. return sum;

    74. }


    75. void monthprint(long year,int month)
    76. {
    77. int t[12]={31,0,31,30,31,30,31,31,30,31,30,31};
    78. int i,y,weekday=0;
    79. long days=0;
    80. if(leap_year(year)) t[1]=29;
    81. else t[1]=28;
    82. y=t[month-1];
    83. if(year>2006)
    84. {
    85. days=(year-2006)*365+count_leap(year)+monthdays(month,year);
    86. weekday=days%7;
    87. }
    88. else if(year<2006)
    89. {days=(2005-year)*365+count_leap(year)+monthdays(month,year);
    90. weekday=7-days%7;
    91. }
    92. else
    93. {days=monthdays(month,year);weekday=days%7;}

    94. for (i=1;i<=weekday+2;i++)
    95. printf ("\t");
    96. for (i=1;i<=y;i++)
    97. {if ((i+weekday-1)%7==0) printf ("\n\n\t\t%3d\t",i);
    98. else printf ("%3d\t",i);};
    99. printf

    100. ("\n********************************************************************************\n\n");
    101. }



    复制代码
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2010-12-4 00:50:01 | 显示全部楼层
    不错 ~

    int leap_year(int y)
    {
        return (!(y%4)&&y%100 )|| !(y%400) ;
    }
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表