急!~~~~ 用C或者C++編寫 打印萬年歷 的程序 要求如下
基本功能已經實現:
/*****************************************************************
*Author?:wacs5 *DATE:20101110(YYYMMDD) *Email:wacs5@126.com *Function?:用C編寫、打印萬年歷 *Targe:/question/195084859.html *Compiler?:TC2.0、Gcc通過(如果是VC++的話,去掉#include?<conio.h>) **另外getch()函數可以換成system("pause")語句. *****************************************************************/#include?<time.h>
#include?<math.h>
#include?<stdio.h>
#include?<conio.h>
int?DaysInMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int?DaysInYear[13]={0,31,59,90,120,151,181,212,243,273,304,334,365};
char?MonthInfo[13][4]={"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
char?WeekInfoEng[8][4]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char?WeekInfoChs[8][3]={"天","壹","二","三","四","五","六"};
int?main()
{
int?year,month,day; int?flag=1;?/*是否輸出到文件,1為輸出,0為不輸出*/ long?date1; int?IsLoopYear(int?year); int?CheckDay(int?year,int?month,int?day); long?Date2Long(int?year,int?month,int?day); int?Week(long?day); int?PrintNow(int?flag); int?MonthCalendar(int?year,int?month,int?flag); int?YearCalendar(int?year,int?flag); int?PrintNumMask(int?year,char?ch,FILE?*fp); PrintNow(flag);?/*輸出當月月歷並輸出時間,flag=1代表輸出到文件*/? printf("year="); scanf("%d",&year); YearCalendar(year,flag);?/*輸出2010年年歷,flag=1代表輸出到文件*/ getch();}
/*判斷閏年*/
int?IsLoopYear(int?year)
{
return?year%400==0?||?(year%100?&&?year%4==0);}
/*檢查日期的合法性*/
int?CheckDay(int?year,int?month,int?day)
{
int?IsLoopYear(int?year); /*年月日大於零,月份為1-12,日期為1至當月最大的天數,註意二月份的閏月*/ if?(year>=0?&&?month>0?&&?month<=12?&&?day>0?&&?\ day<=(?DaysInMonth[month]+(month==2&&IsLoopYear(year))?)?) return?1; else return?0;}
/*返回year年month月day日到0000年1月0日(抽象的時間)的天數*/
long?Date2Long(int?year,int?month,int?day)
{
long?days; days=year*365l+year/4-year/100+year/400?+?(year>0);/*year年1月0日經歷的天數,0年是閏年*/ days+=DaysInYear[month-1]+?(month>2&&IsLoopYear(year))?+?day;?/*加上月份以及天數*/ return?days;}
/*計算星期*/
int?Week(long?days)
{
return?(days+5)%7;?}
/*輸出現在時刻,並輸出當月日歷*/int?PrintNow(int?flag)?
{
time_t?now; struct?tm?*tm_1;? int?Week(long?day); int?CheckDay(int?year,int?month,int?day); long?Date2Long(int?year,int?month,int?day); int?MonthCalendar(int?year,int?month,int?flag); time(&now); tm_1=localtime(&now); MonthCalendar(tm_1->tm_year+1900,tm_1->tm_mon+1,flag); printf("今天是:%4d年%02d月%02d日,星期%s\n",tm_1->tm_year+1900,tm_1->tm_mon+1,tm_1->tm_mday,WeekInfoChs[tm_1->tm_wday]); printf("當前時間是:%02d時%02d分%02d秒\n",tm_1->tm_hour,tm_1->tm_min,tm_1->tm_sec); return?1;}
/*輸出月歷*/
int?MonthCalendar(int?year,int?month,int?flag)?/*flag=1時,寫文件*/
{
int?i,k,day,week; long?days; char?filename[80]; int?Week(long?day); int?CheckDay(int?year,int?month,int?day); long?Date2Long(int?year,int?month,int?day); FILE?*fp; if?(flag) { sprintf(filename,"%04d_%02d.txt",year,month); fp=fopen(filename,"w"); if?(fp==NULL) {printf("File?Open?Error?in?Function:MonthCalendar\nPress?any?key?to?exit:");
getch();
exit(-1);
}? } day=1; if?(!CheckDay(year,month,day)) { printf("Format:%d-%d-1(YYYY-MM-DD)?is?invaild?in?Function:MonthCalendar\n",year,month); return?0; } days=Date2Long(year,month,day); week=Week(days); k=(4*7-9)/2; for?(i=0;i<k;i++) filename[i]='?'; filename[k]='\0'; printf("MonthCalendar:\n%s%d--%s\n",filename,year,MonthInfo[month]); if?(flag) fprintf(fp,"%s%d--%s\n",filename,year,MonthInfo[month]); for?(i=0;i<7;i++) { printf("%s?",WeekInfoEng[i]); if?(flag) fprintf(fp,"%s?",WeekInfoEng[i]); } printf("\n");? if?(flag) fprintf(fp,"\n"); for?(i=0;i<week;i++) { printf(""); if?(flag) fprintf(fp,""); } for?(i=day;i<=DaysInMonth[month]+(month==2)*(IsLoopYear(year));i++)?/*閏年的2月份,要加壹*/ { printf("%3d?",i); if?(flag) fprintf(fp,"%3d?",i); if?((i+week)%7==0) {printf("\n");
if?(flag);
fprintf(fp,"\n");
} } printf("\n"); if?(flag) { fprintf(fp,"\n"); fclose(fp); } return?1;}
/*輸出年歷*/
int?YearCalendar(int?year,int?flag)
{
int?month_col=2;?/*每行幾個月歷*/ int?space_col=4;?/*兩個月歷的空格數*/ int?month_row=(int)ceil(12.0/month_col); int?line,end_day[13],keepgoing[13]={1}; int?i,j,k,month,day[13],week[13]; long?days[13]; char?space[3][80]; int?Week(long?day); int?CheckDay(int?year,int?month,int?day); long?Date2Long(int?year,int?month,int?day); int?PrintNumMask(int?year,char?ch,FILE?*fp); FILE?*fp; if?(flag) { char?filename[80]; sprintf(filename,"%04d.txt",year); fp=fopen(filename,"w"); if?(fp==NULL) {printf("File?Open?Error?in?Function:YearCalendar\nPress?any?key?to?exit:");
getch();
exit(-1);
}? } for?(month=1;month<=12;month++) { day[month]=1; if?(!CheckDay(year,month,day[month])) {printf("Format:%d-%d-1(YYYY-MM-DD)?is?invaild?in?Function:YearCalendar\n",year,month);
return?0;
} days[month]=Date2Long(year,month,day[month]); week[month]=Week(days[month]); keepgoing[month]=1; } k=(7*4-3)/2; for?(i=0;i<k;i++)?/*月份標識前空格*/ space[0][i]='?'; space[0][i]='\0';? for?(i=0;i<7*4-3-k;i++)?/*月份標識後空格*/ space[1][i]='?'; space[1][i]='\0'; for?(i=0;i<space_col;i++)?/*兩列之間的空格*/ space[2][i]='?'; space[2][i]='\0'; PrintNumMask(year,'*',stdout); PrintNumMask(year,'*',fp); month=1; for?(i=0;i<month_row;i++) { for?(j=0;j<month_col;j++)?/*輸出月份的縮寫*/ {printf("%s%s%s",space[0],MonthInfo[month+j],space[1]);?/*前導空格,月份縮寫,後導空格*/
if?(flag)
fprintf(fp,"%s%s%s",space[0],MonthInfo[month+j],space[1]);
if?(j<month_col-1)?/*月份列與列之間的空格*/
{
printf("%s",space[2]);
if?(flag)
fprintf(fp,"%s",space[2]);
}
} printf("\n");?/*換行*/ if?(flag) fprintf(fp,"\n");? for?(j=0;j<month_col;j++)? {for?(k=0;k<7;k++)/*星期的縮寫*/
{
printf("%s?",WeekInfoEng[k]);
if?(flag)
fprintf(fp,"%s?",WeekInfoEng[k]);
}
if?(j<month_col-1)?/*月份列與列之間的空格*/
{
printf("%s",space[2]);
if?(flag)
fprintf(fp,"%s",space[2]);
}
} printf("\n");?/*換行*/ if?(flag) fprintf(fp,"\n");? for?(j=0;j<month_col;j++)?/*橫線*/ {for?(k=0;k<7;k++)/*橫線*/
{
printf("____",WeekInfoEng[k]);
if?(flag)
fprintf(fp,"____",WeekInfoEng[k]);
}
if?(j<month_col-1)?/*月份列與列之間的空格*/
{
printf("%s",space[2]);
if?(flag)
fprintf(fp,"%s",space[2]);
}
} printf("\n");?/*換行*/ if?(flag) fprintf(fp,"\n");? for?(j=0;j<month_col;j++)/*第壹行*/ {for?(k=0;k<week[month+j];k++)?/*1號之前的放空*/
{
printf("");
if?(flag)
fprintf(fp,"");
}
for?(k=1;k<=7;k++)?
{
printf("%3d?",k);
if?(flag)
fprintf(fp,"%3d?",k);
if?((k+week[month+j])%7==0)
{
end_day[month+j]=k+1; printf("%s",space[2]); if?(flag) fprintf(fp,"%s",space[2]); break;}
}
} printf("\n");?/*換行*/ if?(flag) fprintf(fp,"\n");? for?(line=0;line<5;line++) {for?(j=0;j<month_col;j++)
{
if?(keepgoing[month+j]==0)?/*補壹行空格行*/
for?(k=0;k<7;k++) { printf(""); if?(flag) fprintf(fp,""); }else
{
for?(k=end_day[month+j];k<=DaysInMonth[month+j]+(month==2)*(IsLoopYear(year));k++) { printf("%3d?",k); if?(flag) fprintf(fp,"%3d?",k); if?((k+week[month+j])%7==0) {end_day[month+j]=k+1;
break;
} } if?(k>=DaysInMonth[month+j]+(month==2)*(IsLoopYear(year))) keepgoing[month+j]=0;? if?(k>DaysInMonth[month+j]+(month==2)*(IsLoopYear(year)))?/*補空格*/? { for?(;k<100;k++) {printf("");
if?(flag)
fprintf(fp,"");
if?((k+week[month+j])%7==0)
break;
} }}
if?(j<month_col-1)?/*月份列與列之間的空格*/
{
printf("%s",space[2]); if?(flag) fprintf(fp,"%s",space[2]);}
}
printf("\n");?/*換行*/
if?(flag)
fprintf(fp,"\n");?
/*每行中各個月份是否都已經輸出完畢*/
k=0;
for?(j=0;j<month_col;j++)
k+=keepgoing[month+j];
if?(k==0)
break;
} month?+=?month_col; printf("\n");?/*換行*/ if?(flag) fprintf(fp,"\n");? } if?(flag) fclose(fp); return?1;}
/*數字模輸出*/
int?PrintNumMask(int?year,char?ch,FILE?*fp)
{
/*5*5數字模*/ int?mask[10][5]={?{31,17,17,17,31},{?1,?1,?1,?1,?1},\ {31,1?,31,16,31},{31,?1,31,?1,31},\ {17,17,31,?1,?1},{31,16,31,?1,31},\ {31,16,31,17,31},{31,?1,?1,?1,?1},\ {31,17,31,17,31},{31,17,31,?1,31}}; int?digit[16]={0}; char?space='?'; int?i,j,k,n; n=0; while(year) { digit[n++]=year%10; year/=10; } for?(i=0;i<5;i++)?/*行*/ { for?(k=0;k<n;k++)?/*字的第i行*/ {for?(j=0;j<5;j++)?/*列*/
{
if?(?mask[digit[n-1-k]][i]?&?(1<<(4-j))?)
fprintf(fp,"%c",ch);
else
fprintf(fp,"%c",space);
}
fprintf(fp,"%c",space);
} fprintf(fp,"\n"); } return?1;}
輸出結果:
當月月歷以及2010年年歷