C语言课程设计日历题目

 闲来无事,看到梁老师博客里有C课程设计的题,于是就选了一道试着做了下。

(今年课程设计给出的题目中最简单的一道)

代码里面有比较详细的注释,实现起来也很简单,额,,用到的技术有:单链表(存储节日设置),格式化读文件,time.h库应用。。大概就这些吧。

哦对了,为了做这个,特地去研究了下天干地支,,之前我可是连生肖都背不全呢(-_-! 丢人。。)

一个festivals.txt文件,放到程序(.exe)同文件夹下(比如debug文件夹下),格式如下:

 

  1. 10/1 国庆节  
  2. 1/1 元旦节  
  3. 5/1 劳动节  
  4. 6/1 儿童节  
  5. 7/1 建军节  
  6. 8/1 建党节  
  7. 9/10 教师节  
  8. 11/11 光棍节  
 

 

共有calendar.h和main.c两个文件,源代码是:

 

  1. /******************************************************************** 
  2.     created:    2010/11/27 
  3.     created:    27:11:2010   20:40 
  4.     filename:   F:/C&C++/Calendar/calendar.h 
  5.     file path:  F:/C&C++/Calendar 
  6.     file base:  calendar 
  7.     file ext:   h 
  8.     author:     ijse 
  9.      
  10.     purpose:    the calendar supports 
  11. *********************************************************************/  
  12. #include <time.h>  
  13. #include <string.h>  
  14. #include <stdlib.h>  
  15. struct stFestival{ /*定义结构体类型*/  
  16.     int fMonth;  
  17.     int fDay;  
  18.     char *intro[3];  
  19.     struct stFestival *next;  
  20. } *ftls;  
  21. /* 
  22.  * 从外部文件中读取节日信息 
  23.  */  
  24. void readFestivalOpts() {  
  25.     FILE *fs;  
  26.     struct stFestival *head,*p;  
  27.     if((fs=fopen("festivals.txt","r"))==NULL) {  
  28.         printf("cannot open file");  
  29.         exit(0);  
  30.     }  
  31.     p = (struct stFestival*)malloc(sizeof(struct stFestival));  
  32.     //将读取出的数据存放到链表中  
  33.     while(fscanf(fs,"%d/%d %s/n",&(p->fMonth),&(p->fDay),p->intro)!=EOF) {  
  34.         p->next=NULL;  
  35.         if(ftls==NULL) {  
  36.             ftls=p;  
  37.             head=ftls;  
  38.         } else {  
  39.             ftls->next=p;  
  40.             ftls=ftls->next;  
  41.         }  
  42.         p = (struct stFestival*)malloc(sizeof(struct stFestival));  
  43.     }  
  44.     ftls=head;  
  45.     fclose(fs);  
  46. }  
  47. /* 
  48.  * 查询节日 
  49.  */  
  50. char *getFestival(int iMonth,int iDay) {  
  51.     static char* rslt[3];  
  52.     while(ftls!=NULL) {  
  53.         if(ftls->fMonth==iMonth&&ftls->fDay==iDay) {  
  54.             if(strlen(rslt)==0)  
  55.                 strcpy(rslt,ftls->intro);  
  56.             else   
  57.                 strcat(strcat(rslt,","),ftls->intro);  
  58.         }  
  59.         ftls=ftls->next;  
  60.     }  
  61.     return rslt;  
  62. }  
  63. /* 
  64.  * 中英文数字转换函数 
  65.  */  
  66. char *albToZHnum(int i) {  
  67.     static char albZHNums[13][6]={"七","一","二","三","四","五","六","七","八","九","十","十一","十二"};  
  68.     return albZHNums[i];  
  69. }  
  70. /* 
  71.  * 判断是否为闰年 
  72.  */  
  73. int isLeapYear(int iYear) {  
  74.     if((iYear%4==0 && iYear%100!=0) || (iYear%400==0))  
  75.         return 1;  
  76.     else  
  77.         return 0;  
  78. }  
  79. /* 
  80.  * 获得生肖 
  81.  */  
  82. char *getLunarName(int iYear) {  
  83.     static char rslt[3];  
  84.     int t;  
  85.     static char animal[12][3]={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};  
  86.     t=(iYear+8)%12;  
  87.     strcpy(rslt,animal[t]);  
  88.     return rslt;  
  89. }  
  90. /* 
  91.  * 获得天干地支 
  92.  */  
  93. char *getZhYearName(int iYear) {  
  94.     static char rslt[6]; /*用来表示干支的字符变量*/   
  95.     int a,b;  
  96.     static char tg[10][3]={"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};   
  97.     static char dz[12][3]={"子","丑","寅","卯","辰","巳","午","未","申","酉","戍","亥"};   
  98.     a=(iYear-1804)%10; /*以1804年为参考年份,1804年是甲子年*/   
  99.     b=(iYear-1804)%12;   
  100.     strcpy(rslt,tg[a]);   
  101.     strcat(rslt,dz[b]);   
  102.     return rslt;  
  103. }  
  104. /* 
  105.  * 得到一年中某月的天数 
  106.  */  
  107. int getMonthDays(int imonth,int isLpYear) {  
  108.     int monthDay;  
  109.     switch(imonth) {   
  110.             case 1:  
  111.             case 3:  
  112.             case 5:   
  113.             case 7:   
  114.             case 8:    
  115.             case 10:    
  116.             case 12:  
  117.                 monthDay =31;  
  118.                 break;    
  119.             case 2:  
  120.                 if(isLpYear=1)  
  121.                     monthDay =29;  
  122.                 else  
  123.                     monthDay =28;   
  124.                 break;  
  125.             default:  
  126.                 monthDay =30;  
  127.                 break;   
  128.         }  
  129.     return monthDay;  
  130. }  
  131. /* 
  132.  * 得到星期数 
  133.  */  
  134. int getDayOfWeek(int iDay,int iMonth, int iYear) {  
  135.     struct tm *ptr;  
  136.     time_t lt;  
  137.     lt=time(NULL);  
  138.     ptr=localtime(<);  
  139.     ptr->tm_year=iYear-1900;  
  140.     ptr->tm_mon=iMonth-1;  
  141.     ptr->tm_mday=iDay;  
  142.     lt=mktime(ptr);  
  143.     ptr=localtime(<);  
  144.     return ptr->tm_wday;  
  145. }  
  146. /* 
  147.  * 打印一个月份 
  148.  */  
  149. int printMonth(int iYear,int iMonth) {  
  150.     int islpYear,monthDay,i,x,sum,a;  
  151.     islpYear=isLeapYear(iYear); //是否闰年  
  152.     monthDay=getMonthDays(iMonth,islpYear);//得到当月天数  
  153.     sum=0;  
  154.     //下面的循环是计算从公元一年到该年的上一年的所有天数   
  155.     //公元一年第一天为星期一  
  156.     for(i=1;i<iYear;i++) {   
  157.         if(isLeapYear(i)) {  
  158.             sum=sum+366;  
  159.         } else   
  160.             sum=sum+365;   
  161.     }   
  162.     //接下来要计算从该年1月到用户输入月份的上一个月的天数   
  163.     for(x=1; x<iMonth; x++) {   
  164.         sum += getMonthDays(x,islpYear);  
  165.     }  
  166.     a=sum % 7;  
  167.     printf("/n===%s月============================================",albToZHnum(iMonth));  
  168.     printf("/nSun/tMon/tTue/tWed/tThu/tFri/tSat/n");      
  169.     for(x=1;x<=monthDay+a;x++) {  
  170.         if(x<=a)   
  171.             printf("/t");   
  172.         else   
  173.             printf("%d/t",x-a);   
  174.         if(x%7==0)   
  175.             printf("/n");   
  176.     }  
  177.     printf("/n");  
  178.     return 0;  
  179. }  
 

 

 

  1.  /******************************************************************** 
  2.     created:    2010/11/27 
  3.     created:    27:11:2010   20:32 
  4.     filename:   F:/C&C++/Calendar/main.c 
  5.     file path:  F:/C&C++/Calendar 
  6.     file base:  main 
  7.     file ext:   c 
  8.     author:     ijse 
  9.     WebSite:    http://www.ijser.cn 
  10.      
  11.     statement:  1. warnings是因为中文的原因,没有做转码处理,不影响运行。 
  12.                 2. 节日设定存放在外部文件中,与程序同文件夹. 
  13.                 3. 节日设定文件格式为:月/日 节日名字 
  14.                 4. 节日可重复设定,输出时用,隔开。 
  15. *********************************************************************/  
  16. #include <stdio.h>  
  17. #include "calendar.h"  
  18. int main() {  
  19.     int iYear,iMonth,iDay;  
  20.     int i;  
  21.     //Section 1  
  22.     printf("Input the Year:");  
  23.     scanf("%d",&iYear);  
  24.     printf("The calendar of the Year %d.",iYear);  
  25.     printf("/n %s%s年",getZhYearName(iYear),getLunarName(iYear));  
  26.       
  27.     for(i=1;i<=12;++i)   
  28.         printMonth(iYear,i);  
  29.     //Section 2  
  30.     printf("Input the Month:");  
  31.     scanf("%d",&iMonth);  
  32.     printf("The calendar of the Year %d and the month %d",iYear,iMonth);  
  33.     printMonth(iYear,iMonth);  
  34.     //Section 3  
  35.     readFestivalOpts();  
  36.     printf("Input the Day:");  
  37.     scanf("%d",&iDay);  
  38.     printf("%d年%d月%d号是星期%s %s/n",iYear,iMonth,iDay,albToZHnum(getDayOfWeek(iDay,iMonth,iYear)),getFestival(iMonth,iDay));  
  39.       
  40.     return 0;  
  41. }  
 

 

有不懂的可以一起来讨论,希望学弟学妹们认真学习C,认真做课程设计哈~~!

PS: 运行中截图:

。。。。。。。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值