《匠人手记》推荐网上购书渠道:
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
卓越亚马逊网 购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历
载入中...

百宝专栏

载入中...
最新货色

载入中...

粉丝评论

载入中...

载入中...



百宝信息

载入中...

百宝流量

(2006-07-01开始)


匠人手记

 匠人观点: 好记性不如烂笔头  
 黑色幽默:三鹿门——后世畅想

万年历算法全集
程序匠人 发表于 2006-3-31 19:39:00  阅读全文 | 回复(1) | 引用通告 | 编辑

万年历全集

程序可以实现如下三种功能:
求某个日期对应的星期
求某年某月有的天数
输出某年的日历.
例如,打印2006年日历如下:
--------------------------------------------------------------------------
                               2006 年
--------------------------------------------------------------------------
               一   月                                二   月               

周日 周一 周二 周三 周四 周五 周六  周日 周一 周二 周三 周四 周五 周六 
 1    3    5    7    9   11   13                    1    3    5    7  

14   15   16   17   18   19   20     8    9   10   11   12   13   14  

21   22   23   24   25   26   27    15   16   17   18   19   20   21  

28   29   30   31                   22   23   24   25   26   27   28  


               三   月                                四   月               

周日 周一 周二 周三 周四 周五 周六  周日 周一 周二 周三 周四 周五 周六 
                1    3    5    7                                   1  

 8    9   10   11   12   13   14     2    3    4    5    6    7    8  

15   16   17   18   19   20   21     9   10   11   12   13   14   15  

22   23   24   25   26   27   28    16   17   18   19   20   21   22  

29   30   31                        23   24   25   26   27   28   29  

                                    30                                


               五   月                                六   月               

周日 周一 周二 周三 周四 周五 周六  周日 周一 周二 周三 周四 周五 周六 
      1    3    5    7    9   11                         1    3    5  

12   13   14   15   16   17   18     6    7    8    9   10   11   12  

19   20   21   22   23   24   25    13   14   15   16   17   18   19  

26   27   28   29   30   31         20   21   22   23   24   25   26  

                                    27   28   29   30                 


               七   月                                八   月               

周日 周一 周二 周三 周四 周五 周六  周日 周一 周二 周三 周四 周五 周六 
                               1               1    3    5    7    9  

 2    3    4    5    6    7    8    10   11   12   13   14   15   16  

 9   10   11   12   13   14   15    17   18   19   20   21   22   23  

16   17   18   19   20   21   22    24   25   26   27   28   29   30  

23   24   25   26   27   28   29    31                                

30   31                                                               


               九   月                                十   月               

周日 周一 周二 周三 周四 周五 周六  周日 周一 周二 周三 周四 周五 周六 
                          1    3     1    3    5    7    9   11   13  

 4    5    6    7    8    9   10    14   15   16   17   18   19   20  

11   12   13   14   15   16   17    21   22   23   24   25   26   27  

18   19   20   21   22   23   24    28   29   30   31                 

25   26   27   28   29   30                                           


               十一 月                                十二 月               

周日 周一 周二 周三 周四 周五 周六  周日 周一 周二 周三 周四 周五 周六 
                1    3    5    7                              1    3  

 8    9   10   11   12   13   14     4    5    6    7    8    9   10  

15   16   17   18   19   20   21    11   12   13   14   15   16   17  

22   23   24   25   26   27   28    18   19   20   21   22   23   24  

29   30                             25   26   27   28   29   30   31  

原代码如下:

一,非打印版(:即只能输出到屏幕,不可输出到文件)

 /*万年历全集:程序可以实现如下三种功能:
求某个日期对应的星期
求某年某月有的天数
输出某年的日历
*/
/*2006-1-2   梁见斌*/
#i nclude
#i nclude

struct mon
{
 int maxdata;
 int data;
};

void SeekWeekDay(void); //求某个日期对应的星期函数
int WeekDay(int year, int month, int day); //根据输入的日期,返回对应的星期
void HowManyDays(void);//求某年某月有的天数函数
int MonthDays(int year, int month);//根据输入的年号和月份,返回该月的天数   
void PrintWeek(int weekday);//打印星期几
void PrintMonth(int month); //打印月份
void PrintData(); //打印日历

