c语言日历

C语言日历

#include

int leap(int year) //判断闰年
{
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
return 1;
else
return 0;
}

int max_day(int year, int month) //确定输入月份的天数
{
int day[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (leap(year) == 1) //如果是闰年,改变二月的天数
day[1] = 29;
return day[month - 1];
}

int total_day(int year,int month) //确定输入年份的总天数
{
int sum = 0;
int i = 1;
for (i = 1; i < month; i++)
sum += max_day(year, i);
sum += 1;
return sum;
}

int weekday(int year,int month ) //确定输入的当月的第一天是星期几
{
int count;
count = ((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + total_day(year,month)) % 7;
return count;
}

void display(int year, int month) //在屏幕上显示出对应的日历
{
int i = 0, j = 1;
int week, max;
week = weekday(year,month);
max = max_day(year, month);
printf("\n\t\t\t\t%d年%d月", year, month);
printf("\n");
printf("\n\t日\t一\t二\t三\t四\t五\t六\n\n");
for (i = 0; i < week; i++) //一号前几天位置空出
printf("\t");
for (j = 1; j <= max; j++) //打印当月的日历
{
printf("\t%d", j);
if (i % 7 == 6)
printf("\n\n");
i++;
}
}

main()
{
int year, month, day;
printf(“请输入:年,月,日(数据之间空格断开)”);
scanf_s("%d%d%d", &year, &month, &day);
if (month < 1 || month>12 || day < 1 || day>31) //报错
{
printf(“error!”);
return 0;
}
display(year, month);
}

你可能感兴趣的:(c#,代码规范)