C语言如何输出万年历
功能要求:(1)提供菜单方式选择
(2)输入年份、月份、日期,计算得到的是这一天据今天有多少天,星期几;
(3)输入公历的年月日,输出农历年月日。
(4)输入农历节气,输出当年农历的年月日及公历年月日。可以假定只涉及年份是1940年到2040年
希望能按功能输出,谢谢各位了!
[ 本帖最后由 jiangbo90313 于 2010-5-12 11:41 编辑 ]
#include <time.h> #include<stdio.h> void week(time_t year,time_t month); main() { time_t year,month; printf("\nInput year,month:\n"); scanf("%ld,%ld",&year,&month); printf("OK!\n"); week(year,month); } void week(time_t year,time_t month) { struct tm *gm; char *sTime,sMon[4],spaces[31]; time_t days,aday,secs,secs1,feb,leaps; int date,line,indent,i; /*Due to leap year,February has 29 days?*/ feb=28L; if(leap(year)>0) feb=29L; days=0L; aday=86400L;/* One day's seconds.*/ /*Days passed before this month in this year.*/ switch(month) { case 12L:days=days+30L;/* November */ case 11L:days=days+31L; case 10L:days=days+30L; case 9L:days=days+31L; case 8L:days=days+31L; case 7L:days=days+30L; case 6L:days=days+31L; case 5L:days=days+30L; case 4L:days=days+31L; case 3L:days=days+feb; case 2L:days=days+31L; break; default:; } /*Add up leap days since 1970.*/ for(leaps=1970L;leaps<year;leaps++) { if(leap(leaps)>0) { days=days+1L; } } line=0; sMon[0]='\0'; printf("\n\n Weeks of %ld.%ld :\n",month,year); printf(" SUN MON TUE WED THU FRI SAT"); date=1; year=year-1970L; year=year*365L*aday; month=days*aday; secs=year+month; secs1=secs; gm=gmtime(&secs1); sTime=asctime(gm); /*Determine spaces for aligning for the 1st day.*/ indent=0; if(sTime[0]=='M'&&sTime[1]=='o'&&sTime[2]=='n') { indent=5; line=1; } if(sTime[0]=='T'&&sTime[1]=='u'&&sTime[2]=='e') { indent=10; line=2; } if(sTime[0]=='W'&&sTime[1]=='e'&&sTime[2]=='d') { indent=15; line=3; } if(sTime[0]=='T'&&sTime[1]=='h'&&sTime[2]=='u') { indent=20; line=4; } if(sTime[0]=='F'&&sTime[1]=='r'&&sTime[2]=='i') { indent=25; line=5; } if(sTime[0]=='S'&&sTime[1]=='a'&&sTime[2]=='t') { indent=30; line=6; } for(i=0;i<indent;i++) { spaces[i]=' '; } spaces[i]='\0'; if(indent>0) { printf("\n%s",spaces); } sMon[0]=sTime[4]; sMon[1]=sTime[5]; sMon[2]=sTime[6]; /* Just show this month. */ while(sMon[0]==sTime[4]&&sMon[1]==sTime[5]&&sMon[2]==sTime[6]) { if(line%7==0) { printf("\n"); } printf(" %3d",date); line++; sMon[0]=sTime[4]; sMon[1]=sTime[5]; sMon[2]=sTime[6]; date++; secs=secs+aday; secs1=secs; gm=gmtime(&secs1); sTime=asctime(gm); } } int leap(long year) { if(year%4==0&&year%100!=0||year%400==0) { return 1; } return -1; }代码很长,农历节气方面楼主自己想办法....输入格式为:year,month