编辑代码

#include<stdio.h>
#include<time.h>
int year,month;/*定义全局变量*(/

/*检查平年闰年函数*/
int check_year(int year)
{
	if((year%400==0)||(year%4==0&&year%100!=0))
    {
		return 1;/*是闰年返回1*/
	}
    else
    {
		return 0;/*不是闰年返回0*/
	}
}

/*返回输入年份1月1日是星期几*/
int week_f(int year)
{
	if(year==1)/*如果是公元1年1月1日*/
    {
		return 1;/*返回公年1月1日是星期一*/
	}
 
	int sum=0,mo=0;
	for(int i=1;i<year;i++)/*累加至输入年份之前的所有年的天数*/
    {
		if(check_year(i)==1)
        {
			sum+=366;/*闰年累加366天*/
		} 
        else
        {
			sum+=365;/*闰年累加365天*/
		}
	}

    for(int j=1;j<month;j++)/*计算本月前的天数*/
    {
        if(j%2==1&&j<=7)/*7月前的奇数月天数*/
        {
            mo+=31;
        }
        if(j%2==1&&9<=j)/*9月后奇数月天数*/
        {
            mo+=30;
        }
        if(j==2&&check_year(year)==1)
        {
            mo+=29;
        }
        if(j==2&&check_year(year)==0)
        {
            mo+=28;
        }
        if(j%2==0&&j<=6&&j!=2)/*7月前除2月的偶数月天数*/
        {
            mo+=30;
        }
        if(j%2==0&&8<=j)/*7月后的偶数月天数*/
        {
            mo+=31;
        }
    }
	return (sum+mo+1)%7;/*返回输入月份1月1日是星期几,由于把星期日放在了首位,因此需要将sum+1*/
}

/*闰年各月的天数*/
int month_run(int n)
{
	switch(n)
    {
		case 1:return 31;/*1月的天数*/
		case 2:return 29;/*2月的天数*/
		case 3:return 31;/*3月的天数*/
		case 4:return 30;/*4月的天数*/
		case 5:return 31;/*5月的天数*/
		case 6:return 30;/*6月的天数*/
		case 7:return 31;/*7月的天数*/
		case 8:return 31;/*8月的天数*/
		case 9:return 30;/*9月的天数*/
		case 10:return 31;/*10月的天数*/
		case 11:return 30;/*11月的天数*/
		case 12:return 31;/*12月的天数*/
	}
}

/*平年各月的天数*/
int month_ping(int n)
{
	switch(n)
    {
		case 1:return 31;/*1月的天数*/
		case 2:return 28;/*2月的天数*/
		case 3:return 31;/*3月的天数*/
		case 4:return 30;/*4月的天数*/
		case 5:return 31;/*5月的天数*/
		case 6:return 30;/*6月的天数*/
		case 7:return 31;/*7月的天数*/
		case 8:return 31;/*8月的天数*/
		case 9:return 30;/*9月的天数*/
		case 10:return 31;/*10月的天数*/
		case 11:return 30;/*11月的天数*/
		case 12:return 31;/*12月的天数*/
	}
}

/*闰年输出函数*/
void run(int week)
{
	int enter=0;/*记录换行*/
	int wee=week;/*将输入年份1月1日是星期几赋值给wee*/
		printf("----------------------> %d月<----------------------\n",month);/*输出月份标头*/
		printf("日	一	二	三	四	五	六\n") ;/*输出星期标头*/
		for(int k=0;k<wee;k++)/*%d月输出wee个空格后输出%d月1日*/
        {
			printf("	");
			enter++;/*记录空格数*/
		} 
		for(int j=1;j<=month_run(month);j++)/*用循环来输出每月的天*/
        { 
			if(enter%7==0)/*如果enter%7==0,则表明需要已过了一周,需换行*/
            {
				printf("\n");
			}
			printf("%d	",j);/*输出每月的天*/
			enter++;/*记录空格数*/
		}
}

/*平年输出函数*/
 void ping(int week)
 {
	int enter=0;/*记录换行*/
	int wee=week;/*将输入年份1月1日是星期几赋值给wee*/
		printf("----------------------> %d月<----------------------\n",month);/*输出月份标头*/
		printf("日	一	二	三	四	五	六\n") ;/*输出星期标头*/
		for(int k=0;k<wee;k++)/*%d月输出wee个空格后输出%d月1日*/
        {
			printf("	");
			enter++;/*记录空格数*/
		} 
		
		for(int j=1;j<=month_ping(month);j++)/*用循环来输出每月的天*/
        { 
			if(enter%7==0)/*如果enter%7==0,则表明需要已过了一周,需换行*/
            {
				printf("\n");
			}
			printf("%d	",j);/*输出每月的天*/
			enter++;/*记录空格数*/
		}
}

int main()
{
	int mo=0;
	int week=0;
	int sum=0; 
    time_t timep;
    struct tm *p;
    time (&timep);
    p=gmtime(&timep);
    year=1900+p->tm_year;/*输出系统的年份*/
    month=1+p->tm_mon;/*输出系统的月份*/
	week=week_f(year);/*调用函数,得知年份1月1日是星期几*/
	if(check_year(year)==1)/*判断是否为闰年*/
    {
		run(week);/*闰年,调用闰年函数*/
	} else
    {
		ping(week);/*平年,调用平年函数*/
	}
	return 0;
}