用c++编写日历程序
C语言编写万年历
C语言编写万年历程序实现万年历的查询功能,根据使用者输入的年份与月份输出本月的日历。
可以查询1950年之后的日期,查询范围可以在junde函数中进行修改。
源代码:#include<stdio.h>#include<windows.h>void input(int *p1,int *p2){printf("请输入年份:\n");scanf("%d",p1);printf("请输入月份:\n");scanf("%d",p2);}int caculate(int year,int month){int sum;if((year % 4 == 0&&year % 100 != 0)||year % 400 == 0){switch (month){case 1:sum = 31;break;case 2:sum = 29;break;case 3:sum = 31;break;case 4:sum = 30;break;case 5:sum = 31;break;case 6:sum = 30;break;case 7:sum = 31;break;case 8:sum = 31;break;case 9:sum = 30;break;case 10:sum = 31;break;case 11:sum = 30;break;case 12:sum = 31;break;default:break;}}else{switch (month){case 1:sum = 31;break;case 2:sum = 28;break;case 3:sum = 31;break;case 4:sum = 30;break;case 5:sum = 31;break;case 6:sum = 30;break;case 7:sum = 31;break;case 8:sum = 31;break;case 9:sum = 30;break;case 10:sum = 31;break;case 11:sum = 30;break;case 12:sum = 31;break;default:break;}}return sum;}int junde(int year,int month){int week;int a = 0,b = 0,i = 1950,sum = 0;if(year<1950){printf("输入错误!");return -1;}if(month<0||month>12){printf("输入错误!");return -1;}if(year == 1950){switch (month){case 1:b = 1;break;case 2:b = 1 + 31;break;case 3:b = 1 + 31 + 29;break;case 4:b = 1 + 31 + 29 + 31;break;case 5:b = 1 + 31 + 29 + 31 + 30;break;case 6:b = 1 + 31 + 29 + 31 + 30 + 31;break;case 7:b = 1 + 31 + 29 + 31 + 30 + 31 + 30;break;case 8:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31;break;case 9:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31;break;case 10:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30;break;case 11:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;break;case 12:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;break;default:break;}sum = b;week = (sum + 6) % 7;return week;}else if(year != i){for(i = 1950;i<year;i++){if(i % 4 == 0){a = 366;}if(i % 4 != 0){a = 365;}sum += a;}}if(year == i){if(year % 4 == 0){switch (month){case 1:b = 1;break;case 2:b = 1 + 31;break;case 3:b = 1 + 31 + 29;break;case 4:b = 1 + 31 + 29 + 31;break;case 5:b = 1 + 31 + 29 + 31 + 30;break;case 6:b = 1 + 31 + 29 + 31 + 30 + 31;break;case 7:b = 1 + 31 + 29 + 31 + 30 + 31 + 30;break;case 8:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31;break;case 9:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31;break;case 10:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30;break;case 11:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;break;case 12:b = 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;break;default:break;}sum = sum + b;}if(year % 4 != 0){switch (month){case 1:b = 1;break;case 2:b = 1 + 31;break;case 3:b = 1 + 31 + 28;break;case 4:b = 1 + 31 + 28 + 31;break;case 5:b = 1 + 31 + 28 + 31 + 30;break;case 6:b = 1 + 31 + 28 + 31 + 30 + 31;break;case 7:b = 1 + 31 + 28 + 31 + 30 + 31 + 30;break;case 8:b = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31;break;case 9:b = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;break;case 10:b = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;break;case 11:b = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;break;case 12:b = 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;break;default:break;}sum = sum + b;}}week = (sum + 6) % 7;return week;}void output(int days,int week){int num[50] = {0};int i;if(week == -1){exit(1);}printf("星期天\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");for(i = 0;i<week;i++){printf("\t");}for(i = 1;i<=days;i++){printf(" %d\t",i);if((i + week) % 7 == 0){printf("\n");}}printf("\n");}int main(){int year,month;int *p1,*p2;int days,week;p1 = &year,p2 = &month;while (1){input(p1,p2);days = caculate(year,month);week = junde(year,month);output(days,week);}return 0;}运行结果截图:。
用C语言编写的万年历
// VC++6.0 编译通过# include <stdio.h># include <windows.h>int year; //定义年份为全局变量int check_year (int year) //判断输入的年份是否为闰年{if(year%4 == 0 && year%100 !=0 || year%400 == 0)return 1;elsereturn 0;}int check_month (int month) //判断每月的天数{switch (month){case 0: return 0; break;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 (check_year(year) == 1)return 29;elsereturn 28;} break;}}int firstday (int year,int month) //判断某年某月的第一天为星期几{static int w=0;w += check_month(month-1);return ((year-1)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1+w)%7;}main (){int month=1,day,i,flag;printf ("请输入要打印的年份: ");scanf ("%d",&year);printf ("\n");printf (" %d年\n\n",year);for (month=1;month<=12;month++){printf (" %d月\n",month);printf (" ---------------------------\n");printf (" SUN MON TUE WED THU FRI SET\n");printf (" ---------------------------\n");flag = firstday(year,month);for (i=0;i<flag;i++) //每月第一天所在星期几之前的位置用空格输出printf (" ");for (day=1;day<=check_month(month);day++) //按格式输出日历{printf ("%4d",day);if ((day+flag)%7 == 0)printf ("\n");}printf("\n\n");system("pause");printf("\n");}}。
用c 编写日历
return n;
}
else days=28;
}
else days=31;
return days;
}
int first_day_of_month(int month,int leap)//相对每月第一天
{
int add=0,m=0,n=1;
for(n;n<month;n++)
{
m=days_of_month(n,leap);
printf("请输入年份和月份: \n");
scanf("%d,%d",&year,&month);
leap=is_leap(year);
day=days_of_month(month,leap);
week=week_month_year(year,month,leap);
printf("%d年%d月\n",year,month);
printf("SUN MON TUE WED THU FRI SAT\n");
printf("__________________________\n");
for(j=0;j<6;j++)
{
for(i=0;i<7;i++)
{
if((i>=week||j!=0)&&k<=day)
{
date[j][i]=k;
add+=m;
}
add=add%7;
return add;
}
int week_month_year(int year,int month,int leap)//
C语言万年历代码
题目:年历显示。
功能要求:(1)输入一个年份,输出是在屏幕上显示该年的日历。
假定输入的年份在1940-2040年之间。
(2)输入年月,输出该月的日历。
(3)输入年月日,输出距今天还有多少天,星期几,是否是公历节日。
如下图:以下部分为代码:#include<stdio.h>#include<math.h>void part1(int x); //函数1~6为打印日历的函数void part2(int x);void part3(int x);void part4(int x);void part5(int x);void part6(int x);int weak(int x,int y); //计算1号对应的星期int weak2(int x,int y,int z); //计算星期,为函数day提供数据int year(int x); //打印日历的函数int mouth(int x); //打印月历的函数int day(int x); //查询天数的函数int distance(int x,int y,int z); //提供距离天数查询int main(){int a;char z;printf("****************************************************************\n");printf(" * *\n");printf(" * *\n");printf(" * *\n");printf(" * 欢迎使用万年历!*\n");printf(" * *\n");printf(" * *\n");printf(" * 开发:杨润*\n");printf("****************************************************************\n");x: //位置标注printf("\n");printf("请输入一个日期(Ex:20150302或201503或2015):");scanf("%d",&a);printf("\n");if((1940<=a)&&(a<=2040)){year(a);}else if((194001<=a)&&(a<=204012)){mouth(a);}else if((19400101<=a)&&(a<=20401231)){day(a);}else{printf("输入错误,请重新输入!");goto x;}printf("\n");printf("\n");printf("继续查询(y),退出(n):");scanf("%s",&z);if(z=='y'){goto x;}else{;}return 0;}int year(int x){printf("-------------------------------%d年的年历----------------------------\n",x);printf("\n");part1(x); //调用第一部分函数part2(x);part3(x);part4(x);part5(x);part6(x);printf("-----------------------------------------------------------------------\n");return 0;}int mouth(int x){int i,n,y,y2,w,week,yue2,bz1; //n表示年份,y表示月份,w,y2均为临时变量,yue2表示2月份天数n=(int)(x/100);y=(int)(x%100);printf("您现在查询的是%d年%d月\n",n,y);printf(" \n");if(y==1) //1月的情况{printf("1月Sun Mon Tue Wed Thu Fri Sat\n");printf(" ");if(n<2000) //计算小于两千的1.1星期{y2=(n-1)%100; //y2 w=(int)(y2+(y2/4)+(19/4)-38+(26*1.4));week=w%7;}if(n>=2000) //计算大于两千的1.1星期{ y2=(n-1)%100;w=(int)(y2+(y2/4)+(20/4)-40+(26*1.4));week=w%7;}for(i=0;i<week;i++){printf(" ");}bz1=1;for(i=0;i<(7-week);i++) //第一行的一月份{if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第二行的1月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第三行的1月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第四行的1月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第五行的1月份for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf("\n");printf(" "); //第六行的1月份for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf("\n");}if(y==2) //2月的情况{if(n%4==0){if(n%100!=0)yue2=29;else if(n%100==0&&n%400==0)yue2=29;elseyue2=28;}else{yue2=28;}printf("2月Sun Mon Tue Wed Thu Fri Sat\n");printf(" ");if(n<2000) //计算小于两千的2.1星期{y2=(n-1)%100; //y2w=(int)(y2+(y2/4)+(19/4)-38+(26*1.5));week=w%7;}if(n>=2000) //计算大于两千的2.1星期{ y2=(n-1)%100;w=(int)(y2+(y2/4)+(20/4)-40+(26*1.5));week=w%7;}for(i=0;i<week;i++){printf(" ");}bz1=1;for(i=0;i<(7-week);i++) //第一行的2月份{if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第二行的2月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第三行的2月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第四行的2月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第五行的2月份for(i=0;i<7;i++){if(bz1<=yue2){printf(" %d ",bz1);}if(bz1>yue2){printf(" ");}bz1=bz1+1;}printf("\n");printf(" "); //第六行的2月份for(i=0;i<7;i++){if(bz1<=yue2){printf(" %d ",bz1);}if(bz1=yue2){printf(" ");}bz1=bz1+1;}printf("\n");}//2月情况判断完毕if((y==3)||(y==5)||(y==7)||(y==8)||(y==10)||(y==12)) //31天的情况(3、5、7、8、10、12月){printf("%d月Sun Mon Tue Wed Thu Fri Sat\n",y);printf(" ");week=weak(n,y);for(i=0;i<week;i++) //第一行{printf(" ");}bz1=1;for(i=0;i<(7-week);i++){if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第二行for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第三行for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第四行for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第五行for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf("\n");printf(" "); //第六行for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf("\n");}if((y==4)||(y==6)||(y==9)||(y==11)) //30天的情况(4、6、9、11月){printf("%d月Sun Mon Tue Wed Thu Fri Sat\n",y);printf(" ");week=weak(n,y);for(i=0;i<week;i++) //第一行{printf(" ");}bz1=1;for(i=0;i<(7-week);i++){if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第二行for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第三行for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第四行for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf("\n");printf(" "); //第五行for(i=0;i<7;i++){if(bz1<=30){printf(" %d ",bz1);}if(bz1>30){printf(" ");}bz1=bz1+1;}printf("\n");printf(" "); //第六行for(i=0;i<7;i++){if(bz1<=30){printf(" %d ",bz1);}if(bz1>30){printf(" ");}bz1=bz1+1;}printf("\n");}return 0;}int day(int x){int n,y,y1,t,week; //n为年,y为yue,y1位计算y的临时变量,t为天,week为周,p为距离今天天数n=x/10000;y1=x%10000;y=y1/100;t=x%100;printf("\n");printf("您查询的是:%d年%d月%d日",n,y,t);week=weak2(n,y,t);switch(week) //根据返回数值判断星期{case 0:printf("星期日");break;case 1:printf("星期一");break;case 2:printf("星期二");break;case 3:printf("星期三");break;case 4:printf("星期四");break;case 5:printf("星期五");break;case 6:printf("星期六");break;}if(y==1&&t==1) printf("这天是元旦"); //查询节日if(y==2&&t==2) printf("这天是世界湿地日");if(y==2&&t==14) printf("这天是情人节");if(y==3&&t==3) printf("这天是全国爱耳日");if(y==3&&t==5) printf("这天是青年志愿者服务日");if(y==3&&t==8) printf("这天是国际妇女节");if(y==3&&t==9) printf("这天是保护母亲河日");if(y==3&&t==12) printf("这天是中国植树节");if(y==3&&t==14) printf("这天是白色情人节\n");if(y==3&&t==14) printf("这天是国际警察日");if(y==3&&t==15) printf("这天是世界消费者权益日");if(y==3&&t==21) printf("这天是世界森林日\n");if(y==3&&t==21) printf("这天是世界睡眠日");if(y==3&&t==22) printf("这天是世界水日");if(y==3&&t==23) printf("这天是世界气象日");if(y==3&&t==24) printf("这天是世界防治结核病日");if(y==4&&t==1) printf("这天是愚人节");if(y==4&&t==5) printf("这天是清明节");if(y==4&&t==7) printf("这天是世界卫生日");if(y==4&&t==22) printf("这天是世界地球日");if(y==4&&t==26) printf("这天是世界知识产权日");if(y==5&&t==1) printf("这天是国际劳动节");if(y==5&&t==3) printf("这天是世界哮喘日");if(y==5&&t==4) printf("这天是中国青年节");if(y==5&&t==8) printf("这天是世界红十字日");if(y==5&&t==12) printf("这天是国际护士节");if(y==5&&t==15) printf("这天是国际家庭日");if(y==5&&t==17) printf("这天是世界电信日");if(y==5&&t==20) printf("这天是全国学生营养日");if(y==5&&t==23) printf("这天是国际牛奶日");if(y==5&&t==31) printf("这天是世界无烟日");if(y==6&&t==1) printf("这天是国际儿童节");if(y==6&&t==5) printf("这天是世界环境日");if(y==6&&t==6) printf("这天是全国爱眼日");if(y==6&&t==17) printf("这天是世界防治荒漠化和干旱日"); if(y==6&&t==23) printf("这天是国际奥林匹克日");if(y==6&&t==25) printf("这天是全国土地日");if(y==6&&t==26) printf("这天是国际禁毒日");if(y==7&&t==1) printf("这天是中国共产党诞生日\n");if(y==7&&t==1) printf("这天是国际建筑日");if(y==7&&t==7) printf("这天是中国人民抗日战争纪念日"); if(y==7&&t==11) printf("这天是世界人口日");if(y==8&&t==1) printf("这天是中国人民解放军建军节");if(y==8&&t==12) printf("这天是国际青年节");if(y==9&&t==8) printf("这天是国际扫盲日");if(y==9&&t==10) printf("这天是中国教师节");if(y==9&&t==16) printf("这天是中国脑健康日\n");if(y==9&&t==16) printf("这天是国际臭氧层保护日");if(y==9&&t==20) printf("这天是全国爱牙日");if(y==9&&t==21) printf("这天是世界停火日");if(y==9&&t==27) printf("这天是世界旅游日");if(y==10&&t==1) printf("这天是中华人民共和国国庆节\n");if(y==10&&t==1) printf("这天是国际音乐日\n");if(y==10&&t==1) printf("这天是国际老年人日");if(y==10&&t==4) printf("这天是世界动物日");if(y==10&&t==5) printf("这天是世界教师日");if(y==10&&t==8) printf("这天是全国高血压日");if(y==10&&t==9) printf("这天是世界邮政日");if(y==10&&t==10) printf("这天是世界精神卫生日");if(y==10&&t==14) printf("这天是世界标准日");if(y==10&&t==15) printf("这天是国际盲人节\n");if(y==10&&t==15) printf("这天是世界农村妇女日");if(y==10&&t==16) printf("这天是世界粮食日");if(y==10&&t==17) printf("这天是国际消除贫困日");if(y==10&&t==24) printf("这天是联合国日\n");if(y==10&&t==24) printf("这天是世界发展新闻日");if(y==10&&t==28) printf("这天是中国男性健康日");if(y==10&&t==29) printf("这天是国际生物多样性日");if(y==10&&t==31) printf("这天是万圣节");if(y==11&&t==8) printf("这天是中国记者节");if(y==11&&t==9) printf("这天是消防宣传日");if(y==11&&t==14) printf("这天是世界糖尿病日");if(y==11&&t==17) printf("这天是国际大学生节");if(y==11&&t==25) printf("这天是国际消除对妇女的暴力日");if(y==12&&t==1) printf("这天是世界爱滋病日");if(y==12&&t==3) printf("这天是世界残疾人日");if(y==12&&t==4) printf("这天是全国法制宣传日");if(y==12&&t==9) printf("这天是世界足球日");if(y==12&&t==25) printf("这天是圣诞节");if(y==12&&t==29) printf("这天是国际生物多样性日");printf("\n");distance(n,y,t);return 0;}//计算今天日期的函数结尾void part1(int x) //第一部分{int i,week,week2,y,w,b1,bz1,bz2;//week,week2用于计算1.1的星期,bz1和bz2为日期的递增b1=7;printf("1月Sun Mon Tue Wed Thu Fri Sat");printf(" ");printf("7月Sun Mon Tue Wed Thu Fri Sat\n");printf(" ");if(x<2000) //计算小于两千的1.1星期{ y=(x-1)%100;w=(int)(y+(y/4)+(19/4)-38+(26*1.4));week=w%7;}if(x>=2000) //计算大于两千的1.1星期{ y=(x-1)%100;w=(int)(y+(y/4)+(20/4)-40+(26*1.4));week=w%7;}week2=weak(x,b1); //调用计算星期的函数(7.1) for(i=0;i<week;i++){printf(" ");}bz1=1,bz2=1;for(i=0;i<(7-week);i++){if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印7月的for(i=0;i<=week2;i++){printf(" ");}for(i=0;i<(7-week2);i++){if(bz2<10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第一行打印完毕printf(" "); //第二行的1月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印7月的第二行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第二行打印结束printf(" "); //第三行的1月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印7月的第三行for(i=0;i<7;i++){ if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第三行打印结束printf(" "); //第四行的1月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印7月的第四行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第四行打印结束printf(" "); //第五行的1月份for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印7月的第五行for(i=0;i<7;i++){if(bz2<=31){printf(" %d ",bz2);}if(bz2>31){printf(" ");}bz2=bz2+1;}printf("\n"); //第五行打印结束printf(" "); //第六行的1月份for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印7月的第六行for(i=0;i<7;i++){if(bz2<=31){printf(" %d ",bz2);}if(bz2>31){printf(" ");}bz2=bz2+1;}printf("\n"); //第六行打印结束} //第一部分1月、7月打印完毕!void part2(int x) //第二部分2月、8月开始打印!//第二部分{int i,week,week2,y,w,b1,bz1,bz2,yue2;//week,week2用于计算2.1的星期,bz1和bz2为日期的递增b1=8; //表示当前部分的月份printf("2月Sun Mon Tue Wed Thu Fri Sat");printf(" ");printf("8月Sun Mon Tue Wed Thu Fri Sat\n");printf(" ");if(x%4==0){if(x%100!=0)yue2=29;else if(x%100==0&&x%400==0)yue2=29;elseyue2=28;}else{yue2=28;}if(x<2000) //计算小于两千的1.1星期{ y=(x-1)%100;w=(int)(y+(y/4)+(19/4)-38+(26*1.5));week=w%7;}if(x>=2000) //计算大于两千的1.1星期{ y=(x-1)%100;w=(int)(y+(y/4)+(20/4)-40+(26*1.5));week=w%7;}week2=weak(x,b1); //调用计算星期的函数(7.1) for(i=0;i<week;i++){printf(" ");}bz1=1,bz2=1;for(i=0;i<(7-week);i++){if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印8月的第一行for(i=0;i<=week2;i++){printf(" ");}for(i=0;i<(7-week2);i++){if(bz2<10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第一行打印完毕printf(" "); //2月份的第二行for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印8月的第二行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第二行打印结束printf(" "); //第三行的2月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印8月的第三行for(i=0;i<7;i++){ if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第三行打印结束printf(" "); //第四行的2月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印8月的第四行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第四行打印结束printf(" "); //第五行的2月份for(i=0;i<7;i++){if(bz1<=yue2){printf(" %d ",bz1);}if(bz1>yue2){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印8月的第五行for(i=0;i<7;i++){if(bz2<=31){printf(" %d ",bz2);}if(bz2>31){printf(" ");}bz2=bz2+1;}printf("\n"); //第五行打印结束printf(" "); //第六行的2月份for(i=0;i<7;i++){if(bz1<=yue2){printf(" %d ",bz1);}if(bz1>yue2){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印8月的第六行for(i=0;i<7;i++){if(bz2<=31){printf(" %d ",bz2);}if(bz2>31){printf(" ");}bz2=bz2+1;}printf("\n"); //第六行打印结束}void part3(int x) //第三部分{int i,week,week2,a1,b1,bz1,bz2;//week,week2用于计算1.1的星期,bz1和bz2为日期的递增a1=3,b1=9; //表示当前不封的月份printf("3月Sun Mon Tue Wed Thu Fri Sat");printf(" ");printf("9月Sun Mon Tue Wed Thu Fri Sat\n");printf(" ");week=weak(x,a1); //调用计算星期的函数(3.1)week2=weak(x,b1); //调用计算星期的函数(8.1)for(i=0;i<week;i++){printf(" ");}bz1=1,bz2=1;for(i=0;i<(7-week);i++){if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印9月的for(i=0;i<=week2;i++){printf(" ");}for(i=0;i<(7-week2);i++){if(bz2<10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第一行打印完毕printf(" "); //第二行的3月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印9月的第二行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第二行打印结束printf(" "); //第三行的3月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印9月的第三行for(i=0;i<7;i++){ if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第三行打印结束printf(" "); //第四行的3月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印9月的第四行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第四行打印结束printf(" "); //第五行的3月份for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印9月的第五行for(i=0;i<7;i++){if(bz2<=30){printf(" %d ",bz2);}if(bz2>30){printf(" ");}bz2=bz2+1;}printf("\n"); //第五行打印结束printf(" "); //第六行的3月份for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印9月的第六行for(i=0;i<7;i++){if(bz2<=30){printf(" %d ",bz2);}if(bz2>30){printf(" ");}bz2=bz2+1;}printf("\n"); //第六行打印结束}void part4(int x) //第四部分{int i,week,week2,a1,b1,bz1,bz2;//week,week2用于计算1.1的星期,bz1和bz2为日期的递增a1=4,b1=10; //表示当前不封的月份printf("4月Sun Mon Tue Wed Thu Fri Sat");printf(" ");printf("10月Sun Mon Tue Wed Thu Fri Sat\n");printf(" ");week=weak(x,a1); //调用计算星期的函数(4.1)week2=weak(x,b1); //调用计算星期的函数(10.1)for(i=0;i<week;i++){printf(" ");}bz1=1,bz2=1;for(i=0;i<(7-week);i++){if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印4月的for(i=0;i<=week2;i++){printf(" ");}for(i=0;i<(7-week2);i++){if(bz2<10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第一行打印完毕printf(" "); //第二行的4月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印10月的第二行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第二行打印结束printf(" "); //第三行的4月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印10月的第三行for(i=0;i<7;i++){ if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第三行打印结束printf(" "); //第四行的4月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印10月的第四行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第四行打印结束printf(" "); //第五行的10月份for(i=0;i<7;i++){if(bz1<=30){printf(" %d ",bz1);}if(bz1>30){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印10月的第五行for(i=0;i<7;i++){if(bz2<=31){printf(" %d ",bz2);}if(bz2>31){printf(" ");}bz2=bz2+1;}printf("\n"); //第五行打印结束printf(" "); //第六行的4月份for(i=0;i<7;i++){if(bz1<=30){printf(" %d ",bz1);}if(bz1>30){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印10月的第六行for(i=0;i<7;i++){if(bz2<=31){printf(" %d ",bz2);}if(bz2>31){printf(" ");}bz2=bz2+1;}printf("\n"); //第六行打印结束}void part5(int x) //第五部分{int i,week,week2,a1,b1,bz1,bz2;//week,week2用于计算1.1的星期,bz1和bz2为日期的递增a1=5,b1=11; //表示当前不封的月份printf("5月Sun Mon Tue Wed Thu Fri Sat");printf(" ");printf("11月Sun Mon Tue Wed Thu Fri Sat\n");printf(" ");week=weak(x,a1);week2=weak(x,b1); //调用计算星期的函数(11.1)for(i=0;i<week;i++){printf(" ");}bz1=1,bz2=1;for(i=0;i<(7-week);i++){if(bz1<10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印11月的for(i=0;i<=week2;i++){printf(" ");}for(i=0;i<(7-week2);i++){if(bz2<10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第一行打印完毕printf(" "); //第二行的5月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印11月的第二行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10)bz2=bz2+1;}printf("\n"); //第二行打印结束printf(" "); //第三行的5月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印11月的第三行for(i=0;i<7;i++){ if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第三行打印结束printf(" "); //第四行的5月份for(i=0;i<7;i++){ if(bz1<10){printf(" %d ",bz1);}if(bz1>=10){printf(" %d ",bz1);}bz1=bz1+1;}printf(" "); //从这里开始打印11月的第四行for(i=0;i<7;i++){if(bz2<10){printf(" %d ",bz2);}if(bz2>=10){printf(" %d ",bz2);}bz2=bz2+1;}printf("\n"); //第四行打印结束printf(" "); //第五行的5月份for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31)bz1=bz1+1;}printf(" "); //从这里开始打印11月的第五行for(i=0;i<7;i++){if(bz2<=30){printf(" %d ",bz2);}if(bz2>30){printf(" ");}bz2=bz2+1;}printf("\n"); //第五行打印结束printf(" "); //第六行的5月份for(i=0;i<7;i++){if(bz1<=31){printf(" %d ",bz1);}if(bz1>31){printf(" ");}bz1=bz1+1;}printf(" "); //从这里开始打印11月的第六行for(i=0;i<7;i++){if(bz2<=30){printf(" %d ",bz2);}if(bz2>30){printf(" ");}bz2=bz2+1;}printf("\n"); //第六行打印结束}void part6(int x) //第六部分{int i,week,week2,a1,b1,bz1,bz2;//week,week2用于计算1.1的星期,bz1和bz2为日期的递增a1=6,b1=12; //表示当前不封的月份printf("6月Sun Mon Tue Wed Thu Fri Sat");printf(" ");printf("12月Sun Mon Tue Wed Thu Fri Sat\n");printf(" ");week=weak(x,a1);week2=weak(x,b1); //调用计算星期的函数(12.1)for(i=0;i<week;i++){printf(" ");}。
c语言万年历程序
#include <stdio.h>#include <stdlib.h>#include <ctype.h>int leap (int year) ;//判断是否为闰年int days_month (int month,int year) ;//判断这个月的天数int menu_select();/*void f(){int day,month,year,sum,leap,S;printf("\n请输入年月日\n");scanf("%d%d%d",&year,&month,&day);switch(month){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:printf("data error");break;}sum=sum+day;if(year%400==0||(year%4==0&&year%100!=0))leap=1;else leap=0;if(leap==1&&month>2)sum++;S=(year-1+(year-1)/4-(year-1)/100+(year-1)/400+sum)%7; //X表示年份,C是该年份元旦开始到这一日的天数//S/7的系数就是星期数//01printf("%d",S);switch(S){case 1:printf("星期一\n");break;case 2:printf("星期二\n");break;case 3:printf("星期三\n");break;case 4:printf("星期四\n");break;case 5:printf("星期五\n");break;case 6:printf("星期六\n");break;case 0:printf("星期日\n");break;}}*/int Leap(int year){ int leap;if(year%400==0||(year%4==0&&year%100!=0))leap=1;else leap=0;return leap;}int SU(int sum,int year){int S;S=(year-1+(year-1)/4-(year-1)/100+(year-1)/400+sum)%7;return S;}int Sum(int year,int month,int day=1){int sum,leap;switch(month){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:printf("data error");break;}sum=sum+day;leap=Leap(year);if(leap==1&&month>2)sum++;return sum;}void Print(int S){switch(S){case 1:printf("星期一\n");break;case 2:printf("星期二\n");break;case 3:printf("星期三\n");break;case 4:printf("星期四\n");break;case 5:printf("星期五\n");break;case 6:printf("星期六\n");break;case 0:printf("星期日\n");break;}}void Fun1(){int day,month,year,sum,S;printf("\n请输入年月日\n");scanf("%d%d%d",&year,&month,&day);sum=Sum(year,month,day);S=SU(sum,year);Print(S);}void Fun2(){int i,j=1,k=1,a,b,month,year,sum;printf("\n 输入年月:\n");scanf("%d%d",&year,&month);b=days_month(month,year);sum=Sum(year,month);a=SU(sum,year);printf("*************************************\n");printf(" Sun Mon Tue Wed Thu Fri Sat \n");if(a==7){for(i=1;i<=b;i++){printf("%4d",i);if(i%7==0){printf("\n");}}}if(a!=7){while (j<=4*a){printf(" ");j++;}for(i=1;i<=b;i++){printf("%4d",i);if(i==7*k-a){printf("\n");k++;}}}printf("\n*************************************\n");printf("\n");}int days_month (int month,int year){if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 31;if(month==4||month==6||month==9||month==11)return 30;if(month==2&&Leap(year)==1) return 29;else return 28;}void main(){for(;;){switch(menu_select()){case 1:printf("输入年月日计算该月第一天是星期几\n");Fun1();break;case 2:printf("打印任意日历\n");Fun2();break;case 3:printf("谢谢使用!\n");exit(0);}}}int menu_select(){ system("cls");char s;int cn;printf("1. 输入年月日计算该月第一天是星期几\n");printf("2. 打印任意日历\n");printf("3.谢谢使用! \n");printf("input 1-3:");do {s=getchar();cn=(int)s-48;}while(cn<0||cn>6);return cn;}。
c语言:万年历
else if(m==4||m==6||m==9||m==11)/*在一定程度上避免输入的天数出错*/
{ if(d>0&&d<=30);
else {printf("错误!");goto begin;}
}
else if(m==2&&x==1)
{ if(d>0&&d<=29);
if(rey==0) while(getchar()!='\n');
if(y>0);
else {printf("错误!");goto begin;}/*在一定程度上避免输入的年份出错*/
x=Year(y);
if(x==1) printf("闰年\n");
if(x==0) printf("平年\n");
if(s==5) printf("星期五\n");
if(s==6) printf("星期六\n");
}
else printf("错误!");/*防止输入菜单错误*/
goto begin;
esc:printf("Thinks\n");
}
int Year(int n) /* 函数名:Year */
{
printf(" %d ",k);
if((j+k)%7==0) printf("\n");
}
}
else if(a==4)/*打印某年日历*/
{
printf("Input yeas:\n");
用C 编写日历程序
第3页共4页
第4页共4页
1、first.cpp 文件清单 #include<iostream.h> int IsLeapYear(int year) //判断是否是闰年; {
if(year%4==0&&year%100!=0||year%400==0) return 1;
else return 0; } 2、second.cpp 文件清单 #include<iostream.h> #include"aaa.h"// void main() {
case 6:cout<<"\n"<<month[i]<<endl; cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四" <<"\t"<<"五"<<"\t"<<"六"<<endl; cout<<" \t"<<" \t"<<" \t"<<" \t"<<" \t"<<" \t"; for(j=1;j<=month_number[i];j++) { if((j+7)%7!=1) cout<<j<<"\t"; else cout<<j<<endl; };break;
万年历C编程
#include <stdio.h>#include <stdlib.h>#include <conio.h>#define X " Sun Mon Tue Wed Thu Fri Sat"void menu(){system("cls");printf("\nTHIS IS THE MENU OF THE PROGRAM !");printf("\nYOU CAN CHOOSE THE NUMBER FOR THE FUNCTIOM:"); printf("\n\n");printf("1 Find the day by year-month-date.\n");printf("2 Find out whether the year you input is a leap year.\n");printf("3 Print the calendar of the year you input.\n");printf("4 Exit.\n\n");printf("Input your choice:");}intgetday(intyear,intmonth,int date){intisleap(int year);intflag,s,i;int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};intcont=0;flag=isleap(year);if(flag==1)a[2]++;for(i=1;i<month;i++){cont=cont+a[i];}cont=cont+date;s=year+1+(year-1)/4+(year-1)/100+(year-1)/400+cont;return s%7;}intisleap(int year){if(year%4==0&&year%100||year%400==0)return 1;elsereturn 0;}void print(int n){int i;for(i=0;i<n;i++) printf(" ");}int day(int year){long a,b;if(year<=2000){a=2000-year;b=6-(a+a/4-a/100+a/400)%7;return b;}else{a=year-2000;b=(a+1+(a-1)/4-(a-1)/100+(a-1)/400)%7+6;return b%7;}}void printcalendar(int year){int i,j,k,m,n,f1,f2,d;int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; printf("\nThe calendar of the year %d.\n\n",year);d=day(year);if(isleap(year)==1)a[2]++;for(i=1;i<=12;i+=2){m=0; n=0; f1=0; f2=0;switch(i){case 1:printf(" Januray 1 ");break;case 3:printf(" March 3 ");break;case 5:printf(" May 5 ");break;case 7:printf(" July 7 "); break;case 9:printf(" September 9 ");break;case 11:printf(" Nevember 11 ");break;}print(21);switch(i+1){case 2:printf(" February 2 "); break;case 4:printf(" April 4 "); break;case 6:printf(" June 6 "); break;case 8:printf(" August 8 "); break;case 10:printf(" October 10 "); break;case 12:printf(" December 12"); break;}printf("\n");printf(X);print(6);printf(X);printf("\n");for(j=0;j<6;j++){if(j==0){print(d*4);for(k=0;k<7-d;k++){printf("%4d",++m);}print(6);d+=a[i]%7;d%=7;print(d*4);for(k=0;k<7-d;k++){printf("%4d",++n);}printf("\n");}else{for(k=0;k<7;k++){if(m<a[i]){printf("%4d",++m);}else{print(4);}if(m==a[i]) f1=1;}print(6);for(k=0;k<7;k++){if(n<a[i+1]){printf("%4d",++n);}else{print(4);}if(n==a[i+1]) f2=1;}printf("\n");if(f1&&f2) break;}}d+=a[i+1]%7;d%=7;printf(" ");for(k=0;k<27;k++){printf("=");}print(6);printf(" ");for(k=0;k<27;k++){printf("=");}printf("\n");if(i==5){printf("Press any key to others!"); getch();system("cls");}}}void main(){int choice;intyear,month,date;intday,flag;char con;menu();scanf("%d",&choice);if(choice==1){r1:system("cls");printf("\nPlease input the year-month-date(XXXX,XX,XX):"); scanf("%d,%d,%d",&year,&month,&date);day=getday(year,month,date);if(day==0)printf("\n%d-%d-%d is Sunday!\n",year,month,date);if(day==1)printf("\n%d-%d-%d is Monday!\n",year,month,date);if(day==2)printf("\n%d-%d-%d is Tuesday!\n",year,month,date);if(day==3)printf("\n%d-%d-%d is Wednesday!\n",year,month,date);if(day==4)printf("\n%d-%d-%d is Thursday!\n",year,month,date);if(day==5)printf("\n%d-%d-%d is Friday!\n",year,month,date);if(day==6)printf("\n%d-%d-%d is Saturday!\n",year,month,date); printf("\nContinue...(Y/N)");con=getch();if((con=='y')||(con=='Y'))goto r1;if((con=='n')||(con=='N'))main();}if(choice==2){r2:system("cls");printf("\nPlease input the year(XXXX):");scanf("%d",&year);flag=isleap(year);if(flag==1)printf("\nThe year %d is leap year!\n",year);if(flag==0)printf("\nThe year %d is not leap year!\n",year);printf("\nContinue...(Y/N)");con=getch();if((con=='y')||(con=='Y'))goto r2;if((con=='n')||(con=='N'))main();}if(choice==3){r3:system("cls");printf("\nPlease input the year(XXXX):"); scanf("%d",&year);printcalendar(year);printf("Continue...(Y/N)");con=getch();if((con=='y')||(con=='Y'))goto r3;if((con=='n')||(con=='N'))main();}if(choice==4){system("cls");printf("\nDo you want to Exit?(Y/N)");con=getch();if((con=='y')||(con=='Y'))return;if((con=='n')||(con=='N'))main();}}/*【要求】:1.程序运行后,首先在屏幕上显示主菜单:1.查询某年某月某日是星期几2.查询某年是否是闰年3.打印某年的全年日历4.退出2.在主菜单中输入1后,显示:“请输入年月日(XXXX,XX,XX)”运行后输出:XXXX年XX月XX日是星期X,是否继续查询(Y/N)?如果输入Y,则重新显示“请输入年月日(XXXX,XX,XX)”,否则回到主菜单。
c日历源代码
#include <stdio.h>#include <windows.h>void month(int a)//输出月份和星期{printf("\t");switch (a){case 1:printf("Janurary 1\t\t\t\t");case 2:printf("February 2");break;case 3:printf("March 3 \t\t\t\t");case 4:printf("April 4");break;case 5:printf("May 5 \t\t\t\t");case 6:printf("June 6");break;case 7:printf("July 7 \t\t\t\t");case 8:printf("August 8");break;case 9:printf("September 9 \t\t\t");case 10:printf("October 10");break;case 11:printf("November 11 \t\t\t");case 12:printf("December 12");break;}printf("\n\t");printf("Sun Mon Tue Wed Thu Fri Sat\t\t");printf("Sun Mon Tue Wed Thu Fri Sat\n\t");}int date(int a,int b,int c)//输出日期a为月份b为判断润平年r为某月最后一天的星期数{void space(int a);int i,j,n,m,x=1,y,k=0;switch (a){case 1:n=31;break;case 3:n=31;m=30;break;case 5:n=31;m=30;break;case 7:n=31;m=31;break;case 9:n=30;m=31;break;case 11:n=30;m=31;break;}if (a==1)if (b==0)m=28;elsem=29;for (i=1,j=c;i<=n;i++,j++){printf("%2d ",i);if (j==7 && i!=n){j=0;printf("\t\t");for (y=1;x<=m;y++){if (k==0){c=(c+n-1)%7+1;while (c>7)c=c-7;space(c);y=c;k=1;if (y<1)y=1;}printf("%2d ",x);if (y==7 && x!=m){x++;printf("\n\t");y=0;break;}if (x==m && y==7){x++;printf("\n\t");y=0;break;}if (x==m && y!=7)printf("\n\t");x++;}}if (i==n && j==7){j=0;i=0;break;}}if (i!=0)while (7-j){printf(" ");j=1+j;}printf("\t\t");if (x<=m)for (y=1;x<=m;y++,x++){printf("%2d ",x);if (y==7 && x!=m){printf("\n\t\t\t\t\t\t");y=0;}}printf("\n\t===========================\t\t");printf("===========================\n\n");if (y>7 || y==0)y=1;return y;}int leap(int a)//判断是否为闰年{int n;if ((a%4==0 && a%100!=0) || a%400==0)n=1;elsen=0;return n;}int print()//输出日历{void shxi(int a);void ganzhi(int a);void space(int a);int n,days,year,result,r,i,week,rem;while (1){printf("\t\t请输入年份:");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12|240);//设置字体颜色i=scanf("%d",&year);if (i==1){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0|240);break;}elsefflush(stdin);printf("\t\t错误输入!\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0|240);}ganzhi(year);shxi(year);n=leap(year);if (n)days=366;elsedays=365;result=(year-1+(year-1)/4-(year-1)/100+(year-1)/400+days)%7-n;//一月一号星期几的判断公式r=result+1;week=(days+result)/7;rem=(days +result)%7;if (rem!=0) week=week+rem/7+1;//计算这一年有多少周printf("\t\t\t\t\t%d周\n",week);printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");for (i=1;i<=12;i+=2){month(i);space(r);r=date(i,n,r);}printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");return 0;}void space(int a)//输出空格{int i;for (i=0;i<a-1;i++)printf(" ");}int main()//主程序输出选择菜单{void fprint();int n;system("color f0");//设置背景和字体颜色do{printf("\t\t* * * * * * * * * * * * * * * * * * * * * *\n");printf("\t\t*\t1---查询(Search) *\n");printf("\t\t*\t2---导出(Lead) *\n");printf("\t\t*\t0---关闭(Close) *\n");printf("\t\t* * * * * * * * * * * * * * * * * * * * * *\n");printf("\t\t请选择:");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12|240);//设置字体颜色scanf("%d",&n);system("cls");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0|240);switch (n){case 1:print();break;case 2:fprint();break;case 0:break;default:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12|240);printf("\ t\t错误选择!请重选");}fflush(stdin);printf("\n");} while(n!=0);return 0;}void shxi(int a)//判断这一年的属性并输出{int s;if (a>=2008) //属性判断公式s=((a-2008)%100)%12+1;elses=13-((2008-a)%100)%12;switch (s){case 1:printf("鼠");break;case 2:printf("牛");break;case 3:printf("虎");break;case 4:printf("兔");break;case 5:printf("龙");break;case 6:printf("蛇");break;case 7:printf("马");break;case 8:printf("羊");break;case 9:printf("猴");break;case 10:printf("鸡");break;case 11:printf("狗");break;case 12:printf("猪");break;}}void ganzhi(int a)//判断天干地支{int g,z;g=(a-3)%10;//天干判断公式z=(a-3)%12;//地支判断公式printf("\t\t\t");switch (g){case 1:printf("甲");break;case 2:printf("乙");break;case 3:printf("丙");break;case 4:printf("丁");break;case 5:printf("戊");break;case 6:printf("己");break;case 7:printf("庚");break;case 8:printf("辛");break;case 9:printf("壬");break;case 0:printf("癸");break;}switch (z){case 1:printf("子");break;case 2:printf("丑");break;case 3:printf("寅");break;case 4:printf("卯");break;case 5:printf("辰");break;case 6:printf("巳");break;case 8:printf("未");break;case 9:printf("申");break;case 10:printf("酉");break;case 11:printf("戌");break;case 0:printf("亥");break;}printf("年\t");}FILE *fp;//定义文件指针void fmonth(int a)//导出月份和星期{fprintf(fp,"\t");switch (a){case 1:fprintf(fp,"Janurary 1\t\t\t\t");case 2:fprintf(fp,"February 2");break;case 3:fprintf(fp,"March 3 \t\t\t\t");case 4:fprintf(fp,"April 4");break;case 5:fprintf(fp,"May 5 \t\t\t\t");case 6:fprintf(fp,"June 6");break;case 7:fprintf(fp,"July 7 \t\t\t\t");case 8:fprintf(fp,"August 8");break;case 9:fprintf(fp,"September 9 \t\t\t");case 10:fprintf(fp,"October 10");break;case 11:fprintf(fp,"November 11 \t\t\t");case 12:fprintf(fp,"December 12");break;}fprintf(fp,"\n\t");fprintf(fp,"Sun Mon Tue Wed Thu Fri Sat\t\t");fprintf(fp,"Sun Mon Tue Wed Thu Fri Sat\n\t"); }int fdate(int a,int b,int c)//导出日期{int i,j,n,m,x=1,y,k=0,r;switch (a){case 1:n=31;break;case 3:n=31;m=30;break;case 5:n=31;m=30;break;case 7:n=31;m=31;break;case 11:n=30;m=31;break;}if (a==1)if (b==0)m=28;elsem=29;for (i=1,j=c;i<=n;i++,j++){fprintf(fp,"%2d ",i);if (j==7 && i!=n){j=0;fprintf(fp,"\t\t");for (y=1;x<=m;y++){if (k==0){c=(c+n-1)%7+1;while (c>7)c=c-7;for (r=0;r<c-1;r++)fprintf(fp," ");y=c;k=1;if (y<1)y=1;}fprintf(fp,"%2d ",x);if (y==7 && x!=m){x++;fprintf(fp,"\n\t");y=0;break;}if (x==m && y==7){x++;fprintf(fp,"\n\t");y=0;break;}if (x==m && y!=7)fprintf(fp,"\n\t");x++;}}if (i==n && j==7){j=0;i=0;break;}}if (i!=0)while (7-j){fprintf(fp," ");j=1+j;}fprintf(fp,"\t\t");if (x<=m)for (y=1;x<=m;y++,x++){fprintf(fp,"%2d ",x);if (y==7 && x!=m){fprintf(fp,"\n\t\t\t\t\t\t");y=0;}}fprintf(fp,"\n\t===========================\t\t");fprintf(fp,"===========================\n\n");if (y>7 || y==0)y=1;return y;}void fprint()//导出文件{void fshxi(int a);void fganzhi(int a);void fspace(int a);int n,days,year,result,r,i,week,rem;char filename[10],type[]=".txt";while (1){printf("\t\t请输入年份:");i=scanf("%d",&year);if (i==1)break;elsefflush(stdin);printf("\t\t错误输入!\n");}printf("\t\t请输入导出文件名:");scanf("%s",&filename);fp=fopen(strcat(filename,type),"w");//以写的方式打开文件fganzhi(year);fshxi(year);n=leap(year);if (n)days=366;elsedays=365;result=(year-1+(year-1)/4-(year-1)/100+(year-1)/400+days)%7-n;r=result+1;week=(days+result)/7;rem=(days +result)%7;if (rem!=0) week=week+rem/7+1;fprintf(fp,"\t\t\t\t\t\t\t\t%d周\n",week);fprintf(fp," * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");for (i=1;i<=12;i+=2){fmonth(i);fspace(r);r=fdate(i,n,r);}fprintf(fp," * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");fclose(fp);//关闭文件printf("\t\t已导出完成");}void fspace(int a)//导出空格{int i;for (i=0;i<a-1;i++)fprintf(fp," ");}void fshxi(int a)//导出属性{int s;if (a>=2008)s=((a-2008)%100)%12+1;elses=13-((2008-a)%100)%12;switch (s){case 1:fprintf(fp,"鼠");break;case 2:fprintf(fp,"牛");break;case 3:fprintf(fp,"虎");break;case 4:fprintf(fp,"兔");break;case 5:fprintf(fp,"龙");break;case 6:fprintf(fp,"蛇");break;case 7:fprintf(fp,"马");break;case 8:fprintf(fp,"羊");break;case 9:fprintf(fp,"猴");break;case 10:fprintf(fp,"鸡");break;case 11:fprintf(fp,"狗");break;case 12:fprintf(fp,"猪");break;}fprintf(fp,"\n");}void fganzhi(int a)//导出天干地支{int g,z;g=(a-3)%10;z=(a-3)%12;fprintf(fp,"\t\t\t%d年\t\t",a);switch (g){case 1:fprintf(fp,"甲");break;case 2:fprintf(fp,"乙");break;case 3:fprintf(fp,"丙");break;case 4:fprintf(fp,"丁");break;case 5:fprintf(fp,"戊");break;case 6:fprintf(fp,"己");break;case 7:fprintf(fp,"庚");break;case 8:fprintf(fp,"辛");break;case 9:fprintf(fp,"壬");break;case 0:fprintf(fp,"癸");break;}switch (z){case 1:fprintf(fp,"子");break;case 2:fprintf(fp,"丑");break;case 3:fprintf(fp,"寅");break;case 4:fprintf(fp,"卯");break;case 5:fprintf(fp,"辰");break;case 6:fprintf(fp,"巳");break;case 7:fprintf(fp,"午");break;case 8:fprintf(fp,"未");break;case 9:fprintf(fp,"申");break;case 10:fprintf(fp,"酉");break;case 11:fprintf(fp,"戌");break;case 0:fprintf(fp,"亥");break;}fprintf(fp,"年\t");}。
(完整版)C语言万年历
说明部分
在TC或VC6.0中编译程序,编译成功后,通过命令行向编译生成的可执行文件(.exe)传递参数,在命令行中输入该执行文件的路径名称,然后再输入传入的参数,回车,就会显示出运行结果,效果如图1.1所示。
图1.1万年历运行界面
代码部分
#include <stdio.h>
main(int a,char **date)
}
if (day<0||day>month)
{
printf ("\n ERROR! the entered DAY is invalid\n");
exit (0);
}
week=(dy+dm+day-1+6)%7;
if(week<0)
week+=7;
if (day>0) /***判定查看类型***/
{
if (d==-1) year=year*10+(date[1][i]-'0');
if (d==0) month=month*10+(date[1][i]-'0');
if (d==1) day=day*10+(date[1][i]-'0');
i++;
}
if (month<1||month>12)/*若月份传入错误数字*/
else
d=(year-2000)/4-(year-2000)/100+(year-2000)/400;
dy=(year-2000)+d; /***该年1月1号到2000年1月1号的"星期差" ***/
用c语言编写万年历
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
else goto loop2;
loop1: for(i=1901;i<year;i++)
{
if(i%400==0||(i%4==0&&i%100!=0))
n++;
}
days=(year-1901-n)*365+n*366;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("一年内只能有12个月!请重新输入!\n");goto redo;
case 3:printf("\t 五月 %d\t\t 六月 %d\n",year,year);break;
case 4:printf("\t 七月 %d\t\t 八月 %d\n",year,year);break;
goto redo;
}
if(month==6&&(day>=31||day<=0))
{
printf("输入有误!6月只能有30天!\n");
goto redo;
}
if(month==7&&(day>=32||day<=0))
{
printf("输入有误!7月只能有31天!\n");
万年历c语言编程代码
#include <stdio.h>// 函数声明int is_leap_year(int year);int get_first_day_of_year(int year);// 主函数int main() {int year, month, first_day_of_year, first_day_of_month, days_in_month;int i, j, k;printf("请输入年份(例如:2022):");scanf("%d", &year);first_day_of_year = get_first_day_of_year(year);for (month = 1; month <= 12; month++) {printf("%d 年%d 月\n", year, month);printf("日一二三四五六\n");if (month == 2) {if (is_leap_year(year)) {days_in_month = 29;} else {days_in_month = 28;}} else if (month == 4 || month == 6 || month == 9 || month == 11) { days_in_month = 30;} else {days_in_month = 31;}first_day_of_month = (first_day_of_year + (month-1)*28)%7;for (i = 1; i <= first_day_of_month; i++) {printf(" ");}for (j = 1; j <= days_in_month; j++) {printf("%2d ", j);k = (i + j - 1) % 7;if (k == 0) {printf("\n");}}printf("\n");}}// 判断是否闰年int is_leap_year(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);}// 计算年份第一天是星期几int get_first_day_of_year(int year) {int first_day = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;first_day = (first_day + 1) % 7;return first_day;}这是一个简单的万年历示例代码,但它不是完整的代码。
C语言编写万年历
C语言编写万年历【要求】:1.程序运行后,首先在屏幕上显示主菜单:2.查询某年某月某日是星期几3.查询某年是否是闰年4.打印某年的全年日历5.退出2.在主菜单中输入1后,显示:“请输入年月日(XXXX,XX,XX)”运行后输出:XXXX年XX月XX日是星期X,是否继续查询(Y/N)?如果输入Y,则重新显示“请输入年月日(XXXX,XX,XX)”,否则回到主菜单。
3.在主菜单中输入2后,显示:“请输入要查哪一年?(XXXX)”运行后输出:XXXX年是(否)是闰年,是否继续查询(Y/N)?如果输入Y,则重新显示,“请输入要查哪一年?(XXXX)”,否则回到主菜单。
4.在主菜单中输入3后,显示:“请输入要打印的年份(XXXX)”运行后输出XXXX年的日历,格式为:XXXXX(月数.奇) X(月数.)0 1 2 3 4 5 6 0 1 2 3 4 5 6 S M T W T F S S M T W T F S x x x x x x x x x x xx x x xx xx xx xx x x x xx xx xx xx xx xx xx xx xx xx xx x x x xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx 运行完后显示:“是否继续打印(Y/N)?”如果输入Y,则重新显示,“请输入要打印的年份(XXXX)”,否则回到主菜单。
5.在主菜单中输入4后,显示:“是否要真的退出(Y/N)?”如果输入Y,结束程序运行,否则重新显示主菜单。
代码来自东篱下,Turbo C&C++3.0编译调试通过,已经验证输出结果正确:#include <stdio.h>#include <stdlib.h>#include <conio.h>#define X " Sun Mon Tue Wed Thu Fri Sat"void menu(){system("cls");printf("\nTHIS IS THE MENU OF THE PROGRAM !");printf("\nYOU CAN CHOOSE THE NUMBER FOR THE FUNCTIOM:"); printf("\n\n");printf("1 Find the day by year-month-date.\n");printf("2 Find the year you input if the leap year.\n");printf("3 Print the calendar of the year you input.\n");printf("4 Exit.\n\n");printf("Input your choice:");}/*函数定义,将在主函数中调用*/int getday(int year,int month,int date){int flag,s,i;int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int cont=0;flag=isleap(year);if(flag==1)/*修改二月份天数*/a[2]++;for(i=1;i<month;i++){cont=cont+a[i];}cont=cont+date;s=year+1+(year-1)/4+(year-1)/100+(year-1)/400+cont; return s%7;/*利用公式求星期几*/}int isleap(int year){if(year%4==0&&year%100||year%400==0)return 1;elsereturn 0;}/*判定闰年*/void print(int n){int i;for(i=0;i<n;i++) printf(" ");}/*打印空格,排版使用*/int day(int year){int a,b;if(year<=2000){a=2000-year;b=6-(a+a/4-a/100+a/400)%7;return b;}else{a=year-2000;b=(a+1+(a-1)/4-(a-1)/100+(a-1)/400)%7+6;return b%7;}}/*打印日历函数*/void printcalendar(int year){int i,j,k,m,n,f1,f2,d;int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};printf("\nThe calendar of the year %d.\n\n",year);d=day(year);if(isleap(year)==1)a[2]++;for(i=1;i<=12;i+=2){m=0; n=0; f1=0; f2=0;switch(i){case 1:printf(" Januray 1 ");break;case 3:printf(" March 3 ");break;case 5:printf(" May 5 ");break;case 7:printf(" July 7 "); break;case 9:printf(" September 9 ");break;case 11:printf(" Nevember 11 ");break;}print(21); /*打印空格,隔开奇偶月份名*/switch(i+1){case 2:printf(" February 2 "); break;case 4:printf(" April 4 "); break;case 6:printf(" June 6 "); break;case 8:printf(" August 8 "); break;case 10:printf(" October 10 "); break;case 12:printf(" December 12"); break;}printf("\n");printf(X); /*宏调用,打印星期字符串*/print(6);/*打印空格,隔开奇偶月份数据*/printf(X);printf("\n");for(j=0;j<6;j++){if(j==0){print(d*4); /*每个日期数采用个空格的宽度,即4字节。
C语言万年历代码
else if(ch=='n'||ch=='N')
{ month+=1;
if(month>12) {month=1;year+=1;}
}
printf("\t\t********e退出 p上一月 n下一月********");
}
void first_set(int *year,int *month)
{ do{
printf ("please input the year(1~9999):");
if (choose=='y'||choose=='Y') main();
}
days_of_month(int x,int y)
{
int z;
switch (y)
{case 1:
case 3:
case 5:
case 7:
case 8:
printf ("please input the month(1~12):");
scanf ("%d",month);
if(*month<1||*month>12) printf ("WANNING:ERROR,please input again!\n");
printf ("\n\n********************************************************************************\n\n");
自己做的日历(C语言)
本帖最后由 kekeyu 于 2010-8-15 16:59 编辑
c语言实现一个简单日历
c语⾔实现⼀个简单⽇历满⾜三个需求:1.输⼊⼀个年份,输出是在屏幕上显⽰该年的⽇历。
假定输⼊的年份在1940-2040年之间。
2.输⼊年⽉,输出该⽉的⽇历。
3.输⼊年⽉⽇,输出距今天还有多少天,星期⼏,是否是公历节⽇。
最终完善版代码:复制代码代码如下:#include<stdio.h>#include <string.h>#include <time.h>#include <math.h>#include <windows.h>int year , month , day ;int day_of_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};//char wek[7]={'周⽇','周⼀','周⼆','周三','周四','周五','周六'};int current_year,current_month,current_day;/************************************通⽤关键函数**************************************************/ void cls_screen(){printf("请按任意键返回!\n");getchar();getchar();system("cls");}void get_current_time() //获取当前时间{time_t timep;struct tm *p;time(&timep);p = gmtime(&timep);current_year=1900+p->tm_year;current_month=1+p->tm_mon;current_day=p->tm_mday;}int judgement (int y){if (y % 400 == 0 || (y % 100 !=0 && y %4 ==0))return 1 ;else return 0 ;}int show_week (int year , int month , int day){/*公式:w=(y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1)%7*/int w ,k; //记录周⼏int year_last=year %100,c=year/100 , m = month;if (month==1 ){year_last-=1 ;m=13 ;}else if (month==2){year_last-=1;m=14;}w = (year_last + year_last/4 + c/4 - 2*c +26*(m+1)/10+day-1); // abs 绝对值if (w<0){k=(w%7+7)%7;}else k=w%7;return k ;}/************************************第⼀部分**************************************************/void print_year (int year){int i , k ,x ,first_week;printf ("请输⼊想要查询的年⽉(格式如2012):");scanf ("%d",&year);printf("=======================%d年===========================\n",year);printf("\n");if (judgement(year)){day_of_month[1]=29;}else day_of_month[1]=28;for (i=1 ; i <13 ; i++){first_week = show_week(year,i,1);printf("=====================%d⽉⽇历如下========================\n",i); printf ("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");for (x=1;x<=first_week;x++){printf("\t");if (x%7==0) printf ("\n");}for (k=1;k<=day_of_month[i-1];k++){printf("%d\t",k);if (x%7==0) printf ("\n");x++;}printf("\n");printf("\n");printf("\n");}}/************************************第⼆部分**************************************************/void print_year_month (){int k ,x ,first_week;printf ("请输⼊想要查询的年⽉(格式如2012 12):");do{scanf ("%d %d",&year,&month);if (month<1||month>12){printf("您输⼊的⽉份有误哦~请输⼊正确的⽉份\n");printf ("请输⼊想要查询的年⽉(格式如2012 12):");}}while(1>month||month>12);printf("=====================%d年%d⽉======================\n",year,month); if (judgement(year)){day_of_month[1]=29;}else day_of_month[1]=28;first_week = show_week(year,month,1);printf ("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");for (x=1;x<=first_week;x++){printf("\t");if (x%7==0) printf ("\n");}for (k=1;k<=day_of_month[month-1];k++){printf("%d\t",k);if (x%7==0) printf ("\n");x++;}printf("\n");}/************************************第三部分**************************************************/int year_before_sumdays (int year,int month, int day){int days=0 ,i,judgement1;int temp_day=0 ,sum_days; ////printf ("%d,%d\n",current_year,current_month);judgement1=judgement(year);/*===================⽐当前少=====================*/if (year < current_year ){for (i = year+1;i < current_year ;i++){if (judgement(i)){days=days+356;}else days+=355;}for (i = month+1;i<=12;i++){days=days+day_of_month[i-1];}days = days + day_of_month[month-1]-day; //指定⽇⼦距离当年结束还有多少天 //printf("去年还有%d\n",days);for (i = 0;i < current_month-1;i++ ){if (judgement1){day_of_month[1]=29;}temp_day = temp_day + day_of_month[i];}//当前⽇⼦是这⼀年的多少天temp_day = temp_day + current_day;//printf("今天是第%d天\n",temp_day);sum_days=temp_day + days ;}/*===================⽐当前多=====================*/if (year > current_year ){for (i =current_year+1;i < current_year ;i++){if (judgement(i)){days=days+356;}else days+=355;}for (i = current_month+1;i<=12;i++){days=days+day_of_month[i-1];}days = days + day_of_month[month-1]-current_day; //指定⽇⼦距离当年结束还有多少天 //printf("去年还有%d\n",days);for (i = 0;i <month-1;i++ ){if (judgement1){day_of_month[1]=29;}temp_day = temp_day + day_of_month[i];}//当前⽇⼦是这⼀年的多少天temp_day = temp_day + day;//printf("今天是第%d天\n",temp_day);sum_days=temp_day + days ;}/*===================⽐当前⼀样=====================*/if (year == current_year ){if(month <current_month){for (i=month+1;i<current_month;i++){if (judgement1){day_of_month[1]=29;}days = days + day_of_month[i];}sum_days = days + current_day + day_of_month[month-1] - day ;}if (month>current_month){for (i=current_month+1;i<month;i++){if (judgement1){day_of_month[1]=29;}days = days + day_of_month[i];}sum_days = days + day + day_of_month[month-1] - current_day ;printf("%d\n",days);}if (month==current_month){sum_days= abs(day-current_day);}}return sum_days ;}void print(int year,int month,int day){int week;printf ("请输⼊想要查询的年⽉(格式如2012 12 12 ):");do{scanf ("%d %d %d",&year,&month,&day);if (judgement(year)){day_of_month[1]=29;}printf("\n");if (day<=0 || day >day_of_month[month-1])printf ("%d⽉没有%d,请重新输⼊(格式如2012 12 12 ):",month,day);}while(day<=0 || day >day_of_month[month-1]);week=show_week (year,month ,day);printf("\n");switch(month)//判断查找天是否阳历节⽇先由⽉份判断是否有节⽇的⽉份再由⽇判断是否为节⽇ {case 1:switch(day){case 1:printf("元旦") ;break;default:printf("不是阳历节⽇");}break;case 2:switch(day){case 14:printf("情⼈节(Valentines Day)");break;default:printf("不是阳历节⽇");}break;case 3:switch(day){case 8:printf("妇⼥节¨(Women's Day)");break;case 12:printf("植树节(Arbor Day)");break;default:printf("不是阳历节⽇");}break;case 4:switch(day){case 1:printf("愚⼈节(April Fools Day)");break;case 5:printf("清明节(Tomb-sweeping Day)");break;default:printf("不是阳历节⽇");}break;case 5:switch(day){case 1:printf("劳动节(Labor Day)");break;case 4:printf("中国青年节(Chinese Youth Day)");default:printf("不是阳历节⽇");}break;case 6:switch(day){case 1:printf("⼉童节(Children's Day)");break;default:printf("不是阳历节⽇");}break;case 8:switch(day){case 1:printf("建军节(the Army's Day)");break;default:printf("不是阳历节⽇");}break;case 9:switch(day){case 10:printf("教师节(Teacher's Day)");break;default:printf("不是阳历节⽇");}break;case 10:switch(day){case 1:printf("国庆节(National Day)");break;case 31:printf("万圣节(Helloween Day)");break;default:printf("不是阳历节⽇");}break;case 12:switch(day){case 25 :printf("圣诞节(Christmas Day)");break;default:printf("不是阳历节⽇");}break;}printf("\n");printf("%d年%d⽉%d号是:",year,month,day);switch(week)//判断所查找天是星期⼏{case 0:printf("Sunday");break;case 1:printf("Monday");break;case 2:printf("Tuesday");break;case 3:printf("Wednesday");;break;case 4:printf("Thursday");break;case 5:printf("Friday");break;case 6:printf("Saturday");break;}printf("\n");printf("距离今天有%d天\n",year_before_sumdays ( year, month, day));printf("\n");}/********************************************************************************************/void main (){int choice,year,month,day,flag=1;char c , k;for(;1;)//显⽰程序菜单,为永真,每次查找完回到程序菜单ì{printf("===================================菜单选项===================================\n"); printf("请选择:\n");printf("* * * * * * * * 1.查找某⼀年的年历 * * * * * * * *\n");printf("* * * * * * * * 2.查找某⼀⽉的⽉历 * * * * * * * *\n");printf("* * * * * * * * 3.查找某⼀天 * * * * * * * *\n");printf("* * * * * * * * 0.退出 * * * * * * * *\n");printf("==============================================================================\n"); printf("请输⼊您的选择: ");do{if (flag){c=getche();printf("\n");printf("确定选择%c吗? (y/Y )或者(n/N): ",c);}if (flag ){k=getche();// printf("\n");}if (k == 'y' || k == 'Y'){printf("\n");if (c=='\n'){printf("\n");printf("输⼊有误,请输⼊数字\n");printf("\n");printf("请输⼊您的选择: ");}else if (!isdigit(c)){printf("\n");printf("输⼊有误,请输⼊数字\n");printf("\n");printf("请输⼊您的选择: ");}else if (isdigit(c)){choice = c-'0' ;if (choice < 0 || choice > 3 ){printf("输⼊有误,请输⼊0-3之间的数字\n"); }else break ;}}else if (k=='n' || k=='N'){printf("\n");printf("你选择了否,请重新输⼊选项: ");flag=1;}if ((k != 'y'&& k!='Y'&& k!= 'n'&& k!='N')){printf("\n");printf("请输⼊y/n: ");flag=0;if (flag == 0){k=getche();printf("\n");//getchar();}}}while(1);if (choice==1){print_year(year);cls_screen();}else if (choice==2){print_year_month ();cls_screen();}else if (choice==3){get_current_time();print(year,month,day);cls_screen();}else if (choice==0)break;else{printf ("您的输⼊有误,请重新输⼊\n");}}}以上就是本⽂所述的全部内容了,希望⼤家能够喜欢。
C语言电子日历程序设计
C语言电子日历程序设计一、项目要求设计一款电子日历,通过选择,实现如下5项功能:选择操作:1选择操作:2选择操作:3,根据输入的年-月,显示月历。
选择操作:4,根据输入年,显示年历。
部分年历如下图所示。
选择操作:5二、编译环境 Visuall C++6.0 + EasyX(Visual C++ 6.0),采用C++程序。
1. 创建工程在Visual C++6.0中,选择“文件”—>“新建”,弹出如下界面。
先创建工程,选择“Win32 Console Application”工程,给出存储位置和工程名,点“确定”按钮。
后面选择默认,即可。
2. 在工程中创建C++文件在Visual C++6.0中,选择“文件”—>“新建”,弹出如下界面。
刚才创建工程名给出文件名,不要给扩展名,默认创建C++文件,扩展名为cpp 点“确定”按钮,完成C++文件创建。
界面效果如下图所示。
3. 添加类库因为C语言中的图形库头文件graphics.h是Dos环境下的,只能使用TurboC进行编译调试,TurboC2.0环境不是图形化可视界面,调试程序不如VC++6.0。
但在Visual C++6.0中,原有C语言中的图形库头文件graphics.h不可用,有些人员开发了DLL文件,让Visual C++6.0能识别C语言的graphics.h,方便利用C语言进行图形化的设计。
即,要进行图形化设计,需要安装EasyX,添加类库。
安装过程如下:(1)双击EasyX中的Setup文件,效果如下图所示。
单击“下一步”,选择你的开发平台,安装类库。
这里选择Visual C++6.0即可,点击“安装”,弹出安装成功,“关闭”即可。
三、相关知识1. 本项目涉及到的头文件#include <stdio.h>#include <stdlib.h>#include <graphics.h>#include <conio.h>#include <math.h>(1) graphics.h是TC里面的图形库,分为:像素函数、直线和线型函数、多边形函数、填充函数等。
用c语言编写万年历
#include<stdio.h>//假设今天是2013年12月22号,是星期天,根据这个日期计算日历中的某天距离今天有多少天,且输入的日期为星期几!查表得1800年1月1日为星期三。
int i,j,k,a,b,c,w=0;//i,j,k分别代表全局变量的年月日。
int y(){int p, h=0,inleap=0,leap=0,sum=0,count1,count2,leap1;scanf("%d",&i);if(i>2100||i<1800)printf("输入的年份不合法,或者不在程序运算的范围内");else{ printf("%d年的年历为:\n",i);for(j=1;j<=12;j++){{for(a=1800;a<=i;a++){if((a%4==0&&a%100!=0)||(a%400==0))leap++;else inleap++;}count1=leap*366;count2=inleap*365;switch(j){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;}sum=sum+count1+count2;{if(i%400==0||(i%4==0&&i%100!=0))leap1=1; //如果不是闰年则直接执行elseleap1=0;if(leap1==1&&j>2) //如果是闰年并且月份大于2,总天数应该加一天sum++;}h=(sum+2)%7;}if((i%4==0&&i%100!=0)||(i%400==0)){switch(j){case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf("\t\t%d月\n",j);printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");for(p=0;p<h;p++)printf(" ");for(c=1;c<=31;c++){printf("%4d",c);if((c+h)%7==0)printf("\n");}printf("\n");break;case 4:case 6:case 9:case 11:printf("\t\t%d月\n",j);printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");for(p=0;p<h;p++)printf(" ");for(c=1;c<=30;c++){printf("%4d",c);if((h+c)%7==0)printf("\n");}printf("\n");break;case 2:printf("\t\t%d月\n",j);printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");for(c=1;c<=29;c++){printf("%4d",c);if((c+h)%7==0)printf("\n");}printf("\n");}}else{switch(j){case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf("\t\t%d月\n",j);printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");for(p=0;p<h;p++)printf(" ");for(c=1;c<=31;c++){printf("%4d",c);if((c+h)%7==0)printf("\n");}printf("\n");break;case 4:case 6:case 9:case 11:printf("\t\t%d月\n",j);printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");for(p=0;p<h;p++)printf(" ");for(c=1;c<=30;c++){printf("%4d",c);if((h+c)%7==0)printf("\n");}printf("\n");break;case 2:printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");printf("\t\t%d月\n",j);for(p=0;p<h;p++)printf(" ");for(c=1;c<=28;c++){printf("%4d",c);if((c+h)%7==0)printf("\n");}printf("\n");break;}}}}}int ym(){int p,h=0,inleap=0,leap=0,count,sum=0,count1,count2,leap1;scanf("%d%d",&i,&j);if(i<1800||i>2100||j<1||j>12)printf("您输入的年月份不在运算范围或者不合法,请重新输入!");else{ printf("%d年%d月的月历为:\n",i,j);{for(a=1800;a<i;a++){if((a%4==0&&a%100!=0)||(a%400==0))leap++;else inleap++;}count1=leap*366;count2=inleap*365;switch(j){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;}sum=sum+count1+count2;{if(i%400==0||(i%4==0&&i%100!=0))leap1=1; //如果不是闰年则直接执行elseleap1=0;if(leap==1&&j>2) //如果是闰年并且月份大于2,总天数应该加一天sum++;}h=(sum+3)%7;}if((i%4==0&&i%100!=0)||(i%400==0)){switch(j){case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf("\t\t%d月\n",j);printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");for(p=0;p<h;p++)printf(" ");for(c=1;c<=31;c++){printf("%4d",c);if((c+h)%7==0)printf("\n");}printf("\n");break;case 4:case 6:case 9:case 11:printf("\t\t%d月\n",j);printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");for(p=0;p<h;p++)printf(" ");for(c=1;c<=30;c++){printf("%4d",c);if((h+c)%7==0)printf("\n");}printf("\n");break;case 2:printf("\t\t%d月\n",j);printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");for(c=1;c<=29;c++){printf("%4d",c);if((c+h)%7==0)printf("\n");}printf("\n");break;}}else{switch(j){case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");printf("\t\t%d月\n",j);for(p=0;p<h;p++)printf(" ");for(c=1;c<=31;c++){printf("%4d",c);if((c+h)%7==0)printf("\n");}printf("\n");break;case 4:case 6:case 9:case 11:printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");printf("\t\t%d月\n",j);for(p=0;p<h;p++)printf(" ");for(c=1;c<=30;c++){printf("%4d",c);if((h+c)%7==0)printf("\n");}printf("\n");break;case 2:printf("%4s%4s%4s%4s%4s%4s%4s\n","Sun","Mon","Tues","Wed","Thur","Fri","Sat");printf("\t\t%d月\n",j);for(p=0;p<h;p++)printf(" ");for(c=1;c<=28;c++){printf("%4d",c);if((c+h)%7==0)printf("\n");}printf("\n");}}}}int ymd(){int sum=0,leap=0,inleap=-1,leap1,count1=1,count2=1,count3=356,count4,count;//其中的lead代表是从当年到今年的闰年数,count1则为闰年的总天数,//inlead代表的是从当年日期到今年的日期的平年数,count2则为平年的总天数,count3则为今年的第N天,count4则为当年的倒数第N天。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、first.cpp文件清单#include<iostream.h>int IsLeapYear(int year) //判断是否是闰年;{if(year%4==0&&year%100!=0||year%400==0)return 1;else return 0;}2、second.cpp文件清单#include<iostream.h>#include"aaa.h"//void main(){int i,j,year,y,k,ds;int month_first[12];int month_number[12]={31,28,31,30,31,30,31,31,30,31,30,31};char month[12][20]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};cin>>year;for(k=year-1;k<=year+1;k++)//控制前后三年年份的输入;{cout<<k;ds=0;if(year>1990)for(y=1990;y<k;y++){if(IsLeapYear(y)==1){ ds+=366;}else ds+=365;//计算从基准点(1990年1月1日)开始至所求年份前一年的天数;}Else for(y=1990;y>k;y--){if(IsLeapYear(y)==1){ ds+=366;}else ds+=365;//计算从基准点(1990年1月1日)开始至所求年份前一年的天数;}for(i=0;i<12;i++){if(IsLeapYear(k)==1){month_number[1]=29;}else month_number[1]=28;//闰年二月为29天;if(i==0){ds+=1;month_first[0]=ds%7;}else{ds+=month_number[i-1];month_first[i]=ds%7;}//输出年份的每月第一天为星期几;switch(month_first[i]){case 0:cout<<"\n"<<month[i]<<endl;cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;for(j=1;j<=month_number[i];j++){if((j+7)%7!=0)cout<<j<<"\t";else cout<<j<<endl;};break;case 1:cout<<"\n"<<month[i]<<endl;cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;cout<<" \t";for(j=1;j<=month_number[i];j++){if((j+7)%7!=6)cout<<j<<"\t";else cout<<j<<endl;};break;case 2:cout<<"\n"<<month[i]<<endl;cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;cout<<" \t"<<" \t";for(j=1;j<=month_number[i];j++){if((j+7)%7!=5)cout<<j<<"\t";else cout<<j<<endl;};break;case 3:cout<<"\n"<<month[i]<<endl;cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;cout<<" \t"<<" \t"<<" \t";for(j=1;j<=month_number[i];j++){if((j+7)%7!=4)cout<<j<<"\t";else cout<<j<<endl;};break;case 4:cout<<"\n"<<month[i]<<endl;cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;cout<<" \t"<<" \t"<<" \t"<<" \t";for(j=1;j<=month_number[i];j++){if((j+7)%7!=3)cout<<j<<"\t";else cout<<j<<endl;};break;case 5:cout<<"\n"<<month[i]<<endl;cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;cout<<" \t"<<" \t"<<" \t"<<" \t"<<" \t";for(j=1;j<=month_number[i];j++){if((j+7)%7!=2)cout<<j<<"\t";else cout<<j<<endl;};break;case 6:cout<<"\n"<<month[i]<<endl;cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;cout<<" \t"<<" \t"<<" \t"<<" \t"<<" \t"<<" \t";for(j=1;j<=month_number[i];j++){if((j+7)%7!=1)cout<<j<<"\t";else cout<<j<<endl;};break;}//控制每月日期与星期对应输出;}cout<<endl;}}3、all.h文件清单int IsLeapYear(int year); //判断闰年函数说明;。