鱼C论坛

 找回密码
 立即注册
查看: 2608|回复: 0

[技术交流] c语言实现万年历代码

[复制链接]
发表于 2011-11-16 15:39:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

#include <stdio.h>
main()
{
void print_head(int x,int y);       /*打印头文件*/
void print_month(int x,int y);      /*打印月历*/
int days_of_month(int x,int y);     /*计算指定年月的天数*/
int leap(int x,int y);              /*计算指定年月1号是星期几*/
int i,days,year,month,firstday;
char choose;
    do
       {printf ("\n\nplease input the year(0000~9999):\n\n");
        scanf ("%d",&year);
   if (year<0||year>9999) printf ("WANNING:ERROR,please input again!");}
    while (year<0||year>9999);
    printf ("\n\n");
    do
       {printf ("please input the month(0~12)\n\n\n");
        scanf ("%d",&month);
   if (month<=0||month>12) printf ("WANNING:ERROR,please input again!");}
    while (month<=0||month>12);
    printf ("\n\n");
    days=days_of_month(year,month);       /*调用函数*/
    firstday=leap(year,month);
    print_head(year,month);
    print_month(firstday,days);
    choose=getchar();
    printf ("\n\n\n");
    printf("would you like to continue(y/n):\n\n");  
    scanf("%c",&choose);
    if (choose=='y'||choose=='Y') main();
}

days_of_month(int x,int y)
{
int z;
switch (y)
{case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12: z=31;break;
  case 4:
  case 6:
  case 9:
  case 11: z=30;break;
  case 2:
   {if ((x%4==0&&x%100!=0)||(x%400==0)) z=29;
     else z=28;break;
   }
   }
return z;
}

leap(int x,int y)
{
int z,i,moday;
z=(x+(x-1)/4-(x-1)/100+(x-1)/400)%7;
for (i=1;i<y;i++)
  {moday=days_of_month(x,i);       /*钳套调用函数*/
   z=(z+moday)%7;
  }
return z;
}

void print_head(int x,int y)
{
printf ("\n\n********************************************************************************\n\n");
printf ("\t\t\t\t%d       %d\n\n",x,y);
printf ("\t\t\t SUN MON TUE WED THU FRI SAT\n");
printf ("\t\t\t");
}

void print_month(int x,int y)
{
int i;
char space[7]={' '};
for (i=1;i<=x;i++)
    printf ("%4c",space);
for (i=1;i<=y;i++)
    {if ((i+x)%7==1) printf ("\n\t\t\t%4d",i);
      else printf ("%4d",i);}
printf ("\n\n********************************************************************************\n\n");
}


                               
登录/注册后可看大图
该贴已经同步到 zgt2008的微博
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 22:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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