题目:年历显示

5 篇文章 0 订阅

题目:年历显示

功能要求:
(1)输入一个年份,输出是在屏幕上显示该年的日历,假定输入的年份在1940~2040年之间。
(2)输入年月,输出该月的日历。
(3)输入年月日,输出距今天还有多少天,星期几,是否是公历节日
年历输出如下:
输出样例
感谢小伙伴们提醒,之前的年历显示代码有错误,今天抽下午时间改正一下。
备注:输入年月时,格式为2021-06-02

#include<stdio.h>
#include<string.h>
#include<time.h> 
//将1-12月日历依次存入1~10,11~20这样的二维数组中,把日历数组定义成全局变量,方便调用 
int calendar[120][8]={0};
/* 
基姆拉尔森计算公式
W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
在公式中d表示日期中的日数,m表示月份数,y表示年份数。
计算出的值W 0~6表示星期一到星期日 
注意:采用这个公式计算某i一天是星期几,需要把一月和二月看成是上一年的十三月和十四月,例:如果是2021-1-1则换算成:2020-13-1来代入公式计算。
*/
int calWeek(int y, int m,int d){
	if(m==1||m==2) m+=12,y--;
	//我们这里返回的值为0~6, 表示星期日,星期一到星期六 
	return ((d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7+1)%7;
}
//判断平年还是闰年,返回这个月对应的天数 
int calmonth(int y, int m){
	int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
	if ((y % 4 == 0) && (y % 100 != 0) || y % 400 == 0)
		return b[m-1];
	else
		return a[m-1];
}
//数一数这个月输出时需要几行 
int count(int n){
	for(int i=n*10+2;i<n*10+10;i++){
		if(calendar[i][0]==0){
			return i%10-1;
		} 
	} 
}
//输出函数,美化界面 
void coutCalendar(int year){
	printf(":==================The Calendar of Year %04d =====================:\n", year);
	for (int p = 0, q = 6; p < 6; p++, q++){
		int cnt = count(p)>count(q)?count(p):count(q);
		printf(": %2d  SUN MON TUE WED THU FRI SAT  %2d  SUN MON TUE WED THU FRI SAT:\n", p+1, q+1);
		int num1=p*10+1;//左边月份的开始行 
		int num2=q*10+1;
		while(cnt--){
			printf(":   ");
			for(int i=0;i<7;i++){
				if(calendar[num1][i]==0){
					printf("    ");
				}else{
					printf("%4d",calendar[num1][i]);
				}
			}
			num1++;
			printf("      ");
			for(int i=0;i<7;i++){
				if(calendar[num2][i]==0){
					printf("    ");
				}else{
					printf("%4d",calendar[num2][i]);
				}
			}
			num2++;
			printf(":\n");
		}
	}
	printf(":-----------------------------------------------------------------:\n");
}
//输出函数,输出某个月的日历 
void coutMonth(int year,int month){
	printf("         %04d-%02d\n", year, month);
	printf(" SUN MON TUE WED THU FRI SAT\n");
	printf("============================\n");
	for(int i=(month-1)*10+1;i<(month-1)*10+count(month-1)+1;i++){
		for(int j=0;j<7;j++){
			if(calendar[i][j]==0){
				printf("    ");
			}else{
				printf("%4d",calendar[i][j]);
			}
		}
		printf("\n");
	}
}
//这里我就举几个例子,如果是公历节日,返回true, 否则返回false 
bool isFestival(int month, int day){
	if(month == 1 && day == 1){
		//元旦节
		return true; 
	}else if(month == 2 && day == 14){
		//情人节 
		return true;
	}else if(month == 3 && day == 8){
		//妇女节 
		return true;
	}else if(month == 3 && day == 12){
		//植树节 
		return true;
	}else if(month == 4 && day == 1){
		//愚人节 
		return true;
	}else if(month == 5 && day == 1){
		//劳动节 
		return true;
	}else if(month == 5 && day == 4){
		//青年节 
		return true;
	}else if(month == 5 && day == 1){
		//劳动节 
		return true;
	}else if(month == 6 && day == 1){
		//儿童节 
		return true;
	}else if(month == 7 && day == 1){
		//建党节 
		return true;
	}else if(month == 5 && day == 1){
		//建军节 
		return true;
	}else if(month == 9 && day == 10){
		//教师节 
		return true;
	}else if(month == 10 && day == 1){
		//国庆节 
		return true;
	}else if(month == 5 && day == 1){
		//劳动节 
		return true;
	}else if(month == 12 && day == 25){
		//圣诞节 
		return true;
	}else{
		return false;
	} 
}
//输入年月日,输出距今天数,和星期几,是否是公历节日 
void coutFestival(int year_end, int month_end, int day_end){
	time_t t;
    struct tm * lt;
    //获取Unix时间戳
    time(&t);
    //转为时间结构
    lt = localtime(&t); 
    int year_start = lt->tm_year+1900, month_start = lt->tm_mon + 1, day_start=lt->tm_mday;
	int y2, m2, d2;  
	int y1, m1, d1;    
	m1 = (month_start + 9) % 12;  
	y1 = year_start - m1/10;  
	d1 = 365*y1 + y1/4 - y1/100 + y1/400 + (m1*306 + 5)/10 + (day_start - 1);
    m2 = (month_end + 9) % 12;  
	y2 = year_end - m2/10;  
	d2 = 365*y2 + y2/4 - y2/100 + y2/400 + (m2*306 + 5)/10 + (day_end - 1);    
	if(isFestival(month_end, day_end)){
		printf("距今天还有%d天, 星期%d,是公历节日", d2-d1, calWeek(year_end, month_end, day_end));
	}else{
		printf("距今天还有%d天, 星期%d,不是公历节日", d2-d1, calWeek(year_end, month_end, day_end));
	} 
}
int main(){
    int year,month,day;
    printf("Please input the year whose calendar you want to know:"); 
    char a[10];
    scanf("%s",a);
	year = (a[0] - '0') * 1000 + (a[1] - '0') * 100 + (a[2] - '0') * 10 + (a[3] - '0');
	month = (a[5] - '0') * 10 + (a[6] - '0');
	day = (a[8] - '0') * 10 + (a[9] - '0'); 
	for(int i=1;i<=12;i++){
    	int week = calWeek(year,i,1);
    	int cnt=1;
    	int num = (i-1)*10+1;
    	while(cnt<=calmonth(year,i)){
    		calendar[num][week]=cnt++;
    		week=(week+1)%7;
    		if(week==0){
    			num++;
			}
		}
	}
    if(strlen(a)==4){
    	coutCalendar(year);
	}else if(strlen(a) == 7){
		coutMonth(year,month);
	}else{
		coutFestival(year, month, day);
	}
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值