C语言程序设计.docx
《C语言程序设计.docx》由会员分享,可在线阅读,更多相关《C语言程序设计.docx(12页珍藏版)》请在冰点文库上搜索。
C语言程序设计
C
语
言
课
程
设
计
报
告
班级:
姓名:
学号:
专业:
目录
一、题目与要求.................................1
1.问题提出
2.本系统涉及的知识点
3.功能要求
二、功能设计.....................................1
1.算法设计
三、程序设计总结.................................5
1、程序内容
2、程序情况
四、C程序设计总结...............................11
五、结束语........................................11
六、参考文献......................................11
一、题目与要求
1.问题的提出
本组计划编万年历系统,主要用来查询当前日期。
2.系统涉及的知识点
例如结构、数组、循环、函数、分支、指针。
3.功能要求
(1).查询当天日期
(2).判断当年是否为闰年
(3).显示出当前月的每一天及对应的星期几
(4).输出结果
二、功能设计
1.算法设计
图1万年历系统主菜单
2、要输入任一年显示出该年的所有月份日期,应该先设计具体的输出格式,如下:
Thecalendaroftheyear2008.
January
SunMonTueWedThuFriSat
12345
6789101112
13141516171819
20212223242526
2728293031
3、判断是不是闰年(year%4==0&&year%100!
=0)||(year%400==0)满足条件就是平年,把一年中的所有月份分为四类(28,29,30,31)
4、求出一年中的第一天是周几([year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c]%7)
5、用指针数组存放十二个月的英文和星期的英文缩写。
6、用for循环输出各月份的日历。
三、程序代码设计
1、程序内容:
#include
#include
char*month_str[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char*week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
intIsLeapYear(intyear)/*findouttheyearisleapyearornot*/
{
if((year%4==0&&year%100!
=0)||(year%400==0))
return1;
else
return0;
}
intmonth_day(intyear,intmonth)
{
intmon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(IsLeapYear(year)&&month==2)
return29;
else
return(mon_day[month-1]);
}
intDaySearch(intyear,intmonth,intday)/*searchwhatdaythisdayis*/
{
intc=0;
floats;
intm;
for(m=1;mc=c+month_day(year,m);
c=c+day;
s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c;
return((int)s%7);
}
intPrintAllYear(intyear)/*printtheallyear*/
{
inttemp;
inti,j;
printf("\n\n%dCalander\n",year);
for(i=1;i<=12;i++)
{
printf("\n\n%s(%d)\n",month_str[i-1],i);
printf("0123456\n");
printf("SMTWTFS\n\n");
temp=DaySearch(year,i,1);
for(j=1;j<=month_day(year,i)+temp;j++)
{
if(j-temp<=0)
printf("");
elseif(j-temp<10)
printf("%d",j-temp);
else
printf("%d",j-temp);
if(j%7==0)
printf("\n");
}
}
return0;
}
intmain()
{
intoption,da;
charch;
intyear,month,day;
printf("\n\nWelcometousetheWanNianLisystem!
\n");
while
(1)
{
printf("\nPleaseselecttheserviceyouneed:
\n");
printf("\n1Searchwhatdaythedayis");
printf("\n2Searchwhethertheyearisleapyearornot");
printf("\n3Printthecalanderofthewholeyear");
printf("\n4Exit\n");
scanf("%d",&option);
switch(option)
{
case1:
while
(1)
{
printf("\nPleaseinputtheyear,monthandday(XXXX,XX,XX):
");
scanf("%d,%d,%d,%c",&year,&month,&day);
da=DaySearch(year,month,day);
printf("\n%d-%d-%dis%s,doyouwanttocontinue?
(Y/N)",year,month,day,week[da]);
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case2:
while
(1)
{
printf("\nPleaseinputtheyearwhichneedssearched?
(XXXX)");
scanf("%d",&year);
if(IsLeapYear(year))
printf("\n%disLeapyear,doyouwanttocontinue?
(Y/N)",year);
else
printf("\n%disnotLeapyear,doyouwanttocontinue(Y/N)?
",year);
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case3:
while
(1)
{
printf("\nPleaseinputtheyearwhichneedsprinted(XXXX)");
scanf("%d",&year);
PrintAllYear(year);
printf("\nDoyouwanttocontinuetoprint(Y/N)?
");
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case4:
fflush(stdin);
printf("Areyousure?
(Y/N)");
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
exit(0);
break;
default:
printf("\nError:
Sorry,thereisnothisservicenow!
\n");
break;
}
}
return0;
}
2、程序情况
(1)、首先利用char*指针类型将月份与星期几以数组的形式列举出来。
(2)、输入具体年份,利用公式(year%4==0&&year%100!
=0)||(year%400==0)来判断是否为闰年。
利用if循环分析,若为闰年则二月为29天。
(3)、查询当前天是哪一天,并利用for循环和公式(s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c)计算出一年的天数,从而求出该年具体有多少个星期
(4)、利用if循环语句以万年历的形式输出具体年月日,并可在当前页以系统当前日期的月份为准显示当前月的每一天(显示出对应的星期几).当系统日期变到下一月时,系统自动翻页到下一月.
3.运行结果
四、C程序设计总结
在程序分析的过程中,虽然这一年来也是很认真地学习C语言程序设计,但到具体进行程序设计时,却是出现了很多问题。
我和我同组人员李亚兰共同设计了本次编写亿万年历系统的程序。
我分析编写的是前半部分,开始时感觉任务很艰巨,不知道从何下手,通过网上查询资料和向同学老师请教,都得到了解决。
例如,刚开始时我不知道如何以指针数组的形式列举月份和星期几,通过查询C语言程序设计这本书知道了具体操作。
在写到公式(year%4==0&&year%100!
=0)||(year%400==0)
时,把其中的“==”写成了一个“=”,这是严重性出现的问题,通过这次的具体动手,以后不会出现此类问题。
For循环运用的也不是很熟练。
还有就是,程序虽然编好了,但在运行的时候,却出现了错误,通过查资料找补缺、运行调试,和组成员讨论、向老师请教找到了错误,最终程序运行成功。
五、结束语
通过学校此次开展的这两周的C语言程序设计课程,增强了我们的学习的积极性,提高了我们的动手能力。
之前我们对C语言这门课程已经学了一学期,对其有了一定的了解,但是也只是停留在了了解的范围,对里面的一些东西还是很陌生,更多的在运用的时候还是感到很棘手。
上机实验是学习程序语言必不可少的实践环节,特别是C语言灵活、简洁,但却不好掌握,更需要编程的实践来学习它。
同时上机和具体到程序编程,给了我们共同合作、学习的机会。
(1)参考文献
【1】贾宗璞,许合利,C语言程序设计,北京:
人民邮电出版社,2010.9
【2】刘振安,C程序设计课程设计,北京:
机械工业出版社,2004.9