用C语言编写一个年历
黑龙江八一农垦大学理学院
《C程序设计》课程设计报告
2011-2012学年第二学期
设计题目:用C语言设计一个年历系统
指导教师:魏老师
完成时间:2012年6月13日至2012年6月20日
第一组报告成员
成绩评定:分任课教师签字: 2012年6月20日
目录
1、程序设计与分析 ................................................................................ - 2 -
1、功能分析与任务分配 (2)
2、流程图 (2)
3、数据结构 (4)
2、详细设计............................................................................................. - 5 -
3、参考文献............................................................................................. - 7 -
4、附录..................................................................................................... - 8 -
5、总结..................................................................................................... - 9 -
1、程序设计与分析
1、功能分析与任务分配
(一)功能分析:
1、要输入任一年显示出该年的所有月份日期,应该先设计具体的输出格式,如下:
the calendar of the year2012.
January
Sun Mon Tue Wed Thu Fri Sat
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
=============================================
2、判断是不是闰年(year%4!=0||year%100==0&&year%400!=0)满足条件就是平年,把一年中的所有月份分为四类(28,29,30,31)。
3、求出一年中的第一天是周几(year +(year-1)/4-(year-1)/100+(year-1)/400)%7。
4、有指针数组存放十二个月的英文和星期的英文缩写。
5、用FOR循环输出各月份的日历
(二)任务分配:
1、Student1:程序综合整理
2、Student2:判断闰年程序编写
3、Student3:月份循环程序编写
4、Student4:主函数程序编写
5、Student5:天数循环程序编写
2、流程图
1) 整个函数流程图:
2) 判断任一年是否为闰年,及该年每月天数(N-S流程图):
3、数据结构
1)整型:
int judge(int year,int month)
int year,i,j,a,n,m,k;
2)字符型:
char ** p;
3)字符型数组:
week[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
month[]={"January","February","March","April","May","June","July","August","Sep tember","October","November","December"};
4)指针数组:
char * week[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char*month[]={"January","February","March","April","May","June","July","August" ,"September","October","November","December"};
2、详细设计
想要编写一个万年历程序,按其要求输出相应的功能。
1)首先,需要对任一输入年份进行判断,判断其是平年还是闰年:
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) return(1);
else if(month==2)
{
if(year%4!=0||year%100==0&&year%400!=0)
return(2);
else return(3);
}
else return(4);
即当上式返回值为2的时候,为平年;否则,为闰年。
然后,判断这一年的月份,将其分为4种类型,即:天数为31天的月份、天数为28天的月份、天数为29天的月份,和天数为30天的月份:
对应的函数返回值,分别为:n=1,n=2,n=3,n=4。
将以上几部分,综合整理为一个判断函数,其程序为:
int judge(int year,int month) //定义judge函数
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month== 12) //天数为31天的月份
return(1);
else if(month==2)
{
if(year%4!=0||year%100==0&&year%400!=0) //判断闰年
return(2);
else return(3);
}
else return(4);
}
2)接下来,定义主函数。在这一部分,有一个地方非常关键,那就是对任一年第一天是星期几的判断:
a=(year+(year-1)/4-(year-1)/100+(year-1)/400)%7;
即,计算后a值为0、1、2、3、4、5、6,分别对应周日、周一、周二、周三、
周四、周五、周六。
3)然后就是对任一年,月份、星期,和天数的循环:
for(i=0;i<12;i++) //月份循环
{
n=judge(year,i+1); //判断情况,为下程序做准备
p=month+i; //指向指针变量的指针,月份输出
printf("%s\n",*p);
printf("\n");
for(j=0;j<7;j++) //星期循环
{
p=week+j;
printf("%6s",*p);
}
printf("\n");