日历显示:
//头文件
#ifndef __HEAD_DALY_H__
#define __HEAD_DALY_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
uint32_t months[12];
extern bool is_leap(uint32_t year);
extern uint32_t total_day(uint32_t year,uint32_t month,uint32_t day);
extern void display_week(uint32_t year,uint32_t month,uint32_t day);
extern uint32_t get_week(uint32_t year,uint32_t month,uint32_t day );
extern void display2(uint32_t year,uint32_t month,uint32_t day);
extern void display(uint32_t year,uint32_t month,uint32_t day);
#endif
//功能文件
#include "head.h"
uint32_t months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
//判断闰年既然有成功与否就应该用bool
bool is_leap(uint32_t year){
if((year%4==0 && year%100!=0)||(year%400==0)){
return true;
} else{
return false;
}
}
//计算日期是该年的第几天
uint32_t total_day(uint32_t year,uint32_t month,uint32_t day){
bool Leapyear = is_leap(year);
//根据是否是闰年调整每年二月的天数
if(Leapyear){
months[1]+=1;
}
uint32_t days;
//通过for循环将该月之前的天数相加
for(int i=1;i<month;i++){
days+=months[i-1];
}
//这个day就是咱们传进去的
days+=day;
return days;
}
//判断星期几
uint32_t get_week(uint32_t year,uint32_t month,uint32_t day) {
//受公式限制是1,2将转化为13,14月进行计算与此同时年数也会同时减少
if (month == 1 || month == 2) {
month += 12;
year -= 1;
}
//计算是星期几的公式
uint32_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){
//调用上面的函数将星期几打印出来
uint32_t weeks = get_week( year, month, day);
switch (weeks) {
case 1:
printf("%d-%d-%d是星期1\n",year,month,day);
break;
case 2:
printf("%d-%d-%d是星期2\n",year,month,day);
break;
case 3:
printf("%d-%d-%d是星期3\n",year,month,day);
break;
case 4:
printf("%d-%d-%d是星期4\n",year,month,day);
break;
case 5:
printf("%d-%d-%d是星期5\n",year,month,day);
break;
case 6:
printf("%d-%d-%d是星期6\n",year,month,day);
break;
case 0:
printf("%d-%d-%d是星期7\n",year,month,day);
break;
}
}
//显示日历:
//情况1:
/*void display(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");
//将每个月的第一天从周天到该月的第一天前加空格
uint32_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[34m");//背景色是蓝色
printf("%d\t",i);
printf("\033[0m");
} else{
printf("%d\t",i);
}
if((week+i)%7==0) {
putchar('\n');
}
}
putchar('\n');
}
*/
//情况2:
void display2(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 weeks = get_week(year,month,1);
for (int i = 1; i < weeks; 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 ((weeks+ i)%7==1) {
//printf("\n");
putchar('\n');
}
}
}
//主文件
int main() {
printf("请输入日期:");
int year,month,day;
scanf("%d%d%d",&year,&month,&day);
bool Leapyear = is_leap(year);
if(Leapyear){
printf("是闰年\n");
} else{
printf("是平年\n");
}
uint32_t todalDays = total_day(year,month,day);
printf("%d-%d-%d is the %d days of the year.\n",year,month,day,todalDays);
display_week(year,month,day);
//display( year, month, day);
display2( year, month, day);
return 0;
}
第一种情况的显示结果:
第二种情况的显示结果:
- 这是本人的学习笔记不是获利的工具,小作者会一直写下去,希望大家能多多监督我
- 文章会每攒够两篇进行更新发布(受平台原因,也是希望能让更多的人看见)
- 感谢各位的阅读希望我的文章会对诸君有所帮助