C语言万年历代码

要求:1提供系统操作的主界面
2查询某年某月某日是星期几
3判断某年是否是闰年
4查询某月的最大天数
5打印某年的全年日历或某年某月的月历

#include"stdio.h"
#include"stdlib.h"
main( )
{
int Year,Month; //年、月
int FirstDay_Year,FirstDay_Month; //某年的第一天是星期几,某年某月的第一天是星期几(范围是0~6,其中0代表星期日)
int IsLeapYear; //是否为闰年,0表示不是闰年,1表示是闰年
int i,d,y; //临时变量
char YN; //Yes No,程序是否要继续

int Days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//Days[1~12]存储每个月有多少天,其中二月的天数是可变的(闰年29天,平年28天),这里初始化为28天

printf(" C语言简单万年历\n"); //打印标题

XunHuan: //循环标号(可以通过goto跳转到这里)

printf("请输入年份<0000~9999>: "); //提示输入年份(0~9999)
scanf("%d",&Year); //把输入的年份赋值给变量Year

printf("请输入月份<0~12>: "); //提示输入月份(1~12)
scanf("%d",&Month); //把输入的月份赋值给变量Month

y=Year;
FirstDay_Year=5*(y/4)+(y%4)-(y/100)+(y/400);//蔡勒公式(计算某年的第一天是星期几)
IsLeapYear=(y%4==4&&y%100!=100||y%400==0)?1:0;
//判断是否为闰年
Days[2]=(IsLeapYear==1)?29:28; //闰年二月29天,非闰年二月28天
for(i=1,d=0;i<Month;i++)
d=d+Days[i];
FirstDay_Month=(d+FirstDay_Year)%7; //当月的第一天是星期几(0代表星期日)

printf("\n****************************************************\n");
printf("\t\t公元 %d 年 %2d 月",Year,Month); //打印年月
printf("\n****************************************************\n");

printf(" 星期日 星期一 星期二 星期三 星期四 星期五 星期六\n");
//打印星期表头
for(i=0;i<FirstDay_Month;i++)
printf("%7c",' '); //当某月的第一天不是星期日时打印空格占位

for(d=1;d<=Days[Month];d++) //循环,从每个月的第一天开始打印
{
printf("%7d",d);
if(((d+FirstDay_Month)%7)==0 && d<Days[Month])
printf("\n"); //当输出了星期六而且还未输出所有天数时,换行
}
printf("\n****************************************************\n");
printf("\n");
printf("是否继续(Y/N)?\n");
scanf("%c",&YN);
scanf("%c",&YN);
if(YN=='Y' || YN=='y')
goto XunHuan;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-06-21
#include <stdio.h>
#include <string.h>
#include <conio.h>

int main(int argc, char * argv[]); /* 主入口函数 */
void printmonth(int m); /* 函数说明 */
void printhead(int m); /* 函数说明 */
int daysofmonth(int m); /* 函数说明 */
int isleap(int y); /* 函数说明 */
int firstday(int y); /* 函数说明 */
void month(int m,char e[]); /* 函数说明 */
int year,weekday; /* 全局变量定义 */

int main(int argc, char * argv[])
{
int i;

printf("Input which Year:");
scanf("%d",&year);
weekday=firstday(year); /* weekday 为 year 年份元月一日的星期号 */
printf(" %d year\n",year); /* 17个空格 */
for(i=1;i<=12;i++) /* 输出 year 年份 12 个月的日历信息 */
{
printmonth(i);
printf("\nPress any key to continue...\n");
getch();
}
printf("\n\n");

return 0;
}

void printmonth(int m) /* 输出第 m 月份的日历信息 */
{
int i,days;
printhead(m);
days=daysofmonth(m);
for(i=1;i<=days;i++)
{
printf("%5d",i);
weekday=(weekday+1)%7;
if(weekday==0) /* 打印下一个之前是否换行 */
{
printf("\n "); /* 3 个空格 */
}
}
}

void printhead(int m) /* 输入第 m 月份的头部信息 */
{
int i;
char e[5];
month(m,e);
printf("\n %s Sun Mon Tue Wed Thu Fri Sat\n",e);
printf(" "); /* 3 个空格 */
for(i=0;i<weekday;i++)
printf(" "); /* 5 个空格 */
}

int daysofmonth(int m) /* 返回 year 年 m 月的天数 */
{
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2: if(isleap(year))
return 29;
else
return 28;
}
return 0;
}

