c语言输出日历,月历

合集下载

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语言万年历程序

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语言年月日输出格式

c语言年月日输出格式

在C语言中,可以使用`strftime`函数来格式化日期和时间。

以下是一个示例,展示如何将当前日期和时间以年月日的格式输出:
```c
#include <stdio.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("当前的本地时间和日期: %d-%02d-%02d\n", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday);
return 0;
}
```
在这个例子中,我们首先获取当前时间(`time(&rawtime)`),然后将其转换为本地时间(`localtime(&rawtime)`)。

`structtm`结构体中
的`tm_year`,`tm_mon`和`tm_mday`字段分别代表年、月和日。

我们可以通过加1900来获取完整的年份(因为`tm_year`是从1900年开始计数的),通过加1来获取正确的月份(因为`tm_mon`是从0开始计数的)。

最后,我们使用`printf`函数来输出格式化的日期。

c语言:万年历

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语言实训--万年历源代码

C语言实训--万年历源代码

C语言实训--万年历源代码# include#include#include#include#include# define YEAR 365int numday[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31}};int leap(int year);int calday(int y,int m);int counterdays(int year,int month,int day);int week(int year,int month,int day);int eachyeartotal(int year,int month,int day);int maxDay(int y,int month);void print(int y,int m,int w);void festival (int year,int month,int day);int IsLeap(int y);int guanli(int i);void rili(int year,int month);int rili1();void rili2();void rili3();void rili4();void rili5();void fan();void show(int year,int month,int date,int sec,int mini,int hour);int main(){int i;printf("***************************************\n"); printf("1.查询日历,输入年份:\n");printf("2.查询该月日历及对应星期,输入年份和月份:\n"); printf("3.查询星期几及节日,输入年月日:\n");printf("4.距您输入日期还有多少天,输入日期:\n"); printf("5.通过键盘上下控制不同年月日:\n");scanf("%d",&i);guanli(i);return 0;}int guanli(int i){switch(i){case 1:rili1();return main();case 2:rili2();return main();case 3:rili3();return main();case 4:rili4();return main();case 5:rili5();return main();}return 0;}int rili1(){int year;int n,i;printf("输入年份:");scanf("%d",&year);printf("the calendar of the year%d\n",year);printf("\n");printf("=============================\n"); for(i=1;i<13;i++){printf(" %d月份\n",i);rili(year,i);}n = leap(year);return 0;}void rili2(){int year,month,w;printf("输入年月:");scanf("%d%d",&year,&month);w=calday(year,month);print(year,month,w);}void rili3(){int year,month,day,number;printf("输入年月日:");scanf("%d%d%d",&year,&month,&day);festival(year,month,day);number= week(year,month,day);printf("星期%d",number);}void rili4(){int year,month,day;printf("输入年月日:");scanf("%d%d%d",&year,&month,&day); eachyeartotal( year,month,day);counterdays(year,month,day);}void rili5(){int year,month,date,sec,mini,hour;fan();show(year,month,date,sec,mini,hour);}int leap(int year){int n=(year%4==0 && year%100!=0 || year%100!=0); return n;}void rili(int year,int month){int i,day,first_day,flag,year1,year2;int temp = 6;flag =leap(year);year1=year/100;year2=year%100;if(year>2000){year1=year1-20;day = year1/4;day+=(YEAR+24)*year1+YEAR*year2+(year2-1)/4;if(year>2000)day++;switch(month){case 1:day; break;case 2:day+=31;break;case 3:day+=59+flag;break;case 4:day+=90+flag;break;case 5:day+=120+flag;break;case 6:day+=151+flag;break;case 7:day+=181+flag;break;case 8:day+=212+flag;break;case 9:day+=243+flag;break;case 10:day+=273+flag;break;case 11:day+=304+flag;break;case 12:day+=334+flag;break;}day+=temp;first_day=day%7;}else{year1 = 20-1-year;year2=100-year2-1;day+=(YEAR+24)*year1+YEAR*year2+year2/4; switch(month){case 12:day+=31;break;case 11:day+=61;break;case 10:day+=92;break;case 9:day+=122;break;case 8:day+=153;break;case 7:day+=184;break; case 6:day+=214;break; case 5:day+=245;break; case 4:day+=275;break; case 3:day+=306;break; case 2:day+=334+flag;break; case 1:day+=365+flag;break; }day-=temp;day = day%7;first_day=(7-day)%7;}switch(month){case 1:case 2:month=28+flag;break; case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:month=30;break; case 12:month =31;break;}for(i=1;i<4*first_day;i++) printf(" ");for(i=0;i<month;i++)< p="">{printf("%4d",i+1);if((first_day+i)%7==6)printf("\n");}printf("\n");printf("===============================\n");}///////////////////////////////////////////////////////////int counterdays(int year,int month,int day){int year1,days0,days1,month1,days,day1;time_t timep;struct tm*p;time(&timep);p=gmtime(&timep);year1=(1900+p->tm_year);month1=(1+p->tm_mon);day1=(p->tm_mday);days0=(year-1)*365 + (year-1)/4 - (year-1)/100 + (year-1)/400 +eachyeartotal (year,month,day);days1=(year1 - 1)*365+(year1-1)/4-(year1-1)/100 + (year1 - 1)/400 +eachyeartotal (year1,month1,day1);days=days0-days1;printf("\n今天距%d年%d月%d日还有%d天\n\n",year,month,day,days);return days;}int eachyeartotal(int year,int month,int day){int i,sum=0;for(i=1;i<month;i++)< p="">{sum+=maxDay(year,i);}sum+=day;return sum;}int IsLeap(int y){if(y%4==0 && y%100!=0 || y%400==0) return 1; else return 0;}int maxDay(int y,int month){int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};if(IsLeap(y))a[1]=29;return a[month-1];}///////////////////////////////////////void festival (int year,int month,int day){if (month==1 && day==1) printf (",这天是元旦");if (month==2 && day==14) printf (",这天是情人节"); if (month == 3)switch(day){case 8:printf (",这天是妇女节"); break;case 12:printf (",这天是植树节");}if (month==1 && day==1) printf (",这天是愚人节");if(month == 5)switch (day){case 1:printf(",这天是劳动节");break;case 4:printf(",这天是青年节");}if (month==6 && day==1) printf (",这天是儿童节");if (month==7 && day==1) printf (",这天是建党节");if (month==8 && day==1) printf (",这天是建军节");if (month==9 && day==10) printf (",这天是教师节");if (month==10 && day==1) printf (",这天是国庆节");printf("\n");}int week(int year,int month,int day){int number = (year-1)+(year-1)/4 - (year-1)/100+(year-1)/400+eachyeartotal(year,month,day);number = number%7;return number;}/////////////////////////////////////////////void print(int y,int m,int w){int i,l;printf("Calendar %d-%02d\n",y,m);printf("---------------------\n");printf("Su Mo Tu We Th Fr Sa \n");printf("---------------------\n");for (i=0;i<w;i++)< p=""> printf(" ");if (IsLeap(y))l=1;elsel=0;for (i=1;i<=numday[l][m-1];i++) {printf("%-3d",i);w++;if (w%7==0)printf("\n");}if (w%7) printf("\n");printf("---------------------\n"); }int calday(int y,int m){int d=0,w;while (y>1910){if (IsLeap(y)) d+=366;else d+=365;y--;}while (m>1){ --m;d+=numday[0][m-1];}w=(d+1)%7;return w;}//////////////////////////////////////////void show(int year,int month,int date,int sec,int mini,int hour) {const char month_str[][4]={"","Jan","Feb","Mar","Apl", "May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};int i,j,wdays,mdays,days;for(i=1,days=0;i<year;i++)< p="">{if(leap(i))days += 366;}else{days += 365;}}for(i=1;i<month;i++)< p="">{if(i==2 && leap(year)){days+=29;}else{days+=month_day[i];}}printf(" %s.%d.%d %d:%d:%d \n",month_str[month],date,year,hour,mini,sec);printf(" 一二三四五六日\n");wdays = days % 7;for( j = 0; j < wdays; j++){printf(" ");}if(month == 2 && leap(year)){mdays=29;}else{mdays= month_day[month];}for(i=1;i<=mdays;i++){if( i > 1 && days % 7 == 0 ){printf("\n");}printf("%4d",i);days=days+1; }printf("\n---------------------------\n\n\n");}{time_t rawtime;struct tm *info;int year,month,date,sec,mini,hour; char ch;time ( &rawtime );info = localtime ( &rawtime );year =info->tm_year + 1900; month =info->tm_mon + 1;date =info->tm_mday;hour =info->tm_hour;mini =info->tm_min;sec =info->tm_sec;while(1){show(year,month,date,sec,mini,hour); printf("↑......上一年\n\n");printf("↓......下一年\n\n");printf("←....前一个月\n\n");pr intf("→....后一个月\n\n");printf("Esc.......退出\n\n");ch=getch();switch(ch){case 27://Ecs exit(0);case -32://Navigatorch=getch();if(ch==77){//Rightyear+=(month==12)?1:0;month=month%12+1;}else if(ch==75){year-=(month==1)?1:0; month=(month-2+12)%12+1; }else if(ch==72)year--;else if(ch==80)year++;system("cls");}}</month;i++)<></year;i++)<></w;i++)<></month;i++)<></month;i++)<>。

