The third part implements the fourth step first, as shown below, define crt secure no warnings 1
#include
#define start_year 1900
int main()
printf("--- welcome to the Easy Calendar System ---n");
int year, month, monthdays;
1. Enter the year and month to determine whether the entered year is a leap year.
do {printf("Please enter the year:");
scanf("%d", &year);
rewind(stdin);
while (year < start_year);
do {printf("Please enter the month:");
scanf("%d", &month);
rewind(stdin);
while (month < 1 ||month > 12);
int isleapyear = 0;
if (year % 4 == 0 &&year % 100 != 0 ||year % 100 == 0) {
Leap year. isleapyear = 1;
printf("isleapyear=%d", isleapyear);
Calculate the number of days in the input month.
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthdays = 31;
break;
case 4:
case 6:
case 9:
case 11:
monthdays = 30;
break;
case 2:
if (isleapyear == 1) {
monthdays = 29;
else {
monthdays = 28;
break;
printf("monthdays=%d", monthdays);
3. Calculate the day of the week on the first day of the month.
Tips: 1) Cycle counting the total number of days in a year from 1900 to the year entered;
2) It is necessary to determine whether each year is a leap year or a common year, with 366 days in a leap year and 365 days in a common year;
3) Calculate the number of days between the entered month and January of the current year, and add up the days before the entered year;
4) The first day of the week is calculated based on Monday, January 1, 1900
Day x = (1 + total days) %7;Monday to week 6, the numbers 1-6 can be used, and the number 0 can be used to indicate Sunday.
int yeardays = 0;
for (int i = 0; i < year; i++)
if (i % 4 == 0 &&i % 100 != 0 ||i % 400 == 0) {
Leap year. yeardays += 366;
else {
Common year. yeardays += 365;
printf("yeardays=%d", yeardays);
int monthtotaldays = 0;
for (int i = 0; i < month; i++)
if (month == 1 ||month == 3 ||month == 5 ||month == 7 ||month == 8 ||month == 10 ||month == 12) {
monthtotaldays += 31;
else if (month == 4 ||month == 6 ||month == 9 ||month == 11) {
monthtotaldays += 30;
else if (month == 2) {
if (isleapyear == 1) {
monthtotaldays += 29;
else {
monthtotaldays += 28;
printf("monthtotaldays=%d", monthtotaldays);
int week = (yeardays + monthtotaldays + 1) %7;
printf("week=%d ", week);
4. Output calendars in format.
1) Output sequentially by format:
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.
2) Output spaces in a loop, and determine the number of spaces according to the day of the week on the first day of the input month;
3) Output all the days of the month in a loop;
printf("Sunday, t-monday, t-tue, t-wed, t-thurs, t-fri, t-saturday");
for (int i = 0; i < week; i++)
printf("\t");
for (int i = 1; i < monthdays; i++)
printf("%d\t", i);
Think of the spaces and the output date as a whole, and wrap lines every time you output 7 numbers.
if ((week + i) %7 == 0) {
printf("");
return 0;
The test results are as follows, also tested in November 2023
The first day of November 2023 in the calendar is also a Wednesday.
It is best to test several times for the real test, and the test is required in the big month, the small month, and the February of the common year and the leap year.