当前位置:K88软件开发文章中心编程语言C/C++C/C++01 → 文章内容

c语言简单的万年历实现

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-4 7:35:55

-->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 #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[i]);
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");
}

c语言简单的万年历实现