c语言万年历

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语言报告3

万年历C语言报告3

万年历C语言报告3万年历是一种常用的日历工具,可以显示任意年份的任意月份的日历,并且还能够根据用户的需求进行一定的调整。

通过这个项目的设计和实现,我更深入地了解了C语言的应用,并且提升了编程能力。

本次实现的万年历功能主要包括以下几个方面:1. 用户输入年份和月份,程序可以显示对应年份和月份的日历。

为了实现这个功能,我首先使用了C语言的输入函数 scanf 来获取用户的输入,然后使用了计算机中的日期计算方法和算法来推算出该月份的具体日期。

最后,我们使用C语言的输出函数 printf 来将日历以表格形式显示出来。

2.用户可以选择其中一天并输出该天的详细信息。

在实现这个功能时,我首先使用了结构体来存储日期的信息,包括年、月、日、星期等。

然后,当用户选择其中一天时,我根据输入的日期,查找并输出对应的详细信息,如星期几、是一年中的第几天等等。

3.用户可以选择不同的视图显示日历。

为了实现这个功能,我使用了C语言的条件判断语句来判断用户的选择,并且根据不同的选择来显示不同的视图。

用户可以选择显示整个月份的日历,也可以只显示当月的日期。

此外,为了增加程序的可读性和易用性,我还对程序进行了一些优化和改进:1.使用函数来提高代码的复用性。

