用C语言编写万年历
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"int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};int judge(int year,int month){if(month==1||month==3||month==5||month==7||month==8||month==10||month ==12)return(1);else if(month==2){if(year%4!=0||year%100==0&&year%400!=0)return(2);else return(3);}else return(4);}void show2(){int year,i,j,a,n,m,k;char ** p;char * week[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};char*month[]={"January","February","March","April","May","June","July","Augu st","September","October","November","December"};printf("please inter the year:");scanf("%d",&year);printf("\n");printf("the calendar of the year%d.",year);printf("\n");a=(year+(year-1)/4-(year-1)/100+(year-1)/400)%7;for(i=0;i<12;i++){n=judge(year,i+1);p=month+i;printf("%s\n",*p);printf("\n");for(j=0;j<7;j++){p=week+j;printf("%6s",*p);}printf("\n");for(k=0;k<a;k++)printf(" ");for(m=1;m<32;m++){printf("%6d",m);if((a+m)%7==0)printf("\n");if(n==1&&m==31) break;else if(n==2&&m==28) break;else if(n==3&&m==29) break;else if(n==4&&m==30) break;}a=(a+m)%7;/*计算出下个月的第一天是星期几*/printf("\n");printf("**************************************************");printf("\n");}}void show(){printf("******************************\n");printf("1.某年某月某日是星期几\n2.是否闰年\n3.某年某月的最大天数\n4.打印该月日历\n5.退出\n");printf("******************************\n");}int show1(int y,int m,int d){int i,R,sum=0;for(i=0;i<m-1;i++)sum=sum+mon_day[i];sum=sum+d;if((y%4 == 0 &&y%100 != 0)||(y%400 == 0)&&m>2)sum=sum+1;R=(5+(y+1)+(y-1)/4-(y-1)/100+(y-1)/400+sum)%7;if(R==0)R=7;return R;}int leap(int y){if((y%4 == 0 &&y%100 != 0)||(y%400 == 0))return 1;elsereturn 0;}int max_day(int y,int m){int X;if(leap(y)==1&&m==2)X=29 ;//printf("该年该月最大天数:29");elseX=mon_day[m-1];//printf("%d",mon_day[m-1]);return X;}void main(){int year,month,day,i;F:show();printf("请选择:");scanf("%d",&i);switch(i){case 1:printf("请输入年、月,日:");scanf("%d%d%d",&year,&month,&day);printf("%d-%d-%d是星期%d\n",year,month,day,show1(year,month,day));break;case 2:printf("请输入年份:");scanf("%d",&year);if(leap(year)==1)printf("该年是闰年.\n");elseprintf("该年不是闰年\n");break;case 3:printf("请输入年、月:");scanf("%d%d",&year,&month);printf("该月最大天数:%d",max_day(year,month)); printf("\n");break;case 4:show2();break;case 5:return 0;break;}goto F;}。
万年历——C语言编写
万年历的制作,C语言编写!!以下是代码:#include<iostream>#include<cmath>using namespace std;int i,c,day,year,k,a[50];void f1(int x) //大月{for( i=0;i<50;i++)a[i]=33;for(i=k-1;i>=0;i--)a[i]='\000'; //1 号前均为空格for(i=k;i<31+k;i++)a[i]=a[i-1]+1;}void f2(int x) //小月{for( i=0;i<50;i++)a[i]=33;for(i=k-1;i>=0;i--)a[i]='\000';for(i=k;i<30+k;i++)a[i]=a[i-1]+1;}void f3(int x) //闰年二月{for( i=0;i<50;i++)a[i]=33;for(i=k-1;i>=0;i--)a[i]='\000';for(i=k;i<29+k;i++)a[i]=a[i-1]+1;}void f4(int x) //非闰年二月{for( i=0;i<50;i++)a[i]=33;for(i=k-1;i>=0;i--)a[i]='\000';for(i=k;i<28+k;i++)a[i]=a[i-1]+1;}void g(int y[],int z) // 输出日历表{cout<<" 日"<<'\t'<<" 一"<<'\t'<<" 二"<<'\t'<<" 三"<<'\t'<<" 四"<<'\t'<<" 五"<<'\t'<<"六"<<endl;for(i=0;i<50;i++){if(a[i]==0)cout<<(char)a[i]<<'\t';if(a[i]!=0&&a[i]!=33)cout<<a[i]<<'\t';else ;if(i%7==0)cout<<endl;}cout<<endl<<endl<<endl;}int main(){int s,count=0;cout<<"请输入年份:";cin>>year;cout<<endl;c=year;for(s=1;s<=year;s++)if(((s-1)%4==0&&(s-1)%100!=0)||(s-1)%400==0)count++; day=365*c+count; //相差天数cout<<"*********************"<<year<<"年日历*********************";cout<<endl<<endl<<endl<<endl;k=(day%7+6)%7+1;cout<<"一月"<<endl<<endl;f1(k);g(a,50);if((year%4==0&&year%100!=0)||year%400==0){k=((day+31)%7+6)%7+1;cout<<"二月"<<endl<<endl;f3(k);g(a,50);day=day+31+29;}else //非闰年二月{k=((day+31)%7+6)%7+1;cout<<"二月"<<endl<<endl;f4(k);g(a,50);day=day+31+28;}k=(day%7+6)%7+1;cout<<"三月"<<endl<<endl;f1(k);g(a,50);k=((day+31)%7+6)%7+1;cout<<"四月"<<endl<<endl;f2(k);g(a,50);k=((day+31+30)%7+6)%7+1;cout<<"五月"<<endl<<endl;f1(k); g(a,50);k=((day+31+30+31)%7+6)%7+1;cout<<"六月"<<endl<<endl;f2(k);g(a,50);k=((day+31+30+31+30)%7+6)%7+1;cout<<"七月"<<endl<<endl;f1(k);g(a,50);k=((day+31+30+31+30+31)%7+6)%7+1;cout<<"八月"<<endl<<endl;f1(k);g(a,50);k=((day+31+30+31+30+31+31)%7+6)%7+1;cout<<"九月"<<endl<<endl;f2(k);g(a,50);k=((day+31+30+31+30+31+31+30)%7+6)%7+1;cout<<"十月"<<endl<<endl;f1(k);g(a,50);k=((day+31+30+31+30+31+31+30+31)%7+6)%7+1;cout<<"十一月"<<endl<<endl; f2(k);g(a,50);k=((day+31+30+31+30+31+31+30+31+30)%7+6)%7+1;cout<<"十二月"<<endl<<endl;f1(k);g(a,50);return 0;}。
c语言万年历
c语言万年历#include<stdio.h>main(){for(;;){int nian,yue,a,b,m,c,_m=28;long e;printf("请输入年份:");scanf("%d",&nian);e=nian;e=e-1;e=(e*365+e/4)%7;m=e;if((nian%4==0)||(nian%100==0)&&(nian%400!=0)) //求出nian是否为闰年//m++;printf("%d的日历:",nian);for(yue=1;yue<=12;yue++){if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12){c=0;printf("\n");printf(" %d月\n",yue);printf(" 日一二三四五六\n");for(b=1;b<=m;b++){c++;printf(" ");}for(a=1;a<=31;a++){c++;if(c%7-1==0&&c!=1)printf("\n");if(a<10)printf(" ");printf(" %d ",a);m++;if(m==7)m=0;}printf("\n");}else{if(yue==4||yue==6||yue==9||yue==11||yue==2){if(yue==2){c=0;printf("\n");printf(" %d月\n",yue);printf(" 日一二三四五六\n");for(b=1;b<=m;b++){c++;printf(" ");}for(a=1;a<=_m;a++){c++;if(c%7-1==0&&c!=1)printf("\n");if(a<10)printf(" ");printf(" %d ",a);m++;if(m==7)m=0;}printf("\n");}else{c=0;printf("\n");printf(" %d月\n",yue);printf(" 日一二三四五六\n");for(b=1;b<=m;b++){c++;printf(" ");}for(a=1;a<=30;a++){c++;if(c%7-1==0&&c!=1)printf("\n");if(a<10)printf(" ");printf(" %d ",a);m++;if(m==7)m=0;}printf("\n");} } }} } }。
(完整版)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语言写的万年历
}
}
unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; //相当于汇编中的 RRC
DS1302_CLK = 1;
DS1302_RST = 0;
}
unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据
{
unsigned char ucData;
DS1302_RST = 0;
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0; //相当于汇编中的 RRC
DS1302_CLK = 1;
DS1302_CLK = 0; //上升沿沿写入数据
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
{ if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为 2007/07/22
万年历c语言程序
万年历-c语言程序#include; sbit p1_0=P1^0; sbit p1_1=P1^1; sbitp1_2=P1^2; sbit p1_3=P1^3; sbit p1_4=P1^4; sbit p3_2=P3^2; unsigned char code zimal[13]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6 F,0x40,0x80,0x00,}; /*共阴极字形代码,显示0-9字形码,最后三个代码分别表示‘-’,‘.’‘熄灭’*/ unsigned int year,month,day,hour,minute,second;//定义变量年,月,日,时,分,秒; void delay();//延时函数; void xianshi(unsigned int h,min,s);//显示时间函数; voidymdxianshi(unsigned int year,month,day);//显示年月日; void fengming();//蜂鸣器函数; int leap(unsigned int year);//判断闰年函数; unsigned int h,m,s=5;//记录定时时间; unsigned int mode=1,td=0,yw=0;//状态; voidmain(){ year=2010;month=5;day=20;hour=23;minute=59;second=0; /*初始化年,月,日,时,分,秒*/TMOD=0x10;//定时器1;模式1; TL1=0xb0;TH1=0x3c;定时时间为50msTR1=1;//打开定时器1ET1=1;//定时器1溢出中断允许位EX0=1;//外部中断0允许IT0=1;//外部中断0触发方式为边沿触发EA=1; while(1) //主循环;{if(hour==h&&minute==m&&second==s) fengming(); //定时时间到,蜂鸣器响;if(mode%2==0) ymdxianshi(year,month,day); //显示年月日 else{if(td%3==2)xianshi(h,m,s); // 定时状态;elsexianshi(hour,minute,second); //正常显示时间状态;}}void delay()//延时函数;{ unsigned int i; for(i=0;i;=2500)year=0; //年份小于2500;if(month>;12)month=1;//月份小于12;switch(month)//日期限制{case 4:case 6:case 9:case 11:if(day>;30)day=1;break;//4,6,9,11月30天;case 2 :(leap(year))//闰年2月29天;while(day>;29)day=1;elsewhile(day>;28)day=1;//非闰年28天;}break; default:if(day>;31)day=1; //1,3,5,7,8,10,12月31天;}}}//else if(td%3==1) //调整时间; else if(td%3==1) { ET1=0; //时钟中断 for(i=0;i;23)hour=0;if(minute>;59)minute=0; if(second>;59)second=0;} else//定时状态;ET1=1;if(td%3==2){for(i=0;i;23)h=0;if(m>;59)m=0;if(s>;59)s=0;}}} voidtime1()interrupt 3 //定时器1中断; { unsigned char i;TF0=0; TL0=0xb0; TH0=0x3c; i++;if(i==20) //1秒钟到;second++;i=0;}if(second==60)//1分钟;{second=0;minute++;}if(minute==60)//1小时; {minute=0; hour++; } if(hour==24) //1天;{hour=0;day++;}switch(month) //1 月;case 4:case 6:case 9:case11:if(day>;30){month++;day=1;}break;//4,6,9,11月30天;case 2:{if(leap(year))if(day>;29){month++;day=1;} //闰年29天; elseif(day>;28){month++;day=1;}//非半闰年28天;}break;default:if(day>;31){month++;day=1;}//1,3,5,7,8,10 ,12月30天;if(month>;12) //1年;{year++;month=1;}} } void fengming() //蜂鸣器; { int m,n; for (m=0;m<=100;m++) {p1_4=!p1_4;for(n=0;n<380;n++); } }int leap(unsigned int year) //判断闰年函数; { int leap; if(year%4==0&&year%100!=0||(year%400==0)) leap=1; else leap=0;return(leap); }。
万年历C语言代码
//ÍòÄêÀú#include <stdio.h>//±íÍ·void print_head(int x,int y){printf("\n\n************************************************************ ********************\n\n");printf ("\t\t\t\t%d %d\n\n",x,y);printf ("\t\t\t SUN MON TUE WED THU FRI SAT\n");printf ("\t\t\t");}//ÈÕÀúµÄÖмävoid print_month(int x,int y){int i;char space[7]={' '};for (i=1;i<=x;i++){printf ("%4c",space[i]);}for (i=1;i<=y;i++){if ((i+x)%7==1){printf ("\n\t\t\t%4d",i);}else{printf ("%4d",i);}}printf("\n\n************************************************************ ********************\n\n");}//¼ÆËãÖ¸¶¨ÄêÔµÄÌìÊýint days_of_month(int x,int y){int z;switch (y){case 1:case 3:case 5:case 7:case 8:case 10:case 12: z=31;break;case 4:case 6:case 9:case 11: z=30;break;case 2:{if ((x%4==0&&x%100!=0)||(x%400==0)){z=29;}else{z=28;}break;}}return z;} //¼ÆËãÖ¸¶¨ÄêÔÂ1ºÅÊÇÐÇÆÚ¼¸int leap(int x,int y){int z,i,moday;z=(x+(x-1)/4-(x-1)/100+(x-1)/400)%7;for (i=1;i<y;i++){moday=days_of_month(x,i); //ǯÌ×µ÷Óú¯Êýz=(z+moday)%7;}return z;}void main(){do{int days,year,month,firstday;char choose;do{printf ("\n\nÇëÊäÈëÐèÒª²éѯµÄÄê·Ý(0000~9999):\n\n");scanf ("%d",&year);if (year<0||year>9999){printf ("WANNING:ERROR,ÇëÖØÐÂÊäÈë!");}}while (year<0||year>9999);printf ("\n\n");do{printf ("ÇëÊäÈëÐèÒª²éѯµÄÔ·Ý(0~12)\n\n\n");scanf ("%d",&month);if (month<=0||month>12){printf ("WANNING:ERROR,ÇëÖØÐÂÊäÈë!");}} while (month<=0||month>12);printf ("\n\n");days=days_of_month(year,month); //µ÷Óú¯Êýfirstday=leap(year,month);print_head(year,month);print_month(firstday,days);choose=getchar();printf ("\n\n\n");printf("ÇëÎÊÊÇ·ñ¼ÌÐø£¿(y/n):\n\n");scanf("%c",&choose);if (choose=='n'||choose=='N'){break;}}while(1<2);}。
万年历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语言代码#include <stdio.h>main(){for(;;){int year,month,a,b,c,m,n=28;/*定义变量*/long e;printf("请输入年份");scanf("%d",&year);e=year;e--; /*前一年*/e=(e*365+e/4)%7; /*计算这一年的第一天是星期几*/m=e; /*把第一天的日期赋给m*/if((year%4==0)||(year%100!=0)&&(year%400!=0))/*如果是闰年*/n++;printf("%d的日历:",year);for(month=1;month<=12;month++){if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)/*这几个月每个月都是31天*/{c=0;printf("\n");printf(" %d月\n",month);printf(" 日一二三四五六\n");for(b=1;b<=m;b++){c++;printf(" ");/*从星期日开始到这个月开始的那天前都是空的*/}for(a=1;a<=31;a++){c++;if(c%7-1==0&&c!=1)/*判断一行是否已排满,满了就要换行*/printf("\n");if(a<10) /*如果是小于10,空一格,大于10,前后空*/printf(" ");printf(" %d ",a);m++;if(m==7)/*一行排满,开始换行。
(指的是这个月的最后一行)*/m=0;}printf("\n");}else{if(month==4||month==6||month==9||month==11||month==2) {if(month==2)/*判断2月份*/{c=0;printf("\n");printf(" %d月\n",month);printf(" ");printf(" 日一二三四五六\n");for(b=1;b<=m;b++){c++;printf(" ");}for(a=1;a<=n;a++){c++;if(c%7-1==0&&c!=1)printf("\n");if(a<10)printf(" ");printf(" %d ",a);m++;if(m==7)m=0;}printf("\n");}else{c=0;printf("\n");printf(" %d月\n",month);printf(" 日一二三四五六\n");for(b=1;b<=m;b++){c++;printf(" ");}for(a=1;a<=30;a++){c++;if(c%7-1==0&&c!=1)printf("\n");if(a<10)printf(" ");printf(" %d ",a);m++;if(m==7)m=0;}printf("\n");}}}}}}。
新建 万年历c语言
#include"stdio.h"#include"stdlib.h"main( ){int Year,Month,Day; //年、月int FirstDay_Year,FirstDay_Month; //某年的第一天是星期几,某年某月的第一天是星期几(范围是0~6,其中0代表星期日)int IsLeapYear; //是否为闰年,0表示不是闰年,1表示是闰年int i,d,y,A,B,C,Y,M,D; //临时变量int riqi(int Year,int Month,int Day);//阳历转化为阴历的函数int yuefen(int Year,int Month,int Day);int nianfen(int Year,int Month,int Day);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(" 万年历\n"); //打印标题XunHuan: //循环标号(可以通过goto跳转到这里)printf("请输入年份: "); //提示输入年份(0~9999)scanf("%d",&Year); //把输入的年份赋值给变量Yearprintf("请输入月份: "); //提示输入月份(1~12)scanf("%d",&Month); //把输入的月份赋值给变量Monthprintf("请输入日期: "); //提示输入日期scanf("%d",&Day); //把输入的日期赋值给变量Dayy=Year;FirstDay_Year=5*(y/4)+(y%4)-(y/100)+(y/400);//蔡勒公式(计算某年的第一天是星期几) IsLeapYear=(((y%4==0&&y%100!=0)||y%400==0) && y!=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(" 公元%d 年%2d 月%2d日",Year,Month,Day); //打印年月A=nianfen(Year,Month,Day);B=yuefen(Year,Month,Day);C=riqi(Year,Month,Day);printf(" 农历%d 年%2d月%2d日",A,B,C);printf("\n**********************************************************\n");printf(" 星期日星期一星期二星期三星期四星期五星期六\n");//打印星期表头for(i=0;i<FirstDay_Month;i++)printf("%8c",' '); //当某月的第一天不是星期日时打印空格占位for(d=1;d<=Days[Month];d++) //循环,从每个月的第一天开始打印{if(d==Day){printf("%4c",' '); //显示被输入的日期if(Month==1&&Day==1){printf("\b\b元旦节");}else if(Month==2&&Day==14){printf("\b\b情人节");}else if(Month==3&&Day==8){printf("\b\b妇女节");}else if(Month==4&&Day==1){printf("\b\b愚人节");}else if(Month==4&&Day==5){printf("\b\b清明节");}else if(Month==5&&Day==1){printf("\b\b劳动节");}else if(Month==6&&Day==1){printf("\b\b儿童节");}else if(Month==8&&Day==1){printf("\b\b建军节");}else if(Month==10&&Day==1) {printf("\b\b国庆节");}else if(Month==10&&Day==5) {printf("\b\b教师节");}else{printf("[%2d]",d);}if(B==1&&C==1){printf("\b\b\b\b春节");}else if(B==1&&C==5){printf("\b\b\b\b\b\b元宵节"); }else if(B==5&&C==5){printf("\b\b\b\b\b\b端午节"); }else if(B==7&&C==7){printf("\b\b\b\b\b\b乞巧节"); }else if(B==8&&C==15){printf("\b\b\b\b\b\b中秋节"); }else if(B==9&&C==9){printf("\b\b\b\b\b\b重阳节"); }else if(B==12&&C==8){printf("\b\b\b\b\b\b腊八节"); }}else{printf("%6c",' ');printf("%2d",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;}int riqi(int Year,int Month,int Day){int q,r,t,i,a,s=0;int IsLeapYear;int Days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};IsLeapYear=(((Year%4==0&&Year%100!=0)||Year%400==0) && Year!=0)?1:0;Days[2]=(IsLeapYear==1)?29:28;q=(Year-1977)/4;r=(Year-1977)-4*q;for(i=0;i<Month;i++){s=s+Days[i];}t=(int)(14*q+10.6*(r+1)+s+Day)/29.5;a=(int)((14*q+10.6*(r+1)+s+Day)-t*29.5);while(Month==1){Year--;Month=Month+12;}Month=Month-1;return a;}int yuefen(int Year,int Month,int Day){int q,r,t,i,a,s=0;int Days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};q=(Year-1977)/4;r=(Year-1977)-4*q;for(i=1;i<Month;i++){s=s+Days[i];}t=(int)(14*q+10.6*(r+1)+s+Day)/29.5;a=(int)(14*q+10.6*(r+1)+s+Day)-t*29.5;while(Month==1){if(Day<23){Year=Year-1;Month=Month+12;}else{Month=Month+1;} }Month=Month-1;return Month;}int nianfen(int Year,int Month,int Day){int q,r,t,i,a,s=0;int Days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};q=(Year-1977)/4;r=(Year-1977)-4*q;for(i=1;i<Month;i++){s=s+Days[i];}t=(int)(14*q+10.6*(r+1)+s+Day)/29.5;a=(int)(14*q+10.6*(r+1)+s+Day)-t*29.5;while(Month==1&&Day<25){Year--;Month=Month+12;}Month=Month-1;return Year;}。
用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天。
C语言万年历
源代码:完美运行#include<stdio.h>#include<time.h>#include<stdlib.h>int judgeyear (int year){if(year%4==0&&year%100!=0||year%400==0)return 366;elsereturn 365;}int days_month (int month,int year){/*1,3,5,7,8,10,12每月31天*/if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 31;/*4,6,9,11每月为30天*/if(month==4||month==6||month==9||month==11)return 30;/*2月判断是28还是29天*/if(month==2&&judgeyear(year)==366)return 29;elsereturn 28;}//计算用户输入的年份和今年相隔多少天int distance_inputyear_nowyear(int inputyear,int nowyear){int day_distance=0;if(inputyear<nowyear){for(int i=inputyear;i<nowyear;i++){if(judgeyear(i)==366)day_distance+=366;elseday_distance+=365;}}else if(inputyear>nowyear){for(int i=nowyear;i<inputyear;i++){if(judgeyear(i)==366)day_distance+=366;elseday_distance+=365;}}elseday_distance=0;// printf("%d年的第一天和今年第一天相隔%d天\n",inputyear,day_distance);return day_distance;}//获取今年第一天是星期几void firstday_weekday_nowyear(){int weekday_nowyear_firstday;//今年第一天星期几time_t tval;struct tm *now;tval = time(NULL);now = localtime(&tval);if(now->tm_yday%7<now->tm_wday)weekday_nowyear_firstday=(now->tm_wday-(now->tm_yday%7));elseweekday_nowyear_firstday=((now->tm_wday+7)-(now->tm_yday%7));// printf("%d",now->tm_yday);//今年已经过了多少天// printf("%d",now->tm_wday);//今天是星期几printf("今年第一天是星期%d\n",weekday_nowyear_firstday);}//获取用户输入的年份的第一天是星期几int firstday_weekday_inputyear(int inputyear){int weekday_inputyear_firstday;//输入年份第一天星期几time_t tval;struct tm *now;tval = time(NULL);now = localtime(&tval);if(inputyear<now->tm_year+1900){if(distance_inputyear_nowyear(inputyear,now->tm_year+1900)%7<now->tm_wday)weekday_inputyear_firstday=(now->tm_wday-distance_inputyear_nowyear(inputyear,now->tm_ year+1900)%7);elseweekday_inputyear_firstday=(now->tm_wday+7)-distance_inputyear_nowyear(inputyear,now->t m_year+1900)%7;}elseweekday_inputyear_firstday=(now->tm_wday+distance_inputyear_nowyear(inputyear,now->tm_ year+1900)%7)%7;// printf("%d",now->tm_yday);//今年已经过了多少天// printf("%d",now->tm_wday);//今天是星期几// printf("%d年第一天是星期%d\n",inputyear,weekday_inputyear_firstday);return weekday_inputyear_firstday;}//计算用户输入年份的每个月份的第一天是星期几int weekday_firstday_inputyear_month(int inputyear,int month)//month>=1{int distance_day_month=0;int weekday_month=0;switch(month){case 1:return firstday_weekday_inputyear(inputyear);break;case 2:distance_day_month=31;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 3:distance_day_month=days_month(2,inputyear)+31;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 4:distance_day_month=days_month(2,inputyear)+62;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 5:distance_day_month=days_month(2,inputyear)+92;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 6:distance_day_month=days_month(2,inputyear)+123;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 7:distance_day_month=days_month(2,inputyear)+153;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 8:distance_day_month=days_month(2,inputyear)+184;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 9:distance_day_month=days_month(2,inputyear)+215;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 10:distance_day_month=days_month(2,inputyear)+245;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 11:distance_day_month=days_month(2,inputyear)+276;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;case 12:distance_day_month=days_month(2,inputyear)+306;weekday_month=(firstday_weekday_inputyear(inputyear)+distance_day_month%7)%7;break;}if(weekday_month==0)return 7;return weekday_month;}void print(int year,int month){int outputNo=0; //用作记录打印出来的数字个数printf(" Sun Mon Tue Wed Thu Fri Sat \n");switch(weekday_firstday_inputyear_month(year,month)){case 7: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;}for(int i=1;i<=days_month(month,year);i++){printf("%4d",i);outputNo++;if(i!=7&&outputNo%7==0)printf("\n");if(i<=7&&(i+weekday_firstday_inputyear_month(year,month))%7==0) {printf("\n");outputNo=0;}}}int main(){int year,month,isyear=0,noyear=0,a;time_t tval;struct tm *now;tval = time(NULL);now = localtime(&tval);printf(" ******************************\n");printf(" ** 欢迎使用万年历**\n");printf(" ******************************\n");printf(" ** 制作人:秦盛伟**\n");printf(" ******************************\n");printf("请输入年份:");scanf("%d",&year);printf("请输入月份:");scanf("%d",&month);print(year,month);//打印日历printf("\n");for(int i=0;i<10;i++){printf("\n*******************************************\n");printf("是否需要再次查询?\n");printf("请输入你的选择:(1:是/2:否)");scanf("%d",&a);printf("\n*******************************************\n");if(a==1){printf("请输入年份:");scanf("%d",&year);printf("请输入月份:");scanf("%d",&month);print(year,month);}elseexit(0);}getchar();}。
C语言-万年历
}
week=(year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400+d;//判断这个月的第一天是星期几
week=week%7;
printf("\n\n\t这是%d年的%d月的日历\n",year,month);
Today(year,month,day);
Tomonth(year,month);
printf("\n\t\t输 入 Y 或 者 y 键 继 续。。。");
for(;getch()=='y'||getch()=='Y';)
{
printf("\n\n 请 输 入 您 所 想 要 的 功 能:\n");
scanf("%d",&x);
switch(x)
{
case 1:
{
system("cls");
search();
printf("\n\t\t 按 Y 或 者 y 继 续 ...");
break;
}
case 2:
{
system("cls");
case 3:printf("\t%d 年%d 月%d 日 是 星 期: 三",year,month,day);break;
case 4:printf("\t%d 年%d 月%d 日 是 星 期: 四",year,month,day);break;
case 5:printf("\t%d 年%d 月%d 日 是 星 期: 五",year,month,day);break;
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#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天。