Programming 版 (精华区)
发信人: kaola (我好困呀!), 信区: Programming
标 题: 一个用C编制的年历
发信站: 哈工大紫丁香 (2001年09月09日12:25:12 星期天), 站内信件
这是一个小日历程序,你可以查询公元1年以后任何一年的日历。你也可以在进入程序后
,选择"d"选项,直接察看这一年的日历,也可以选择"1"--"c"来察看一月份到十二月份
的日历。选择"0"项,则退出程序。
----------------------------------------------------------------------------
----
正文:
一个用C编制的年历
这是一个小日历程序,你可以查询公元1年以后任何一年的日历。你也可以在进入程序后
,选择"d"选项,直接察看这一年的日历,也可以选择"1"--"c"来察看一月份到十二月份
的日历。选择"0"项,则退出程序。
print()函数负责输出一年的日历信息。
detail_print()函数负责输出被查询年指定月份的日历信息。
judg_leapyear()函数用于判断被查询年是不是润年。
cal_days()函数用于计算被查询年的1月1日距离时间判断点(2000年1月1日)的天数。
menu()函数用于生成菜单。
以后想知道某年某月某日是礼拜几,就用我这个程序吧!!!就是有点土,因为它是在
Dos运行的。不过,编写这个程序本来就是用来练练手的,相信对于初学C语言的朋友还
是有点帮助的。本准备将农历和每一个节日、节气查询的功能也加进去,这样的话程序
肯定会很冗长,反而,不容易解读。程序写的一点尔也不拗口。
/*
*2000年1月1日 星期六
*0--星期一
*1--星期二
*2--星期三
*3--星期四
*4--星期五
*5--星期六
*6--星期天
*/
#include "stdio.h"
static int nor_year[12]={31,28,31,30,31,30,31,31,30,31,30,31};
static int leapyear[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int ymonth[12][31];
/*
*The function judg_leapyear() is used to judge
*whether the year is leapyear.
*If the year is leapyear,the function returns 1;
*else it returns 0;
*/
int judg_leapyear(year)
int year;
{
if((year%4==0 && year%100!=0) || year%400==0)
return(1);
else
return(0);
}
/*
*The function cal_days() is used to calculate
*the days which is between year/1/1 and 2000/1/1.
*/
long cal_days(year)
int year;
{
int count_i,temp_year,disc_year;
long ret;
ret=0;
temp_year=year;
disc_year=year-2000;
year--;
if(disc_year==0)
return(0);
else
if(disc_year>0)
{
while(disc_year)
{
if(judg_leapyear(year))
{
ret=ret+366;
}
else
{
ret=ret+365;
}
disc_year--;
year--;
}
return(ret);
}
else
{
while(disc_year<0)
{
if(judg_leapyear(temp_year))
{
ret=ret+366;
}
else
{
ret=ret+365;
}
disc_year++;
temp_year++;
}
return(ret);
}
}
/*
*Print the calendar of specify year/month.
*/
detail_print(year,month)
int year,month;
{
int i;
int pmonth,pday;
if(judg_leapyear(year) && month==2)
{
for(i=0;i<29;i++)
{
pday=i+1;
switch(ymonth[2][i])
{
case 0:
printf(" %d日 星期一 ;",pday);
break;
case 1:
printf(" %d日 星期二 ;",pday);
break;
case 2:
printf(" %d日 星期三 ;",pday);
break;
case 3:
printf(" %d日 星期四 ;",pday);
break;
case 4:
printf(" %d日 星期五 ;",pday);
break;
case 5:
printf(" %d日 星期六 ;",pday);
break;
case 6:
printf(" %d日 星期天 ;",pday);
break;
}
}
}
else
{
for(i=0;i<nor_year[month];i++)
{
pday=i+1;
switch(ymonth[month][i])
{
case 0:
printf(" %d日 星期一 ;",pday);
break;
case 1:
printf(" %d日 星期二 ;",pday);
break;
case 2:
printf(" %d日 星期三 ;",pday);
break;
case 3:
printf(" %d日 星期四 ;",pday);
break;
case 4:
printf(" %d日 星期五 ;",pday);
break;
case 5:
printf(" %d日 星期六 ;",pday);
break;
case 6:
printf(" %d日 星期天 ;",pday);
break;
}
}
}
printf("\n Press any key ,return!!!");
getch();
}
/*
*Printf the content of array ymonth[12][31];
*/
void print(year)
int year;
{
int count_i,count_j;
int print_ctr;
int pmonth,pday;
print_ctr=0;
pmonth=pday=0;
if(judg_leapyear(year))
{
for(count_i=0;count_i<12;count_i++)
{
for(count_j=0;count_j<leapyear[count_i];count_j++)
{
pmonth=count_i+1;
pday=count_j+1;
if(print_ctr<2)
{
switch(ymonth[count_i][count_j])
{
case 0:
printf(" %d年%d月%d日 星期一 ;",year,pmonth,pday);
break;
case 1:
printf(" %d年%d月%d日 星期二 ;",year,pmonth,pday);
break;
case 2:
printf(" %d年%d月%d日 星期三 ;",year,pmonth,pday);
break;
case 3:
printf(" %d年%d月%d日 星期四 ;",year,pmonth,pday);
break;
case 4:
printf(" %d年%d月%d日 星期五 ;",year,pmonth,pday);
break;
case 5:
printf(" %d年%d月%d日 星期六 ;",year,pmonth,pday);
break;
case 6:
printf(" %d年%d月%d日 星期天 ;",year,pmonth,pday);
break;
}
print_ctr++;
}
else
{
switch(ymonth[count_i][count_j])
{
case 0:
printf(" %d年%d月%d日 星期一 ;\n",year,pmonth,pday);
break;
case 1:
printf(" %d年%d月%d日 星期二 ;\n",year,pmonth,pday);
break;
case 2:
printf(" %d年%d月%d日 星期三 ;\n",year,pmonth,pday);
break;
case 3:
printf(" %d年%d月%d日 星期四 ;\n",year,pmonth,pday);
break;
case 4:
printf(" %d年%d月%d日 星期五 ;\n",year,pmonth,pday);
break;
case 5:
printf(" %d年%d月%d日 星期六 ;\n",year,pmonth,pday);
break;
case 6:
printf(" %d年%d月%d日 星期天 ;\n",year,pmonth,pday);
break;
}
print_ctr=0;
}
}
if(count_i==11)
{
printf("The display finished!\n");
break;
}
else
getchar();
printf("\n");
}
}
else
{
for(count_i=0;count_i<12;count_i++)
{
for(count_j=0;count_j<nor_year[count_i];count_j++)
{
pmonth=count_i+1;
pday=count_j+1;
if(print_ctr<2)
{
switch(ymonth[count_i][count_j])
{
case 0:
printf(" %d年%d月%d日 星期一 ;",year,pmonth,pday);
break;
case 1:
printf(" %d年%d月%d日 星期二 ;",year,pmonth,pday);
break;
case 2:
printf(" %d年%d月%d日 星期三 ;",year,pmonth,pday);
break;
case 3:
printf(" %d年%d月%d日 星期四 ;",year,pmonth,pday);
break;
case 4:
printf(" %d年%d月%d日 星期五 ;",year,pmonth,pday);
break;
case 5:
printf(" %d年%d月%d日 星期六 ;",year,pmonth,pday);
break;
case 6:
printf(" %d年%d月%d日 星期天 ;",year,pmonth,pday);
break;
}
print_ctr++;
}
else
{
switch(ymonth[count_i][count_j])
{
case 0:
printf(" %d年%d月%d日 星期一 ;\n",year,pmonth,pday);
break;
case 1:
printf(" %d年%d月%d日 星期二 ;\n",year,pmonth,pday);
break;
case 2:
printf(" %d年%d月%d日 星期三 ;\n",year,pmonth,pday);
break;
case 3:
printf(" %d年%d月%d日 星期四 ;\n",year,pmonth,pday);
break;
case 4:
printf(" %d年%d月%d日 星期五 ;\n",year,pmonth,pday);
break;
case 5:
printf(" %d年%d月%d日 星期六 ;\n",year,pmonth,pday);
break;
case 6:
printf(" %d年%d月%d日 星期天 ;\n",year,pmonth,pday);
break;
}
print_ctr=0;
}
}
if(count_i==11)
{
printf("The display finished!\n");
break;
}
else
getchar();
printf("\n");
}
}
}
/*
*Create the main menu.
*/
void menu(disp_year)
int disp_year;
{
char ch;
int i;
do
{
system("cls");
printf("%d年日历.请你选择所要进行的操作:",disp_year);
printf("\n\t1. See about calendar of January.");
printf("\n\t2. See about calendar of February.");
printf("\n\t3. See about calendar of Mar.");
printf("\n\t4. See about calendar of April.");
printf("\n\t5. See about calendar of May.");
printf("\n\t6. See about calendar of June.");
printf("\n\t7. See about calendar of July.");
printf("\n\t8. See about calendar of August.");
printf("\n\t9. See about calendar of September.");
printf("\n\ta. See about calendar of October.");
printf("\n\tb. See about calendar of December.");
printf("\n\tc. See about calendar of .");
printf("\n\td. I want to see about all informations!");
printf("\n\t0. Quit!!! \n\n");
printf("\t Please select:");
ch=getch();
switch(ch)
{
case '1':
printf("1\n");
printf("%d年1月\n",disp_year);
detail_print(disp_year,1);
break;
case '2':
printf("2\n");
printf("%d年2月\n",disp_year);
detail_print(disp_year,2);
break;
case '3':
printf("3\n");
printf("%d年3月\n",disp_year);
detail_print(disp_year,3);
break;
case '4':
printf("4\n");
printf("%d年4月\n",disp_year);
detail_print(disp_year,4);
break;
case '5':
printf("5\n");
printf("%d年5月\n",disp_year);
detail_print(disp_year,5);
break;
case '6':
printf("6\n");
printf("%d年6月\n",disp_year);
detail_print(disp_year,6);
break;
case '7':
printf("7\n");
printf("%d年7月\n",disp_year);
detail_print(disp_year,7);
break;
case '8':
printf("8\n");
printf("%d年8月\n",disp_year);
detail_print(disp_year,8);
break;
case '9':
printf("9\n");
printf("%d年9月\n",disp_year);
detail_print(disp_year,9);
break;
case 'a':
printf("10\n");
printf("%d年10月\n",disp_year);
detail_print(disp_year,10);
break;
case 'b':
printf("11\n");
printf("%d年11月\n",disp_year);
detail_print(disp_year,11);
break;
case 'c':
printf("12\n");
printf("%d年12月\n",disp_year);
detail_print(disp_year,12);
break;
case 'd':
printf("%c\n",ch);
print(disp_year);
break;
case '0':
system("cls");
exit(1);
default:
printf("\n\t ****WRONG!!!****\n");
for(i=0;i<600;i++);
{;}
}
}
while(1);
}
main()
{
int year,week;
long disc_days;
int count_i,count_j;
char string_year[100];
for(count_i=0;count_i<12;count_i++)
for(count_j=0;count_j<31;count_j++)
ymonth[count_i][count_j]=0;
printf("Please input the YEAR:\n");
while(1)
{
gets(string_year);
year=atoi(string_year);
if(atoi(string_year)==0)
printf("Please reset the YEAR,it must be valid!\n");
else
break;
}
printf("The year is %d \n",year);
disc_days=cal_days(year);
printf("The days between %d/1/1 and 2000/1/1 is %d days.\n",year,disc_days);
if(year-2000==0)
{
week=5;
for(count_i=0;count_i<12;count_i++)
for(count_j=0;count_j<leapyear[count_i];count_j++)
{
if(week>6)
{
week=0;
ymonth[count_i][count_j]=week;
week++;
}
else
{
ymonth[count_i][count_j]=week;
week++;
}
}
}
else
if(year-2000>0)
{
week=(disc_days%7+5)%7;
if(judg_leapyear(year))
{
for(count_i=0;count_i<12;count_i++)
for(count_j=0;count_j<leapyear[count_i];count_j++)
{
if(week>6)
{
week=0;
ymonth[count_i][count_j]=week;
week++;
}
else
{
ymonth[count_i][count_j]=week;
week++;
}
}
}
else
{
for(count_i=0;count_i<12;count_i++)
for(count_j=0;count_j<nor_year[count_i];count_j++)
{
if(week>6)
{
week=0;
ymonth[count_i][count_j]=week;
week++;
}
else
{
ymonth[count_i][count_j]=week;
week++;
}
}
}
}
else
{
week=(disc_days%7+3)%7;
if(judg_leapyear(year))
{
for(count_i=0;count_i<12;count_i++)
for(count_j=0;count_j<leapyear[count_i];count_j++)
{
if(week>6)
{
week=0;
ymonth[count_i][count_j]=week;
week++;
}
else
{
ymonth[count_i][count_j]=week;
week++;
}
}
}
else
{
for(count_i=0;count_i<12;count_i++)
for(count_j=0;count_j<nor_year[count_i];count_j++)
{
if(week>6)
{
week=0;
ymonth[count_i][count_j]=week;
week++;
}
else
{
ymonth[count_i][count_j]=week;
week++;
}
}
}
}
menu(year);
}
--
我怕水,我怕谁?
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.247.126]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.729毫秒