我将一些常用的功能封装成函数,如计算2024年6月的天数、计算其中一天的星期几等等。

这样不仅可以减少代码的冗余,并且让代码更加易读和易于维护。

2.添加错误处理机制。

用户输入的年份和月份可能会不符合规定范围,为了避免程序出错,我对用户的输入进行了检查,并在输入错误时进行了相应的提示。

这样可以增加程序的稳定性和可靠性。

3.增加程序的交互性。

用户可以通过菜单选择不同的功能和视图,并根据提示进行操作。

这样可以提高用户的使用体验和程序的易用性。

通过完成这个万年历项目,我不仅学习到了C语言的应用,还锻炼了自己的编程能力。

在这个过程中,我掌握了C语言的输入输出函数、条件判断语句、循环语句等基本语法,并熟悉了C语言的常用函数和相关算法。

(完整版)C语言万年历

(完整版)C语言万年历
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语言版输出日历

C语言版输出日历
return year;
else
printf("The year must be at least 1900\n");
}
}
void PrintCalendar(int year)
{
int month;
for(month=1;month<=12;month++)
printf("The year must not before 1900\n");
}
int GetYear()
{
int year;
while(true)
{
printf("Please input a year:\n");
scanf("%d",&year);
if(year>=1900)
else
weekday=(weekday+365)%7;
}
for(i=1;i<month;i++)
{
weekday=(weekday+MonthDays(year,i))%7;
}
return weekday;
}
bool IsLeapYear(int year)
{
int year;
GetInstruction();
year=GetYear();
PrintCalendar(year);
}
void GetInstruction()
{
printf("This programme displays a calendar for a full\n");

C语言程序设计 输出任意年份的日历

C语言程序设计  输出任意年份的日历
for(q=i1;q<7;q++)/*格式对齐的控制使第二列的日历每行的第一个书从同一位置开始*/
printf("");
printf("");
i2=0;flag2=0;
for(day2=p-(W+t[month-1])%7;day2<=t[month]&&flag2==0;day2++)
{i2++;printf("%4d",day2);
{
if(month<3)
{c=((int)(year-1)/100);
Y=(year-1)-((int)((year-1)/100))*100;
w=((int)(c/4))-2*c+Y+((int)(Y/4))+((int)(13*(month+13)/5));
W=(w+70)%7;
}
else if(month>=3)
if(i2%7==0) flag2=1;}
printf("\n");
}
}
}
{c=((int)(year/100));
Y=year-((int)(year/100))*100;
w=((int)(c/4))-2*c+Y+((int)(Y/4))+((int)(13*(month+1)/5));
W=(w+70)%7;
}
printf("\n\n%d%d\n",month,month+1);printf("日一二三四五六日一二三四五六\n");i1=W;/*一行一行的输出*/

