C语言实现万年历

C语言实现万年历

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

//判断润年
bool isLeap(int year);
//获取月份天数
int getMonthDays(int year,int month);
//函数功能:输入年月日,输出1900-1-1到该天的天数,-1位输入不合法
int Day(int y,int m,int d);
//绘制表头 
void printHeader();
//绘制日历日期 
void printDays(int week,int day);
//绘制表脚 
void prinfFooter();
//输出菜单 
void printMenu();
//绘制日历 
void printCalendar(int year,int month);
//重新输入时间
void resetDate();
//下月 
void nextMonth();
//上月 
void lastMonth();
//获取当前年份月份 
void getNowDate();

int year,month;

int main(){
	getNowDate(); 
	printCalendar(year,month);
	while(1){
		printMenu();
	} 
	return 0;
} 

void getNowDate(){
	time_t timep;
	struct tm *p;
	time(&timep);
	p = gmtime(&timep);
	year = 1900 + p->tm_year;
	month = 1 + p->tm_mon;
//	printf("%d  %d  %d:", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday);
//	printf("%d;%d;%d ", p->tm_hour+8, p->tm_min, p->tm_sec);
	return;
}

void printCalendar(int year,int month){
	int m = getMonthDays(year,month);
	int d = Day(year,month,1) % 7;
	printHeader(); 
	printDays(d - 1,m);
	prinfFooter();
} 

//重新输入时间
void resetDate(){
    printf("请输入年、月(用空格隔开):");
    scanf("%d %d",&year,&month);
    printCalendar(year,month);
} 
//下个月 
void nextMonth(){
	month = month + 1;
	if(month == 13){
		month = 1;
		year = year + 1;
	}
    printCalendar(year,month);
} 
//上个月 
void lastMonth(){
	month = month - 1;
	if(month == 0){
		month = 12;
		year = year - 1;
	}
    printCalendar(year,month);
} 

void printMenu(){
	printf("1、重新输入时间\n");
	printf("2、下个月\n");
	printf("3、上个月\n");
	printf("4、退出程序\n");
	printf("请选择:");
	int choose;
	scanf("%d",&choose); 
	system("CLS");
	switch(choose){
		case 1:
			resetDate(); 
			break;
		case 2:
			nextMonth();
			break;
		case 3:
			lastMonth();
			break;
		case 4:
			exit(1);
			break;
		default:
			printf("请选择正确选项!!!\n");
			break;
	}
	
}

void printHeader(){
	printf("\t\t%d年%d月\t\n",year,month);
	printf(" -------------------------------------------------------\n");
	printf("|*******************************************************|\n");
	printf("|-------------------------------------------------------|\n");
	printf("|星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六  |\n");
	printf("|-------------------------------------------------------|\n");
	return ;
}

void prinfFooter(){
	printf("\n|-------------------------------------------------------|\n");
	printf("|*******************************************************|\n");
	printf("|-------------------------------------------------------|\n");
}

void printDays(int week,int day){
	printf("|");
	if(week == -1) week = 0;
	for(int i = 0; i < week; i++){
		printf("\t");
	}
//	printf("week=%d\n",week);
	for(int i = 0; i < day; i++){
		if((i + week > 0) && (i + week) % 7 == 0){
			printf("|\n|\n| %d\t",i + 1);
		}else{
			printf("  %d\t",i + 1);
		}
	}
	return;
}

//判断润年
bool isLeap(int year){
	if((year%4==0 && year%100!=0)||(year%400==0)){
        return true;
    }
    return false;
}
//获取月份天数
int getMonthDays(int year,int month){
	month = month - 1;
	if(month < 0 || month > 11) return 0;
	int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    if(isLeap(year)){
    	months[1] = 29;
    }
    return months[month];
}
//函数功能:输入年月日,输出1900-1-1到该天的天数,-1位输入不合法
int Day(int y,int m,int d)
{
    int i;
    int r=0;  //储存1900到该天的闰年个数
    int a[]={31,28,31,30,31,30,31,31,30,31,30}; //m给每出月的天数
 
    if(y>=1900 && m>0 && m<13 && d>0 && d<32)
    {
        for(i=1900;i<=y;i++)  //计算闰年的个数
        {
            if(i%400==0||i%100!=0&&i%4==0)
                r++;
        }
        for(i=0;i<m-1;i++)  //统计当年1月1日到当月的天数
        {
            d+=a[i];
        }
        if((y%400==0||y%100!=0&&y%4==0)&&m<3)  //如果当年是闰年但不到二月就不能算进去
            r--;
        return 365*(y-1900)+r+d;
    }
    else
        return -1;
}

上一篇:vue获取当天时间


下一篇:力扣·日期专题