输入年月日打印这个月的日历
calendar.h文件
#ifndef __HEAD_CALENDAR_H__
#define __HEAD_CALENDAR_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
extern uint32_t month[12];
extern bool is_leap(uint32_t year);
extern uint32_t total_day(uint32_t year,uint32_t month,uint32_t day);
extern uint8_t get_week(uint32_t year,uint32_t month,uint32_t day);
extern void display_week(uint32_t year,uint32_t month,uint32_t day);
extern void display_month(uint32_t year,uint32_t month,uint32_t day);
#endif
calendar.c文件
#include "calendar.h"
uint32_t months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//判断是否是闰年
bool is_leap(uint32_t year){
if(year%4==0&&year%100!=0||year%400==0){
return 1;
}else
return 0;
}
//判断该日期是该年的第几天
uint32_t total_day(uint32_t year,uint32_t month,uint32_t day){
bool isLeapYear = is_leap(year);
bool days;
if(isLeapYear==1){
months[1]+=1;
}
for(int i=1;i<month;i++){
days+=months[i-1];
}
days+=day;
return days;
}
//判断日期是星期几
uint8_t get_week(uint32_t year,uint32_t month,uint32_t day){
if(month==1||month==2){
month+=12;
year-=1;
}
uint8_t week=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;
return week;
}
void display_week(uint32_t year,uint32_t month,uint32_t day){
uint8_t week = get_week(year,month,day);
switch(week){
case 1:
printf("%d-%d-%d is Monday\n",year,month,day);
break;
case 2:
printf("%d-%d-%d is Tuesday\n",year,month,day);
break;
case 3:
printf("%d-%d-%d is Wednesday\n",year,month,day);
break;
case 4:
printf("%d-%d-%d is Thursday\n",year,month,day);
break;
case 5:
printf("%d-%d-%d is Friday\n",year,month,day);
break;
case 6:
printf("%d-%d-%d is Saturday\n",year,month,day);
break;
case 0:
printf("%d-%d-%d is Sunday\n",year,month,day);
break;
}
}
//显示日历
void display_month(uint32_t year,uint32_t month,uint32_t day){
printf("\t\t\t%d-%d\n",year,month);
printf("日\t一\t二\t三\t四\t五\t六\n");
uint8_t week = get_week(year,month,1);
for(int i=1;i<=week;i++){
printf("\t");
}
for(int i=1;i<months[month-1];i++){
if(i==day){
printf("\033[35m");
printf("%d\t",i);
printf("\033[0m");
}else{
printf("%d\t",i);
}
if((week+i)%7==0){
putchar('\n');
}
}
putchar('\n');
}
主文件
#include "calendar.h"
int main(){
printf("please input date:");
int year,month,day;
scanf("%d %d %d",&year,&month,&day);
bool isLeapYear = is_leap(year);
if(isLeapYear){
printf("%d is a leap year.\n",year);
}else{
printf("%d is not leap year.\n",year);
}
uint32_t totalDays = total_day(year,month,day);
printf("%d-%d-%d is the %d days of the year.\n",year,month,day,totalDays);
display_month(year,month,day);
return 0;
}