c语言实现一个简单日历

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语言月历

#include <stdio.h>int yy(int date,int weekdays) //输出函数,输出该月月历{int i,j=0;printf("该月的月历如下:\n");printf("Sun Mon Tue Wed Thu Fri Sat");for(i=1;i<=date;) //计算本月第一天是周几。

{if(j<weekdays)printf(" ");elsetf("%3d ",i++) prin;if(j%7==0)printf("\n");j++;}}int ryear(int year) //判断是否为闰年{if (year%400==0||year%4==0&&year%100!=0)return 1;elsereturn 0;}int year(int year) //计算之前年份的总天数{int i,days=0;for(i=1;i<=year;i++){if(ryear(i)) //若是闰年,天数为366天,否则是365天。

days+=366;elsedays+=365;}return days;}int month(int year,int month) //计算该年前几个月份的总天数{int days=0;if(month>1)days+=31;if(month>2){if(ryear(year)) //若是查询闰年2-12月份,则天数+29,否则天数+28 days+=29;elsedays+=28;}if(month>3)days+=31;if(month>4)days+=30;if(month>5)days+=31;if(month>6)days+=30;if(month>7)days+=31;if(month>8)days+=31;if(month>9)days+=30;if(month>10)days+=31;if(month>11)days+=30;elsedays+=0;return days;}int date(int year,int month) //分析该月天数{//int date;int date[13];if (ryear(year)){date[0]=0,date[1]=31,date[2]=29,date[3]=31,date[4]=30,date[5]=31,date[6]=30,date[7]=31,date[8]=31,date[9]=30,date[10]=31,date[11]=30,date[12]=31;}else{date[0]=0,date[1]=31,date[2]=28,date[3]=31,date[4]=30,date[5]=31,date[6]=30,date[7]=31,date[8]=31,date[9]=30,date[10]=31,date[11]=30,date[12]=31;}return date[month];}int day(int day) //总天数{int days=0;days+=day;}int face() //欢迎语{printf("\n");printf(" ┏━━━━━━━━┓\n");printf(" ┃欢迎使用万年历!┃\n");printf(" ┗━━━━━━━━┛\n");printf("\n");printf("\n");}int main() //主函数{int a,b,days,weekdays;face();printf("请输入要查询的年月份(中间用空格间隔):\n"); //输入scanf("%d %d",&a,&b); //输入年月份days=year(a)+month(a,b); //调用年函数和月函数weekdays=days%7;yy(date(a,b),weekdays); //调用输出函数}。

万年历代码c语言

万年历代码c语言

万年历代码c语言万年历是一种实用的日历工具,它可以根据年、月、日来显示当天的日期信息,并可以切换到其他日期来查询对应的日期信息。

