| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1062 人关注过本帖
标题:万年历的c程序
只看楼主 加入收藏
zerokingf1
Rank: 2
等 级:论坛游民
帖 子:31
专家分:23
注 册:2011-7-30
收藏
得分:0 
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int first_day = 0;
int add_value;
char *week[7] = {"周日", "周一","周二","周三","周四","周五","周六"};


//计算输入年份是否为闰年
bool isLeap(int year)
{
    return ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0));
}

//计算输入年份第一天是星期几
void Init(int year)
{
    add_value = isLeap(year);
    day[1] = 28 + add_value;//如果输入年为闰年,2月份的天数为29
    first_day = (year + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7;//计算出输入年第一天为周几
}

//计算到输入年份的当前月的距离
int get_distance(int month)
{
    int distance[12] = {0, 31, 59 + add_value, 90 + add_value, 120 + add_value,
                        151 + add_value, 181 + add_value, 212 + add_value,
                        243 + add_value, 273 + add_value, 304 + add_value, 334 + add_value};
    return distance[month - 1];
}

void display(int year)
{
    Init(year);
    printf("         %d年年历       \n",year);
    printf("  周日  周一  周二  周三  周四  周五  周六  \n");
    for(int i = 1; i <= 12; i++)
    {   
        printf("                %d月份        \n", i);
        for(int j = 0; j < first_day; j++)
        {
            printf("      ");
        }
        for(int start = 1; start <= day[i - 1]; start++)
        {
            printf("%6d", start);
            first_day++;
            if(first_day == 7)
            {
                first_day = 0;
                printf("\n");
            }
        }
        printf("\n");
    }
}
2011-09-07 14:28
小光vs小熊
Rank: 2
等 级:论坛游民
帖 子:18
专家分:10
注 册:2011-9-5
收藏
得分:0 
谢谢啦,我感觉再加上阴历太难了,我就写简单点的
2011-09-07 15:52
lin471306489
Rank: 4
等 级:业余侠客
帖 子:136
专家分:247
注 册:2011-8-16
收藏
得分:0 
我豪无头绪了
2011-09-07 16:59
袁光辉
Rank: 1
等 级:新手上路
帖 子:3
专家分:1
注 册:2011-8-31
收藏
得分:0 
#include<stdio.h>
#include<windi.h>
long int f(int year,int month)
{/*f(年,月)=年-1,如月<3;否则,f(年,月)=年*/
   if(month<3) return year-1;
   else return year;
}
long int g(int month)
{/*g(月)=月+13如月<3;否则,g(月)=月+1*/
   if(month<3)  return month+13;
   else return month+1;
}
long int n(int year,int month,int day)
{/*N=1461*f(年,月)/4+153*g(月)/5+日*/
   return 1461L*f(year,month)/4+153L*g(month)/5+day;
}
int w(int year,int month,int day)
{/*w=(N-621049)%7(0<=w<7)*/
   return(n(year,month,day)%7-621049L%7+7);
}
int day[12][6][7];
int day_tbl[][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
main()
{
      int sw,leap,i,j,k,wd,day;
      int year;/*年*/
      int date[i][j][k];
      char title[]="SUN MON TUE WED THU FRI SAT";
      printf("please input the year whose calendar you want to kown:");
      scanf("%s",&year);
      sw=w(year,1,1);
      leap=year%4==0&&year%400==0;
      for(i=0;i<12;i++)
         for(k=0;k<7;k++)
            date[i][j][k]=0;/*日期表置0*/
      for(i=0;i<12;i++)/*一年十二个月*/
        for(wd=0,day-1;day<=day_tbl[leap][i];day++)
        {/*将第i+1月的日期填入日期表*/
           date[i][wd][sw]=day;
           sw=++sw%7;/*每星期7天,以0~6计数*/
           if(sw==0) wd++;/*日期表每7天一行*/
        }
     printf("\n |===============================the calendar of year %d==================================|\n",year);
     for (i=0;i<6;i++)
     {/*先测算第i+1月和第i+7月的最大星期数*/
     for(wd=0,k=0;k<7;k++)
        wd+=date[i][5][k]+date[i+6][5][k];
        wd=wd?6:5;
        printf("%2d    %s       %2d       %s  |\n|",i+1,title,i+7,title);
        for (j=0;j<wd;j++)
        {
            printf("   ");/*输入三个 空字符*/
            /*左栏是第i+1月,右栏是第i+7月*/
               for(k=0;k<7;k++)
               if(date[i][j][k])
                 printf("4%d",date[i][j][k]);
                 else printf("   ");
                 printf("   ");
                 for(k=0;k<7;k++)
                   if(date[i+6][j][k]);
                   else printf("   ");
                   printf(" |\n|");
                   }
      }
  puts("========================================================");
  puts("\n press any key to quit...");
  system("pause");
  get();
}   
                 
                  
              
2011-11-27 13:30
快速回复:万年历的c程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013195 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved