基本功能已经实现:
/*****************************************************************
*Author :wacs5
*DATE :20101110(YYYMMDD)
*Email :wacs5@126.com
*Function :用C编写、打印万年历
*Targe :http://zhidao.baidu.com/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=0i<ki++)
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=0i<7i++)
{
printf("%s ",WeekInfoEng[i])
if (flag)
fprintf(fp,"%s ",WeekInfoEng[i])
}
printf("\n")
if (flag)
fprintf(fp,"\n")
for (i=0i<weeki++)
{
printf(" ")
if (flag)
fprintf(fp," ")
}
for (i=dayi<=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=1month<=12month++)
{
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=0i<ki++) /*月份标识前空格*/
space[0][i]=' '
space[0][i]='\0'
for (i=0i<7*4-3-ki++) /*月份标识后空格*/
space[1][i]=' '
space[1][i]='\0'
for (i=0i<space_coli++) /*两列之间的空格*/
space[2][i]=' '
space[2][i]='\0'
PrintNumMask(year,'*',stdout)
PrintNumMask(year,'*',fp)
month=1
for (i=0i<month_rowi++)
{
for (j=0j<month_colj++) /*输出月份的缩写*/
{
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=0j<month_colj++)
{
for (k=0k<7k++) /*星期的缩写*/
{
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=0j<month_colj++) /*横线*/
{
for (k=0k<7k++) /*横线*/
{
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=0j<month_colj++)/*第一行*/
{
for (k=0k<week[month+j]k++) /*1号之前的放空*/
{
printf(" ")
if (flag)
fprintf(fp," ")
}
for (k=1k<=7k++)
{
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=0line<5line++)
{
for (j=0j<month_colj++)
{
if (keepgoing[month+j]==0) /*补一行空格行*/
for (k=0k<7k++)
{
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<100k++)
{
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=0j<month_colj++)
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=0i<5i++) /*行*/
{
for (k=0k<nk++) /*字的第i行*/
{
for (j=0j<5j++) /*列*/
{
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年年历
网上找的部分带我,我又修改了一下,现在基本满足lz的要求。手头没有Wintc用vc测的,不过没用到什么vc特有的东西,应该可以的。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void DayOfWeek()
{
int rtnDay
int y,m,d
char ch
while (1)
{
printf("请输入年月日(xxxx,xx,xx): ")
scanf("%d,%d,%d", &y, &m, &d)
fflush(stdin)
rtnDay = (3*y-(7*(y+(m+9)/12))/4+(23*m)/9+d+17-((y+(m<3?-1:0))/100+1)*3/4) % 7
printf("%04d年%02d月%02d日 是 星期%d\n", y, m, d, rtnDay?rtnDay:7)
printf("继续查询(y/n)")
ch = getchar()
if(ch != 'y' &&ch != 'Y')
break
}
}
void GetMonth(int D, int md, int n)
{
int N ,m,d,y,c,Wd
int i,maxday,i1
if(md>=13||md<=0)
printf("HAVE WRONG!")
else
{
for(i1=0md++,i1++)
{
if(md==0)
{
md=12
D-=1
}
else
if(md==13)
{
md=1
D+=1
}
m=md
printf("\n============%d,%2d============\n",D,md)
if(0<m&&m<=12)
{
switch(m)
{
case 1:maxday=31break
case 2:if(D%4==0&&D%100!=0||D%400==0)
maxday=29
else
maxday=28
break
case 3:maxday=31break
case 4:maxday=30break
case 5:maxday=31break
case 6:maxday=30break
case 7:maxday=31break
case 8:maxday=31break
case 9:maxday=30break
case 10:maxday=31break
case 11:maxday=30break
case 12:maxday=31break
default:printf("HAVE WRONG")
}
if(0<m&&m<=2)
{
N=D-1
m=m+10
}
else
{
N=D
m=m-2
}
c=N/100
y=N%100
Wd=((1+(13*m-1)/5+y+y/4+c/4-2*c)%7+7)%7
printf(" SUN MOU TUE WED TUR FRI SAT\n")
for(i=0i<Wdi++)
printf("%4c",' ')
for(d=1d<=maxdayd++)
{
printf("%4d",d)
Wd=(Wd+1)%7
if(Wd==0)
printf("\n")
}
}
else
printf("HAVE WRONG!")
if(i1==n)
break
}
}
}
void MonthOfYear()
{
char ch
int y, m
while (1)
{
printf("请输入年月(xxxx,xx): ")
scanf("%d,%d", &y, &m)
fflush(stdin)
GetMonth(y, m, 0)
printf("\n继续查询(y/n)")
ch = getchar()
if(ch != 'y' &&ch != 'Y')
break
}
}
void GetYear()
{
char ch
int y
while (1)
{
printf("请输入年: ")
scanf("%d", &y)
fflush(stdin)
GetMonth(y, 1, 11)
printf("\n继续查询(y/n)")
ch = getchar()
if(ch != 'y' &&ch != 'Y')
break
}
}
int main()
{
char ch
while(1)
{
system("cls")
printf("1.查询星期\n")
printf("2.查询月份\n")
printf("3.显示一年\n")
printf("4.退出程序\n")
ch = getchar()
fflush(stdin)
switch(ch)
{
case '1':
DayOfWeek()
break
case '2':
MonthOfYear()
break
case '3':
GetYear()
break
case '4':
printf("是否退出(y/n)")
ch = getchar()
fflush(stdin)
if(ch == 'y' || ch == 'Y')
exit(0)
break
}
}
}
另外,虚机团上产品团购,超级便宜
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)