在编写万年历的代码时,我们需要考虑输入的年份是否为闰年、每个月的天数、以及每个月第一天是星期几等等。

以下是一份使用C语言编写万年历的参考代码:```c#include <stdio.h>// 判断是否为闰年int isLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }// 获取某年某月的天数int getMonthDays(int year, int month) {int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return days[month - 1];}// 获取某年某月第一天是星期几int getFirstDayOfWeek(int year, int month) {int day = 1;for (int i = 1800; i < year; i++) {if (isLeapYear(i)) {day = (day + 366) % 7;} else {day = (day + 365) % 7;}}for (int i = 1; i < month; i++) {day = (day + getMonthDays(year, i)) % 7;}return day;}int main() {int year, month;printf("请输入年份:");scanf("%d", &year);printf("请输入月份:");scanf("%d", &month);// 判断输入是否合法if (year < 1800 || month < 1 || month > 12) {printf("输入的年份或月份不合法!\n");return 0;}// 获取某年某月的天数和第一天是星期几int days = getMonthDays(year, month);int firstDayOfWeek = getFirstDayOfWeek(year, month); // 打印万年历printf("日一二三四五六\n");for (int i = 0; i < firstDayOfWeek; i++) {printf(" ");}for (int i = 1; i <= days; i++) {printf("%2d ", i);if ((firstDayOfWeek + i) % 7 == 0) {printf("\n");}}printf("\n");return 0;}```以上代码实现了一个简单的万年历功能,在控制台输出给定年份和月份的月历。

C语言打印某一年中某月的日历

C语言打印某一年中某月的日历

C语⾔打印某⼀年中某⽉的⽇历在做学校作业发现的⼀道不错的练⼿题⽬,记录下来⽅便复习⼤致思路:①判断输⼊的年份是否闰年;②计算输⼊⽉份的第⼀天是该年的第⼏天;③计算输⼊⽉份的第⼀天是星期⼏;④输出。

结合代码使⽤:#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main(){int arr1[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };int i = 0;int year = 0;int month = 0;int days = 0;int week = 0;//判断第⼀天周⼏scanf("%d%d", &year, &month);if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))//判断是否是闰年{arr1[2] = 29;//闰年2⽉29天}for (i = 0; i < month; i++){days += arr1[i];//计算该⽉第⼀天是该年多少天}days++;//该⽉第⼀天,还要把这⼀天算上//套⽤计算星期⼏的公式week = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + days) % 7;printf(" ⽇⼀⼆三四五六\n");//表头int arr2[50] = {0};for (i = 0; i < week; i++){printf(" ");//整型数组不能存放空格,因此直接把空格打印出来}arr2[week] = 1;for (i = week + 1; i <= arr1[month]+week-1; i++)//把输⼊⽉份的每⼀天放到数组中{arr2[i] = arr2[i-1]+1;}int count = 0;//记录已打印⼏个数据,打印7个数据就换⾏i = week;int flag = 1;//⽤于记录是否为第⼀⾏,第⼀⾏不能⽤count计数⽅式换⾏while (i<=arr1[month]+week-1)//把该⽉所有天数都打印出来{if (flag <= 7-week){printf("%3d", arr2[i]);if (flag == 7 - week)//打印够数据后换⾏printf("\n");flag++;}else{printf("%3d", arr2[i]);count++;if (count == 7){count = 0;printf("\n");}}i++;}return 0;}实现结果:以上所述是⼩编给⼤家介绍的C语⾔打印某⼀年中某⽉的⽇历,希望对⼤家有所帮助。

C语言实现日历输出

C语言实现日历输出

C语⾔实现⽇历输出这个还是挺实⽤的。

头⽂件:#ifndef MAIN_H#define MAIN_H#include "stdio.h"#include "math.h"#include "stdlib.h"//获取某⼀个⽉的最⼤天数int monthday(int,int );//判断闰年 ,是返回1,不是返回0int isleapyear(int );#endif主函数:#include "main.h"int main(){int year,month,days,weekday;int i,d;while(1){printf("please input the year:\n");scanf("%d",&year);days = year-1+(year-1)/400+(year-1)/4-(year-1)/100;//计算某年第⼀天是星期⼏for(month=1;month<=12;month++){printf("\t\t****%d年--%d⽉****\n",year,month);printf("sun\tmon\ttues\twed\tthur\tfir\tsat\t\n");//表头i = 1; d = 1;weekday = (days + 1)%7; //求星期⼏while(i<=weekday) //输出前⾯的空格{printf("\t");i++;}while(d<=monthday(month,year)) //输出⽇期{weekday = (days + 1)%7;if(weekday==6) //最后⼀个是星期六,输出之后要换⾏printf("%d\n",d);else//不是星期六的输出后不换⾏printf("%d\t",d);if(d==monthday(month,year))printf("\n");d++;days++;}}}}功能函数实现:#include "main.h"int monthday(int month,int year){switch(month){case1:case3:case5:case7:case8:case10:case12:return31;break;case4:case6:case9:case11:return30;break;case2:if(isleapyear(year)){return29;//闰年29天break;}else{return28;break;}}}int isleapyear(int year){if((year%4==0)&&(year%100!=0)||(year%400==0))return1;elsereturn0;}效果图:。

C语言输出某年某月日历

C语言输出某年某月日历

C语⾔输出某年某⽉⽇历C语⾔输出某年某⽉⽇历#include<stdio.h>int datetoweekday(int year, int month, int day) {int monthday[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int monthday1[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int weekday = 0;unsigned long sumday = 0; //距离1900.1.1总间隔天数if (year == 1900 && month <= 1 && month > 0) { //如果⼩于⼀个⽉sumday = day;} else if (year == 1900 && month > 1 && month <= 12) { //如果⼤于⼀个⽉但⼩于⼀年for (int i = 0; i < month - 1; i++) {sumday += monthday[i];}sumday += day;} else if (year > 1900 && month >= 1 && month <= 12) { //如果⼤于⼀年for (int i = 1900; i < year ; i++) { //计算⼀年前所有年的天数if (i % 4 == 0) { //是闰年sumday += 366;if (i == 1900) { //1900不是闰年sumday -= 1;}} else { //不是闰年sumday += 365;}}if (year % 4 == 0) { //当年是闰年for (int i = 0; i < month - 1; i++) {sumday += monthday1[i];}sumday += day;} else { //当年不是闰年for (int i = 0; i < month - 1; i++) {sumday += monthday[i];}sumday += day;}}weekday = sumday % 7;return weekday;}int judgedatestyle(int year, int month, int day) {int ret = 0;if (year < 1900) {printf("year is error.");} else if (month < 1 && month > 12) {printf("month is error.");} else if (day < 0 && day > 31) {printf("day is error.");} else if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {printf("date is wrong");} else if (year % 4 != 0 && month == 2 && day > 28) {printf("date is wrong");} else if (year % 4 == 0 && month == 2 && day > 29) {printf("date is wrong");} else {ret = 1;}return ret;}int main() {//打印⽇历int year = 0, month = 0, day = 1;scanf("%d%d", &year, &month);int monthday[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int monthday1[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};printf(" ⽇⼀⼆三四五六\n");for (int i = 0; i < datetoweekday(year, month, day); i++) {printf(" ");}if(year%4!=0){for (int i = 0; i < 7; i++) {for(int i=datetoweekday(year,month,day);i<=6&&day<=monthday[month-1];i++){printf("% 3d", day);day++;}printf("\n");}}else{for (int i = 0; i < 7; i++) {for(int i=datetoweekday(year,month,day);i<=6&&day<=monthday1[month-1];i++){ printf("% 3d", day);day++;}printf("\n");}}return 0;}。

C语言课程设计-打印月历

C语言课程设计-打印月历

《打印月历》课程设计报告题目打印月历学号姓名年级专业指导教师完成日期年月日目录1实验目的2实验内容3源程序4测试数据5分析评价6实验心得一、实验目的1)在教师指导下,学生根据选定的课题,综合运用所学程序设计的知识,完成问题分析、模块设计、代码编写、程序调试和运行等训练任务。