int main(void)
{
 int choice;
 
 while(1)
 {
   puts("------------------------------------------");
   puts("请输入您的选择:");
   puts("输入1求某个日期对应的星期");
   puts("输入2求某年某月有的天数");
   puts("输入3输出某年的日历");
   puts("输入4结束程序");
   puts("------------------------------------------");
  scanf("%d", &choice); fflush(stdin);
  switch(choice)
  {
   case 1: SeekWeekDay();  break;
   case 2: HowManyDays();  break;
   case 3: PrintData();  break;
   case 4: return 0;
   default: puts("输入错误,请重新输入"); break;
  }
  printf("\n");   printf("\n");
 }
  system("pause");
  return 0;
}
void HowManyDays(void) //求某年某月有的天数函数
{
 int year, month, days;
 
 puts("请输入年号和月份:");
 scanf("%d%d", &year, &month); fflush(stdin);
 printf("你的输入为 %d年%d月,", year, month);
 days = MonthDays(year, month); //根据输入的年号和月份,返回该月的天数   
 printf(" %d年%d月有%d天\n", year, month, days);
}
void SeekWeekDay(void) //求某个日期对应的星期函数 
{
 int year, month, day, weekday;
 
 puts("请输入年,月, 日:");
 scanf("%d%d%d", &year, &month, &day); fflush(stdin);
 printf("你的输入为 %d年%d月%d日\n", year, month, day);
 weekday = WeekDay(year, month, day); //根据输入的日期,返回对应的星期
 printf("这天是 ");
 PrintWeek(weekday); //打印星期几
}
void PrintWeek(int weekday)//打印星期几
{
 switch(weekday)
 {
  case 0 : printf("%s","周日 "); break;
  case 1 : printf("%s","周一 "); break;
  case 2 : printf("%s","周二 "); break;
  case 3 : printf("%s","周三 "); break;
  case 4 : printf("%s","周四 "); break;
  case 5 : printf("%s","周五 "); break;
  case 6 : printf("%s","周六 "); break;
 }
}
void PrintMonth(int month)  //打印月份
{
 switch(month)
 {
  case 1 : printf("%s","一   月 "); break;
  case 2 : printf("%s","二   月 "); break;
  case 3 : printf("%s","三   月 "); break;
  case 4 : printf("%s","四   月 "); break;
  case 5 : printf("%s","五   月 "); break;
  case 6 : printf("%s","六   月 "); break;
  case 7 : printf("%s","七   月 "); break;
  case 8 : printf("%s","八   月 "); break;
  case 9 : printf("%s","九   月 "); break;
  case 10: printf("%s","十   月 "); break;
  case 11: printf("%s","十一 月 ");   break;
  case 12: printf("%s","十二 月 ");   break;
 }
}
int WeekDay(int year, int month, int day) //根据输入的日期,返回对应的星期
{
 int i;
 int run=0, ping=0;
 long sum;
 
 for(i=1; i {
  if(i%4==0 && i%100!=0 || i%400==0)
   run++;
  else
   ping++;
 }
 sum = 366*run + 365*ping;
 for(i=1; i  sum += MonthDays(year, i);
 sum += day;   //计算总天数
 return (int)sum%7;
}
int MonthDays(int year, int month)//根据输入的年号和月份,返回该月的天数   
{
 switch(month)
 {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:  return 31;
  case 4:
  case 6:
  case 9:
  case 11:  return 30;
  case 2:   if(year%4==0 && year%100!=0 || year%400==0)
        return 29;
       else
        return 28;
  default: puts("这是一个错误的月份!"); system("pause"); return 0; 
 }
}

void PrintData(void)//打印日历,对输出格式的控制较复杂
{
 struct mon month[13];
 int i, j, k;
 int year, mon, week;
 
 puts("请输入年号:");
 scanf("%d", &year);
 
 for(i=1; i<13; i++) //存储该年每个月的总天数和初始日期
 {
  month[i].data = 1;
  month[i].maxdata = MonthDays(year, i);
 }
 for(i=0; i<6; i++) //总共输出6排
 {
  for(j=1; j<=2; j++) //每排输出2个月
  {
   mon = 2*i + j;
   printf("%15s", " ");
   PrintMonth(mon);   //第一行打印月份
   printf("%15s", " ");
   if(j==1)
    printf("\t");
  }
  printf("\n");   printf("\n");
  for(j=1; j<=2; j++)
  {
   for(k=0; k<7; k++)
   {
     PrintWeek(k);   //第2行打印星期
   }
   printf("\t");
  }
  printf("\n");
  for(j=1; j<=2; j++)
  {
   mon = 2*i + j;
   week = WeekDay(year, mon, 1);//根据输入的日期,返回对应的星期
   printf("%*d   ", week*5+2, month[mon].data++); //控制输出格式,把每月的1日打印在对应星期的下面
   week++;
   while(week < 7) //接着在该行打印该周剩余的日期
   {
    printf("%2d   ", month[mon].data++);
    week++;
   }
   if(j==1)
    printf("\t");
  }
  printf("\n");   printf("\n");//从第4行起打印该月剩余的日期,每7个一行;直至该月日期打印完毕
  while(month[2*i+1].data<=month[2*i+1].maxdata || month[2*i+2].data<=month[2*i+2].maxdata)
  {
   for(j=1; j<=2; j++)
   {
    mon = 2*i + j;
    for(k=0; k<7; k++)
    {  //如果该月日期未打印完,打印该日期
     if(month[mon].data<=month[mon].maxdata)
      printf("%2d   ", month[mon].data++);
     else  //否则输出空格
      printf("     ");
    }
    if(j==1)
    printf("\t");
   }
   printf("\n"); printf("\n");
  }
  printf("\n");
 }
}


二,打印版(:即既能输出到屏幕,又可输出到文件)

/*万年历全集:程序可以实现如下三种功能:
求某个日期对应的星期
求某年某月有的天数
输出某年的日历
*/
/*2006-1-2   梁见斌*/
#i nclude
#i nclude

struct mon
{
 int maxdata;
 int data;
};

void SeekWeekDay(void); //求某个日期对应的星期函数
int WeekDay(int year, int month, int day); //根据输入的日期,返回对应的星期
void HowManyDays(void);//求某年某月有的天数函数
int MonthDays(int year, int month);//根据输入的年号和月份,返回该月的天数   
void PrintWeek(int weekday, FILE *fp);//打印星期几
void PrintMonth(int month, FILE *fp); //打印月份
void PrintData(); //打印日历

int main(void)
{
 int choice;
 
 while(1)
 {
   puts("------------------------------------------");
   puts("请输入您的选择:");
   puts("输入1求某个日期对应的星期");
   puts("输入2求某年某月有的天数");
   puts("输入3输出某年的日历");
   puts("输入4结束程序");
   puts("------------------------------------------");
  scanf("%d", &choice); fflush(stdin);
  switch(choice)
  {
   case 1: SeekWeekDay();  break;
   case 2: HowManyDays();  break;
   case 3: PrintData();  break;
   case 4: return 0;
   default: puts("输入错误,请重新输入"); break;
  }
  printf("\n");   printf("\n");
 }
  system("pause");
  return 0;
}
void HowManyDays(void) //求某年某月有的天数函数
{
 int year, month, days;
 
 puts("请输入年号和月份:");
 scanf("%d%d", &year, &month); fflush(stdin);
 printf("你的输入为 %d年%d月,", year, month);
 days = MonthDays(year, month); //根据输入的年号和月份,返回该月的天数   
 printf(" %d年%d月有%d天\n", year, month, days);
}
void SeekWeekDay(void) //求某个日期对应的星期函数 
{
 FILE *fp;
 int year, month, day, weekday;
 
 if ( (fp=fopen("wnlweek.txt","w+")) == NULL)
  {
    fprintf(stderr,"\nError opening file\n");
   exit(1);
  }
 puts("请输入年,月, 日:");
 scanf("%d%d%d", &year, &month, &day); fflush(stdin);
 printf("你的输入为 %d年%d月%d日\n", year, month, day);
 weekday = WeekDay(year, month, day); //根据输入的日期,返回对应的星期
 printf("这天是 ");
 PrintWeek(weekday, fp); //打印星期几
}
void PrintWeek(int weekday, FILE *fp)//打印星期几
{
 switch(weekday)
 {
  case 0 : fprintf(stdout, "%s","周日 "); fprintf(fp, "%s","周日 "); break;
  case 1 : fprintf(stdout, "%s","周一 "); fprintf(fp, "%s","周一 ");break;
  case 2 : fprintf(stdout, "%s","周二 "); fprintf(fp, "%s","周二 ");break;
  case 3 : fprintf(stdout, "%s","周三 "); fprintf(fp, "%s","周三 ");break;
  case 4 : fprintf(stdout, "%s","周四 "); fprintf(fp, "%s","周四 "); break;
  case 5 : fprintf(stdout, "%s","周五 "); fprintf(fp, "%s","周五 ");break;
  case 6 : fprintf(stdout, "%s","周六 "); fprintf(fp, "%s","周六 ");break;
 }
}
void PrintMonth(int month, FILE *fp)  //打印月份
{
 switch(month)
 {
  case 1 : fprintf(stdout, "%s","一   月 "); fprintf(fp, "%s","一   月 ");break;
  case 2 : fprintf(stdout, "%s","二   月 "); fprintf(fp, "%s","二   月 ");break;
  case 3 : fprintf(stdout, "%s","三   月 "); fprintf(fp, "%s","三   月 ");break;
  case 4 : fprintf(stdout, "%s","四   月 "); fprintf(fp, "%s","四   月 ");break;
  case 5 : fprintf(stdout, "%s","五   月 "); fprintf(fp, "%s","五   月 ");break;
  case 6 : fprintf(stdout, "%s","六   月 "); fprintf(fp, "%s","六   月 ");break;
  case 7 : fprintf(stdout, "%s","七   月 "); fprintf(fp, "%s","七   月 ");break;
  case 8 : fprintf(stdout, "%s","八   月 "); fprintf(fp, "%s","八   月 ");break;
  case 9 : fprintf(stdout, "%s","九   月 "); fprintf(fp, "%s","九   月 ");break;
  case 10: fprintf(stdout, "%s","十   月 "); fprintf(fp, "%s","十   月 ");break;
  case 11: fprintf(stdout, "%s","十一 月 "); fprintf(fp, "%s","十一 月 ");  break;
  case 12: fprintf(stdout, "%s","十二 月 "); fprintf(fp, "%s","十二 月 ");  break;
 }
}
int WeekDay(int year, int month, int day)  //根据输入的日期,返回对应的星期
{
 int i;
 int run=0, ping=0;
 long sum;
 
 for(i=1; i {
  if(i%4==0 && i%100!=0 || i%400==0)
   run++;
  else
   ping++;
 }
 sum = 366*run + 365*ping;
 for(i=1; i  sum += MonthDays(year, i);
 sum += day;   //计算总天数
 return (int)sum%7;
}
int MonthDays(int year, int month)//根据输入的年号和月份,返回该月的天数   
{
 switch(month)
 {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:  return 31;
  case 4:
  case 6:
  case 9:
  case 11:  return 30;
  case 2:   if(year%4==0 && year%100!=0 || year%400==0)
        return 29;
       else
        return 28;
  default: puts("这是一个错误的月份!"); system("pause"); return 0; 
 }
}

void PrintData(void)//打印日历,对输出格式的控制较复杂
{
 FILE *fp;
 struct mon month[13];
 int i, j, k;
 int year, mon, week;
 
 if ( (fp=fopen("wnldata.txt","w+")) == NULL)
  {
    fprintf(stderr,"\nError opening file\n");
   exit(1);
  }
 puts("请输入年号:");
 scanf("%d", &year);
 fprintf(stdout, "--------------------------------------------------------------------------\n");
 fprintf(fp, "--------------------------------------------------------------------------\n");
 fprintf(stdout, "%35d %s\n", year, "年"); fprintf(fp, "%35d %s\n", year,"年");
 fprintf(stdout, "--------------------------------------------------------------------------\n");
 fprintf(fp, "--------------------------------------------------------------------------\n");
 for(i=1; i<13; i++) //存储该年每个月的总天数和初始日期
 {
  month[i].data = 1;
  month[i].maxdata = MonthDays(year, i);
 }
 
 for(i=0; i<6; i++) //总共输出6排
 {
  for(j=1; j<=2; j++)//每排输出2个月
  {
   mon = 2*i + j;
   fprintf(stdout, "%15s", " ");  fprintf(fp, "%15s", " ");
   PrintMonth(mon, fp);  //第一行打印月份
   fprintf(stdout, "%15s", " ");  fprintf(fp, "%15s", " ");
   if(j==1)
   { fprintf(stdout, "\t");     fprintf(fp, "\t"); }
  }
  fprintf(stdout, "\n");  fprintf(stdout, "\n");  fprintf(fp, "\n");  fprintf(fp, "\n");
  for(j=1; j<=2; j++)
  {
   for(k=0; k<7; k++)
   {
     PrintWeek(k, fp);    //第2行打印星期
   }
   fprintf(stdout, "\t");   fprintf(fp, "\t");
  }
  printf("\n");    fprintf(fp, "\n");
  for(j=1; j<=2; j++)
  {
   mon = 2*i + j;
   week = WeekDay(year, mon, 1);   //根据输入的日期,返回对应的星期
   //控制输出格式,把每月的1日打印在对应星期的下面
   fprintf(stdout, "%*d   ", week*5+2, month[mon].data);  fprintf(fp, "%*d   ", week*5+2, month[mon].data);
   month[mon].data++;
   week++;
   while(week < 7) //接着在该行打印该周剩余的日期
   {
    fprintf(stdout, "%2d   ", month[mon].data++);  fprintf(fp, "%2d   ", month[mon].data++);
    week++;
   }
   if(j==1)
   { fprintf(stdout, "\t");   fprintf(fp, "\t"); }
  }
  fprintf(stdout, "\n");  fprintf(stdout, "\n");   fprintf(fp, "\n");   fprintf(fp, "\n");
  //从第4行起打印该月剩余的日期,每7个一行;直至该月日期打印完毕
  while(month[2*i+1].data<=month[2*i+1].maxdata || month[2*i+2].data<=month[2*i+2].maxdata)
  {
   for(j=1; j<=2; j++)
   {
    mon = 2*i + j;
    for(k=0; k<7; k++)
    {
     if(month[mon].data<=month[mon].maxdata)
     {  //如果该月日期未打印完,打印该日期
      fprintf(stdout, "%2d   ", month[mon].data); 
      fprintf(fp, "%2d   ", month[mon].data);
      month[mon].data++;
     }
     else    //否则输出空格
     { fprintf(stdout, "     ");     fprintf(fp,"     "); }
    }
    if(j==1)
    { fprintf(stdout, "\t");   fprintf(fp, "\t");  }
   }
   fprintf(stdout, "\n"); fprintf(stdout, "\n");   fprintf(fp, "\n"); fprintf(fp, "\n");
  }
  fprintf(stdout, "\n");  fprintf(fp, "\n");
 }
 fclose(fp);
}

看《匠人手记》,与匠人同行!北航出版,正在热卖!

回复:万年历算法全集
大海(游客)发表评论于2006-7-5 17:46:00  个人主页 | 引用 | 返回 | 删除 | 回复

大海(游客)

有汇编语言的吗?急用啊

 

看《匠人手记》,与匠人同行!北航出版,正在热卖!

发表评论:
载入中...

芯片专题

器件专题

软件专题

硬件专题

综合专题

项目专题

原创专题

器件检测
LCD LED
按键 触摸键
E2PROM
电池 电机
电阻 电容 电感

指令系统
软件算法
编程规范
滤波算法
串行通讯

PCB设计
I2C PWM
红外遥控
充电技术
中断 ADC 

匠人手记
匠人夜话
网络心路
一周热点串烧
从零开始玩PIC
DIY旋转时钟

广告5号位 [投放]


学习板、开发板、编程器、下载器、仿真器(查看详情……)

广告3号位 [投放]

站内搜索


站外搜索


百度  google
mp3  歌词 
图片  FLASH 
知道  文档
新闻  词典 
地图  mp3 
软件  天网 
雅虎  爱问 
搜狗  讯雷 
网讯  华军 
天空 

21IC器件搜索
百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人手记》21IC书友会
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     

    匠人原创

    往日酷贴

     
     
     

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!