wangnianli.cpp
上传用户:clotteng
上传日期:2022-07-23
资源大小:1k
文件大小:2k
- #include<stdio.h>
- void disp(int);
- int leap_year(int);
- int date(int,int,int);
- void main()
- {
- int year=0;
- printf("Input year(XXXX):");
- scanf("%d",&year);
- disp(year);
- }
- int leap_year(int year)//判断是否是闰年
- {
- if((year%4==0&&year%100!=0)||(year%400==0))
- return(1);
- else
- return(0);
- }
- int date(int year,int month,int day)//计算某年某月某日是星期几
- {
- int leap,days,sum;//days计算某个日期起,这一年的天数
- days=0;
- leap=leap_year(year);
- switch(month-1)
- {
- case 11:days+=30;
- case 10:days+=31;
- case 9:days+=30;
- case 8:days+=31;
- case 7:days+=31;
- case 6:days+=30;
- case 5:days+=31;
- case 4:days+=30;
- case 3:days+=31;
- case 2:if(leap)
- days+=29;
- else
- days+=28;
- case 1:days+=31;
- default:days+=day;
- }
- sum=year-1+(year-1)/4-(year-1)/100+(year-1)/400+days;//这是工式,计算sum这天是星期几,用总的天数模除7
- return(sum%7);
- }
- void disp(int year)//打印输出算法
- {
- int i,j,k,leap,days,count,n;
- leap=leap_year(year);
- days=date(year,1,1);//计算某年的第一天是星期几
- printf("%d年n",year);
- for(i=1;i<=12;i++)
- {
- printf("%3d月份n",i);
- printf("n");
- printf("t");
- printf("%-8s%-8s%-8s%-8s%-8s%-8s%-8sn","Sun","Mon","Tues","Wed","Thurs","Fri","Sat");//格式化输出字符串
- n=days;
- printf("t");
- for(j=1;j<=8*days;j++)
- printf(" ");//输出空格,为了让数字与日期对齐
- switch(i)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:count=31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:count=30;
- break;
- case 2:
- if(leap)
- count=29;
- else
- count=28;
- break;
- }
- for(k=1;k<=count;k++)//控制界面对齐
- {
- printf("%-8d",k);//从每个月的1号开始输出
- n++;
- if(n==7)
- {
- printf("n");
- printf("t");
- n=0;
- }
- }
- printf("nn");
- days=n;
- }
- }
-