闲来无事,看到梁老师博客里有C课程设计的题,于是就选了一道试着做了下。
(今年课程设计给出的题目中最简单的一道)
代码里面有比较详细的注释,实现起来也很简单,额,,用到的技术有:单链表(存储节日设置),格式化读文件,time.h库应用。。大概就这些吧。
哦对了,为了做这个,特地去研究了下天干地支,,之前我可是连生肖都背不全呢(-_-! 丢人。。)
一个festivals.txt文件,放到程序(.exe)同文件夹下(比如debug文件夹下),格式如下:
- 10/1 国庆节
- 1/1 元旦节
- 5/1 劳动节
- 6/1 儿童节
- 7/1 建军节
- 8/1 建党节
- 9/10 教师节
- 11/11 光棍节
共有calendar.h和main.c两个文件,源代码是:
- /********************************************************************
- created: 2010/11/27
- created: 27:11:2010 20:40
- filename: F:/C&C++/Calendar/calendar.h
- file path: F:/C&C++/Calendar
- file base: calendar
- file ext: h
- author: ijse
- purpose: the calendar supports
- *********************************************************************/
- #include <time.h>
- #include <string.h>
- #include <stdlib.h>
- struct stFestival{ /*定义结构体类型*/
- int fMonth;
- int fDay;
- char *intro[3];
- struct stFestival *next;
- } *ftls;
- /*
- * 从外部文件中读取节日信息
- */
- void readFestivalOpts() {
- FILE *fs;
- struct stFestival *head,*p;
- if((fs=fopen("festivals.txt","r"))==NULL) {
- printf("cannot open file");
- exit(0);
- }
- p = (struct stFestival*)malloc(sizeof(struct stFestival));
- //将读取出的数据存放到链表中
- while(fscanf(fs,"%d/%d %s/n",&(p->fMonth),&(p->fDay),p->intro)!=EOF) {
- p->next=NULL;
- if(ftls==NULL) {
- ftls=p;
- head=ftls;
- } else {
- ftls->next=p;
- ftls=ftls->next;
- }
- p = (struct stFestival*)malloc(sizeof(struct stFestival));
- }
- ftls=head;
- fclose(fs);
- }
- /*
- * 查询节日
- */
- char *getFestival(int iMonth,int iDay) {
- static char* rslt[3];
- while(ftls!=NULL) {
- if(ftls->fMonth==iMonth&&ftls->fDay==iDay) {
- if(strlen(rslt)==0)
- strcpy(rslt,ftls->intro);
- else
- strcat(strcat(rslt,","),ftls->intro);
- }
- ftls=ftls->next;
- }
- return rslt;
- }
- /*
- * 中英文数字转换函数
- */
- char *albToZHnum(int i) {
- static char albZHNums[13][6]={"七","一","二","三","四","五","六","七","八","九","十","十一","十二"};
- return albZHNums[i];
- }
- /*
- * 判断是否为闰年
- */
- int isLeapYear(int iYear) {
- if((iYear%4==0 && iYear%100!=0) || (iYear%400==0))
- return 1;
- else
- return 0;
- }
- /*
- * 获得生肖
- */
- char *getLunarName(int iYear) {
- static char rslt[3];
- int t;
- static char animal[12][3]={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
- t=(iYear+8)%12;
- strcpy(rslt,animal[t]);
- return rslt;
- }
- /*
- * 获得天干地支
- */
- char *getZhYearName(int iYear) {
- static char rslt[6]; /*用来表示干支的字符变量*/
- int a,b;
- static char tg[10][3]={"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
- static char dz[12][3]={"子","丑","寅","卯","辰","巳","午","未","申","酉","戍","亥"};
- a=(iYear-1804)%10; /*以1804年为参考年份,1804年是甲子年*/
- b=(iYear-1804)%12;
- strcpy(rslt,tg[a]);
- strcat(rslt,dz[b]);
- return rslt;
- }
- /*
- * 得到一年中某月的天数
- */
- int getMonthDays(int imonth,int isLpYear) {
- int monthDay;
- switch(imonth) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- monthDay =31;
- break;
- case 2:
- if(isLpYear=1)
- monthDay =29;
- else
- monthDay =28;
- break;
- default:
- monthDay =30;
- break;
- }
- return monthDay;
- }
- /*
- * 得到星期数
- */
- int getDayOfWeek(int iDay,int iMonth, int iYear) {
- struct tm *ptr;
- time_t lt;
- lt=time(NULL);
- ptr=localtime(<);
- ptr->tm_year=iYear-1900;
- ptr->tm_mon=iMonth-1;
- ptr->tm_mday=iDay;
- lt=mktime(ptr);
- ptr=localtime(<);
- return ptr->tm_wday;
- }
- /*
- * 打印一个月份
- */
- int printMonth(int iYear,int iMonth) {
- int islpYear,monthDay,i,x,sum,a;
- islpYear=isLeapYear(iYear); //是否闰年
- monthDay=getMonthDays(iMonth,islpYear);//得到当月天数
- sum=0;
- //下面的循环是计算从公元一年到该年的上一年的所有天数
- //公元一年第一天为星期一
- for(i=1;i<iYear;i++) {
- if(isLeapYear(i)) {
- sum=sum+366;
- } else
- sum=sum+365;
- }
- //接下来要计算从该年1月到用户输入月份的上一个月的天数
- for(x=1; x<iMonth; x++) {
- sum += getMonthDays(x,islpYear);
- }
- a=sum % 7;
- printf("/n===%s月============================================",albToZHnum(iMonth));
- printf("/nSun/tMon/tTue/tWed/tThu/tFri/tSat/n");
- for(x=1;x<=monthDay+a;x++) {
- if(x<=a)
- printf("/t");
- else
- printf("%d/t",x-a);
- if(x%7==0)
- printf("/n");
- }
- printf("/n");
- return 0;
- }
- /********************************************************************
- created: 2010/11/27
- created: 27:11:2010 20:32
- filename: F:/C&C++/Calendar/main.c
- file path: F:/C&C++/Calendar
- file base: main
- file ext: c
- author: ijse
- WebSite: http://www.ijser.cn
- statement: 1. warnings是因为中文的原因,没有做转码处理,不影响运行。
- 2. 节日设定存放在外部文件中,与程序同文件夹.
- 3. 节日设定文件格式为:月/日 节日名字
- 4. 节日可重复设定,输出时用,隔开。
- *********************************************************************/
- #include <stdio.h>
- #include "calendar.h"
- int main() {
- int iYear,iMonth,iDay;
- int i;
- //Section 1
- printf("Input the Year:");
- scanf("%d",&iYear);
- printf("The calendar of the Year %d.",iYear);
- printf("/n %s%s年",getZhYearName(iYear),getLunarName(iYear));
- for(i=1;i<=12;++i)
- printMonth(iYear,i);
- //Section 2
- printf("Input the Month:");
- scanf("%d",&iMonth);
- printf("The calendar of the Year %d and the month %d",iYear,iMonth);
- printMonth(iYear,iMonth);
- //Section 3
- readFestivalOpts();
- printf("Input the Day:");
- scanf("%d",&iDay);
- printf("%d年%d月%d号是星期%s %s/n",iYear,iMonth,iDay,albToZHnum(getDayOfWeek(iDay,iMonth,iYear)),getFestival(iMonth,iDay));
- return 0;
- }
有不懂的可以一起来讨论,希望学弟学妹们认真学习C,认真做课程设计哈~~!
PS: 运行中截图:
。。。。。。。