2)通过课程设计,初步锻炼运用所学基础知识解决实际问题的能力,掌握软件开发的基本过程和基本方法以及良好的编程风格,培养在软件开发中相互合作的团队意识。

二、实验内容打印月历1) 年份和月份由键盘输入,打印的月历按星期一到星期日排列;2) 注意大小月、闰年等问题。

三、源程序#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>int days[6][7];int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int mdays2[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};#define is_leap_year(y) ((!((y) % 4) && ((y) % 100)) || (!((y) % 400)))#define max_mdays(m, y) (is_leap_year(y) ? mdays2[m] : mdays[m])int diff_days(int y1, int m1, int d1, int y2, int m2, int d2){int d = 0;int md = max_mdays(m1, y1);while ((y1 != y2) || (m1 != m2) || (d1 != d2)){d++;d1++;if (d1 > md){d1 = 1;m1++;if (m1 >= 12){m1 = 0;y1++;}md = max_mdays(m1, y1);}}return d;}int get_wday(int y, int m, int d) {struct tm tm;time_t t;int cy, cm, cd, cw;int dd, w, f = 0;time(&t);tm = *localtime(&t);cy = tm.tm_year + 1900;cm = tm.tm_mon;cd = tm.tm_mday;cw = tm.tm_wday;if (cy < y){dd = diff_days(cy, cm, cd, y, m, d);f = 1;}else if (cy > y){dd = diff_days(y, m, d, cy, cm, cd);}else{if (cm < m){dd = diff_days(cy, cm, cd, y, m, d);f = 1;}else if (cm > m){dd = diff_days(y, m, d, cy, cm, cd); }else{if (cd < d){dd = diff_days(cy, cm, cd, y, m, d);f = 1;}else if (cd > d){dd = diff_days(y, m, d, cy, cm, cd);}else{dd = 0;}}}d=d+1;w = f ? (cw + (dd % 7)) : (cw - (dd % 7)) ;if (w < 0){w += 7;}w = f ? (w + ((d - 1) % 7)) : (w - ((d - 1) % 7));if (w < 0){w += 7;}return w;}int main(){int year, mon, day;int i, j, d, w, md;printf("please input the date: ");scanf("%d %d %d", &year, &mon, &day);if (((mon < 1) || (mon > 12))|| ((day < 0) || (day > mdays[mon - 1]))){printf("error! [1 <= mon <= 12] [1 <= day<= 31]\n");return -1;}w = get_wday(year, mon - 1, day);md = max_mdays(mon - 1, year);d = 1;while (d <= md){i = w / 7;j = w % 7;days[i][j] = d++;w++;}printf("\n date: %d year %d mon %dday\n", year, mon, day);printf("==========================================\n"); printf(" %2s %2s %2s %2s %2s %2s %2s\n", "MON", "TUE", "WED", "THU", "FRI", "SAT","SUN");for (i = 0, d = 0; i < 6; i++){for (j = 0; j < 7; j++){if (days[i][j]){d++;(d == day) ? printf("[%2d]", d) : printf(" %2d ", d);}else{printf("%-4s", "");}}printf("\n");}printf("============================================\n"); system("pause");return 0;}四、测试数据任意输入一个日期得到结果:再次尝试输入一个闰年例如2000年2月:得到结果:五、分析评价流程图:我在设计这个程序时采用了使用函数来实现各个打印月历的功能。

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
return 0;
}
int is_leak_year(int year)
{
int is_leak;
if(year%4==0)
{
if(year%100==0 && year%400==0)
{
is_leak=1;
}
else if(year%100!=0)
{
is_leak=1;
{
m=14;
c=(int)((year-1)/100);
y=year-1-c*100;
}
else
{
m=month+1;
c=(int)(year/100);
y=year-c*100;
}
month=convert_str_to_int(argc[2],strlen(argc[2]))-1;
day=1;//默认为一号
weekday=week_calculate(year,month,day);
}
else if(argv==4)//输入参数为月日年时
{
year=convert_str_to_int(argc[1],strlen(argc[1]));
day=today->tm_mday;
weekday=today->tm_wday;
prical=1;
}
else if(argv==2)//输入参数为两个时,打印帮助信息或出错信息
{
if(strcmp(argc[1],"-h")==0)
{
print_help();
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
const int month_day[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31},
int pow_10(int p);
void print_help(void);//打印帮助信息
int main(int argv,char **argc)
{
struct tm *today;
time_t t1;
int leak;//是否为闰年
int year,month,day,weekday;//年月日星期
}
else
{
fprintf(stderr,"不能识别参数,帮助用-h参数");
exit(EXIT_FAILURE);
}
}
else if(argv==3)//输入参数为年月时
{
year=convert_str_to_int(argc[1],strlen(argc[1]));
};//月的天数
//const char *week_day[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat",};
const char *week_day[7]={"日","一","二","三","四","五","六",};
void print_calendar1(int monthday,int mon_first_wd);//打印有输入参数的日历
int convert_str_to_int(char str[],int len);//字符串转化为整数
int week_calculate(int year,int month,int day);//计算输入年月日的星期序号
month=convert_str_to_int(argc[2],strlen(argc[2]))-1;
day=convert_str_to_int(argc[3],strlen(argc[3]));
weekday=week_calculate(year,month,day);
}
else
{
int i;
for(i=0;i<7;i++)
{
printf("%s",week_day[i]);
if(i<6)
{
printf(" ");
}
}
printf("\n");
for(i=0;i<mon_first_wd;i++)//打印1号前的空格
}
}
printf("\n");
}
int convert_str_to_int(char str[],int len)//将年月日字符串转化为整型数据
{
int i;
int j=0;
for(i=0;i<len;i++)
{
j+=(int)(str[i]-'0')*pow_10(len-i-1);
算)
d:日
w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
*/
int y,c,m,w;
int week;
if(month==0)
{
m=13;
c=(int)((year-1)/100);
y=year-1-c*100;
}
else if(month==1)
{
printf(" ");
}
for(i=1;i<=monthday;i++)//打印日期
{
if(i!=today)
{
printf("%2d ",i);
}
else
{
printf("\033[7m%2d\033[0m ",i);//linux中反白显示
const char *month_name[12]={"一","二","三","四","五","六","七","八","九","十","十一","十二",};
int is_leak_year(int year);//判断是否为闰年
void print_calendar0(int monthday,int mon_first_wd,int day);//打印默认参数日历
for(i=0;i<7;i++)
{
printf("%s",week_day[i]);
if(i<6)
{
printf(" ");
}
}
printf("\n");
for(i=0;i<mon_first_wd;i++)//打印1号前的空格
w=y+(int)(y/4)+(int)(c/4)-2*c+(int)(26*(m+1)/10)+day-1;
//计算星期序号0~6
//printf("%d\n",w);
if((w%7)<0)
printf(" %d年%s月\n",year,month_name[month]);
/*打印月历*/
if(prical)
{
print_calendar0(month_day[leak][month],mon_first_day_week,day);
}
else
{
print_calendar1(month_day[leak][month],mon_first_day_week);
exit(EXIT_FAILURE);
}
/*if(month==1)
{
if(leak)
{
if(day>29)
{
fprintf(stderr,"%4d年是闰年,2月只有29天\n",year);
exit(EXIT_FAILURE);
{
/*计算星期序号用蔡勒公式,该公式仅适用于1582年10月15日之后
w:星期;对w取余运算,week=w%7,如果w<0,week=w%7+7,即可得到星期的序号
c:世纪减1(年份前两位数)
y:年(后两位数)
m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作
上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计
}
}*/
//mon_first_day_week=weekday-(day-1)%7;
if((mon_first_day_week=weekday-(day-1)%7)<0)
{
mon_first_day_week+=7;
}
// printf("weekday:%d,%d\n",weekday,mon_first_day_week);
}
else
{
is_leak=0;
}
}
else
{
is_leak=0;
}
return is_leak;
}
void print_calendar0(int monthday,int mon_first_wd,int today)//打印默认月历,高亮显示当前日期
相关文档
最新文档