编辑代码

#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)/*计算奇数周*/
        {
            if(j<=7)
            {
                mo+=31;
            }
            else
            {
                mo+=30;
            }
        }
        if(j%2==0)/*计算偶数周*/
        {
            if(j==2)
            {
                if(check_year(year)==1)/*闰平年判断*/
                {
                    mo+=29;
                }
                else
                {
                    mo+=28;
                }
            }
            else
            {
                if(j<=6)
                {
                    mo+=30;
                }
                else
                {
                    mo+=31;
                }
            }
        }
    }
	return (sum+mo+2)%7;/*返回输入月份1月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 runping(int week)
{
	int runpingnian,enter=0;/*记录换行*/
	int wee=week;/*将输入年份1月1日是星期几赋值给wee*/
		printf("----------------------> %d月<----------------------\n",month);/*输出月份标头*/
		printf("日	一	二	三	四	五	六\n") ;/*输出星期标头*/
		for(int k=1;k<wee;k++)/*%d月输出wee个空格后输出%d月1日*/
        {
			printf("	");
			enter++;/*记录空格数*/
		} 
        if(check_year(year)==1)
        {
            runpingnian=month_run(month);/*使用闰年月份天数*/
        }
        else
        {
            runpingnian=month_ping(month);/*使用平年月份天数*/
        }
        for(int j=1;j<=runpingnian;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日是星期几*/
	runping(week);/*调用闰平年函数输出*/
	return 0;
}