int isleap(int y) /* 判断 y 年是否为闰年 */
{
return((y%4==0 && y%100!=0) || y%400==0);
}

int firstday(int y) /* 从公元第 1 天为星期天算出 y 年第 1 天是星期几 */
{
long n=y*365+1;
int i;
for(i=1;i<y;i++) /* 补上所有闰年的天数 */
n+=isleap(i);
return n%7;
}

void month(int m,char e[5]) /* 返回 m 月的名称 */
{
switch(m)
{
case 1:
strcpy(e, "JAN");
break;
case 2:
strcpy(e, "FEB");
break;
case 3:
strcpy(e, "MAR");
break;
case 4:
strcpy(e, "APR");
break;
case 5:
strcpy(e, "MAY");
break;
case 6:
strcpy(e, "JUN");
break;
case 7:
strcpy(e,"JUL");
break;
case 8:
strcpy(e,"AUG");
break;
case 9:
strcpy(e,"SEP");
break;
case 10:
strcpy(e,"OCT");
break;
case 11:
strcpy(e,"NOV");
break;
case 12:
strcpy(e,"DEC");
break;
}
}

参考资料:我大一时候写的,需要源代码文件给我邮件——[email protected]

第2个回答  2010-07-01
#include <stdio.h>
#include <process.h>
int day_s();
int year_s();
int week_s();
void output_month();
void menu();
void output_year();
int year,month;
int main()
{
printf("请输入年月以打印该月日历!\n");
printf("年:");
scanf("%d",&year);
printf("月:");
scanf("%d",&month);
system("cls");
printf("\t\t公元%d年\n",year);
output_month();
menu();
return 0;
}

int day_s()
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;break;
case 4:
case 6:
case 9:
case 11:return 30;break;
case 2:
{
if(year%4==0&&year%100!=0||year%400==0) return 29;
else return 28;
break;
}
}
}

int year_s()
{
int s=1;
switch(month-1)
{
case 11:s+=30;
case 10:s+=31;
case 9:s+=30;
case 8:s+=31;
case 7:s+=31;
case 6:s+=30;
case 5:s+=31;
case 4:s+=30;
case 3:s+=31;
case 2:
{
if(year%4==0&&year%100!=0||year%400==0)s+=29;
else s+=28;
}
case 1:s+=31;
}
return s;
}

int week_s()
{
int s=1,n;
n=(year-1)%400;
for(int i=1;i<=n;i++)
{
if(year%4==0&&year%100!=0||year%400==0)
s+=2;
else s+=1;
}
s+=year_s();
s%=7;
return s;
}

void output_month()
{
int week,day,i;
printf("\t\t公元%d月\n",month);
printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
day=day_s();
week=week_s();
for(i=1;i<=week;i++)
{
printf(" \t");
}
for(i=1;i<=day;i++)
{
printf("%d",i);
if((i+week)%7==0) printf("\n");
else printf("\t");
}
for(int j=1;j<=42-week-i;j++)
{
printf("\t");
}
printf("\n\n");
}

void output_year()
{
printf("\t\t公元%d年\n",year);
for(int i=1;i<=12;i++)
{
month=i;
output_month();
}
printf("1查看下年\n");
printf("2查看上年\n");
printf("3重新输入年月\n");
printf("0退出\n");
printf("请选择:");
int n;
scanf("%d",&n);
system("cls");
switch(n)
{
case 1:year+=1;output_year();break;
case 2:year-=1;output_year();break;
case 3:main();break;
case 0:;break;
}

}
void menu()
{
printf("1查看下个月\n");
printf("2查看上个月\n");
printf("3查看本年全部日历\n");
printf("4重新输入年月\n");
printf("0退出\n");
printf("请选择:");
int n;
scanf("%d",&n);
system("cls");
switch(n)
{
case 1:
{
if(month+1==13)
{
year+=1;
month=1;
}
else month+=1;
printf("\t\t公元%d年\n",year);
output_month();
menu();
break;
}
case 2:
{
if(month-1==0)
{
year-=1;month=12;
}
else
{
month-=1;
printf("\t\t公元%d年\n",year);
output_month();
menu();
}
break;62
}
case 3:output_year();break;
case 4:main();break;
case 0:break;
}
}
相似回答