万年历显示
课程设计目标:
设计一个程序实现万年历的显示,要求:
(1)根据输入的年份(1980-2040),在屏幕上输出该年的日历。
(2)根据输入的年月信息,在屏幕上输出该月的日历。
(3)根据输入的年月日信息,计算距今天还有多少天,星期几,是否是节日。
( 4)输出年历时,以月份和星期做表头。
个人理解:
1.输入年份1980-2040在屏幕上输出该年的日历
2.根据输入的年月信息,在屏幕上输出该月的日历
3.根据输入的年月日信息,计算距今还有多少天,星期几,是否为节日
4.输出年历时,以月份和星期作表头。
5.要求有菜单
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "time.h"
int nowyear(){ //获取系统的年份
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
return( timeinfo->tm_year+1900) ;
}
void happy(int month,int day){ //节假日查询
printf("\t\t\t***********************************************\n");
printf("\n\t\t\t");
if(month==1&&day==1) printf("您所查询的日期为元旦节,祝您元旦节快乐");
if(month==1&&day==6) printf("您所查询的日期为小寒,请注意保暖");
if(month==1&&day==20) printf("您所查询的日期为大寒,请注意保暖");
if(month==2&&day==4) printf("您所查询的日期为立春,请注意保暖");
if(month==2&&day==14) printf("您所查询的日期为情人节,请注意保护单身狗");
if(month==3&&day==5) printf("您所查询的日期为惊蛰,多喝热水");
if(month==3&&day==8) printf("您所查询的日期为妇女节,多喝热水");
if(month==3&&day==12) printf("您所查询的日期为植树节,蚂蚁森林也能种树哦");
if(month==3&&day==20) printf("您所查询的日期为春分,多出去走走享受生活");
if(month==4&&day==1) printf("您所查询的日期为愚人节,请小心你身边的伙伴");
if(month==4&&day==4) printf("您所查询的日期为清明节,踏青春游正在路上");
if(month==4&&day==19) printf("您所查询的日期为谷雨,多喝热水");
if(month==5&&day==1) printf("您所查询的日期为劳动节,今年的劳动节您准备去哪里玩呢?");
if(month==5&&day==4) printf("您所查询的日期为五四青年节,五四精神永远存在青年心中");
if(month==5&&day==20) printf("您所查询的日期为小满,天气转热要多注意");
if(month==6&&day==1) printf("您所查询的日期为儿童节,身边的孩子有没有更可爱呢?");
if(month==6&&day==21) printf("您所查询的日期为夏至,我要一大早醒来爱你一整天");
if(month==7&&day==1) printf("您所查询的日期为建党节,没有共产党就没有新中国");
if(month==7&&day==6) printf("您所查询的日期为小暑,请注意避暑");
if(month==7&&day==22) printf("您所查询的日期为大暑,也要出去运动注意湿气哦");
if(month==8&&day==1) printf("您所查询的日期为建军节,听党指挥能打胜仗作风优良");
if(month==8&&day==7) printf("您所查询的日期为立秋,今天有没有想我啊");
if(month==8&&day==22) printf("您所查询的日期为处暑,天气开始转凉,要注意身体哦");
if(month==9&&day==1) printf("就要开学了,你兴奋吗!");
if(month==9&&day==7) printf("您所查询的日期为白露,蒹葭苍苍,白露为霜") ;
if(month==9&&day==10) printf("您所查询的日期为教师节,今天向老师问好了吗");
if(month==9&&day==22) printf("您所查询的日期为秋分,多喝热水");
if(month==10&&day==1) printf("您所查询的日期为国庆节,我为我的祖国自豪");
if(month==10&&day==8) printf("您所查询的日期为寒露,天气渐冷,注意穿衣");
if(month==10&&day==23) printf("您所查询的日期为霜降,注意穿衣");
if(month==11&&day==7) printf("您所查询的日期为立冬,今天立冬了,你爱的人在哪里呢");
if(month==12&&day==7) printf("您所查询的日期为大雪,今天的郑州下雪了吗");
if(month==12&&day==21) printf("您所查询的日期为冬至,圣源餐厅的饺子今天什么馅的啊");
}
void searchhead(int year,int month){ //月历的表头
printf("\t%d年%d月\n",year,month);
printf("\t***************************************************\n") ;
printf("\t日\t一\t二\t三\t四\t五\t六\n");
}
int monthnum(int year,int month){ //返回月份多少天
int a1[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int a2[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
if(leapyear(year)==1) return a2[month];
else return a1[month];
}
int leapyear(int year){ //判断是否为闰年
if(year%4==0&&year%100!=0||year%4==0&&year%400==0) return 1;
else return 0;
}
int week(int year,int month,int day){ //根据蔡勒公式计算星期几
int w ,c,y;
if(month==1){
month=13;
year=year-1;
}
if(month==2){
month=14;
year=year-1;
}
y=year%100;
c=(year/1000)*10+(year/100%10);
if(y==0) c=c-1;
w=y+floor(y/4)+floor(c/4)-2*c+floor(26*(month+1)/10.0)+day-1;
while(w<0)
w=w+7;
if(w%7==0)
return(7);
return(w%7);
}
int nowmonth() { //获取系统的月份
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
return(timeinfo->tm_mon+1 ) ;
}
int sum(int year, int month, int day){ //计算一个时间点到公元一年一月一日的天数
int mon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int sum = (year - 1) * 365; //自从公元一年来的天数
int i;
sum += (year - 1) / 4 + 1; //能被四整除的加上1
sum -= (year - 1) / 100 + 1; //其中被100整除的不是闰年
sum += (year - 1) / 400 + 1; //实际上能被400整除的即为闰年
for(i = 0; i < month - 1; ++i) //将本年剩下的整月加上
sum += mon[i];
if(leapyear(year) == 1 && month > 2) //本年是否为闰2月
++sum;
sum += day; //本月的日子加上
return sum;
}
void copy(int year,int month) { //打印日历
printf("\n");
int k,i,j,spacenum,count;
searchhead(year,month) ;//获取表头
spacenum=week(year,month,1);//计算星期几
if(spacenum!=7) //输出空格 第一位数字
for(i=1;i<=spacenum;i++)
printf("\t ");
count=spacenum;
for(i=1;i<=monthnum(year,month);i++){ //打印日历
printf("\t%d ",i);
count++;
if(count%7==0)
printf("\n");
}
printf("\n");
}
int function1(){
int k,i,j,spacenum,count;
printf("-----查看当年年历或当月月历-----\n\n");
printf("\t您想查看今年的年历还是当月的月历,请输入1【年历】或0【月历】:");
scanf("%d",&k);
printf("\n");
if(k==0)
copy(nowyear(),nowmonth());
if(k==1)
for(j=1;j<=12;j++)
copy(nowyear(),j);
}
int function2(){
int year,month,j;
printf("-----搜素某年的年历或某月的日历-----\n\n");
printf("\t请输入您所想查看的年份:");
scanf("%d",&year);
printf("\t请输入您所想查看的月份(如果想查看整年日历请输入'0'):");
scanf("%d",&month);
if(month!=0)
copy(year,month);
if(month==0)
for(j=1;j<=12;j++)
copy(year,j);
}
int function3(){
time_t timep;
struct tm *p;
time(&timep);
p = gmtime(&timep);
int year,month,day,baby,sign;
int a,b,c,x,y,z;
printf("\n\n\n\t\t\t********************************************************************\n");
printf("\n\t\t\t如果您需要计算距离本日时间请输入0,如果是需要计算两个日子时间请输入1:");
scanf("%d",&sign);
if(!sign){
printf("\n\n\t\t\t*****************************\n");
printf("\n\t\t\t请输入您所需要计算的年份:");
scanf("%d",&year);
printf("\n\n\t\t\t***************************\n");
printf("\n\t\t\t请输入您所需要计算的月份:");
scanf("%d",&month);
printf("\n\n\t\t\t***************************\n");
printf("\n\t\t\t请输入您所需要计算的日份:");
scanf("%d",&day);
baby = sum(nowyear(),nowmonth(),p->tm_mday)-sum(year,month,day);
baby=fabs(baby);
printf("\n\n\t\t\t据今天有%d天",baby);
printf(",而且您所查询的日期为星期%d!\n\n",week(year,month,day));
happy(month,day);
}
else{
printf("\n\n\t\t\t***********************************\n");
printf("\n\t\t\t请输入您所需要计算第一个的年份:");
scanf("%d",&a);
printf("\n\n\t\t\t********************************\n");
printf("\n\t\t\t请输入您所需要计算第一个的月份:");
scanf("%d",&b);
printf("\n\n\t\t\t********************************\n");
printf("\n\t\t\t请输入您所需要计算第一个的日份:");
scanf("%d",&c);
printf("\n\n\t\t\t**********************************\n");
printf("\n\t\t\t请输入您所需要计算第二个的年份:");
scanf("%d",&x);
printf("\n\n\t\t\t********************************\n");
printf("\n\t\t\t请输入您所需要计算第二个的月份:");
scanf("%d",&y);
printf("\n\n\t\t\t********************************\n");
printf("\n\t\t\t请输入您所需要计算第二个的日份:");
scanf("%d",&z);
baby=sum(x,y,z)-sum(a,b,c);
baby=fabs(baby);
printf("\n\n\t\t\t********************");
printf("\n\n\t\t\t两个日期相差有%d天",baby);
}
}
int head(){
int select;
printf("\n\n\n\n\n\n\n");
printf("\t\t\t\t\t <--欢迎使用老张的万年历-->\n\n");
printf("\t\t\t\t ****************************************\n");
printf("\t\t\t\t 请选择要使用的功能:\n\n");
printf("\t\t\t\t\t 1.查看今年年历或当月月历\n");
printf("\t\t\t\t\t 2.搜素某年的年历或某月的日历\n");
printf("\t\t\t\t\t 3.查询历史上某日\n");
printf("\t\t\t\t\t 4.退出\n");
printf("\t\t\t\t ****************************************\n");
printf("\t\t\t\t\t 请选择<1,2,3,4>:");
scanf("%d",&select) ;
printf("\n");
return select;
}
int frist(){
printf("\n\t\t\t\tWelcome to use the calendar made by Mr.zhang\n");
printf("\n\t\t\t\tYou need to answer one question before using the calendar\n");
printf("\n\t\t\t\tIs zhang haojie a handsome boy?\n");
printf("\n\t\t\t\tOnly can u answer yes or no\n");
printf("\n\t\t\t\tPlease enter your answer:");
char ch0[10],ch1[10]="no",ch2[10]="yes";
scanf("%s",ch0);
if(strcmp(ch0,ch2)){
while(1){
printf("\n\t\t\t\tTry again:");
scanf("%s",ch0);
if(strcmp(ch0,ch1)) break;
}
}
printf("\n\t\t\t\t好滴!谢谢你啦!");
return;
}
int main(){
int select;
char ch;
frist();
while(1){
select=head();
if(select==1)
function1();
if(select==2)
function2();
if(select==3)
function3();
if(select==4)
break;
}
}