代码:
char *the_first(int year_int) {
int day_one = 0, month = 1, month_day = 31;
char *weekday;
char *month_name;
//Calculating Dates:
//N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
//N = 1 + 2(13) + [3(13+1)/5] + year_int + [year_int/4] - [year_int/100] + [year_int/400] + 2
while (month < 13) {
if (month == 1 || month == 2) {
//Very important Rule states that we have to use the previous year and 13 for the month
//removing a year from year_int but not impacting memory year_int
day_one =
1 + (2 * (month + 12)) + 3 * ((month + 12) + 1) / 5 + (year_int-1) + ((year_int-1) / 4) - ((year_int-1) / 100) +
((year_int -1) / 400) +
2;
day_one = day_one % 7;
} else {
day_one =
1 + (2 * month) + 3 * (month + 1) / 5 + year_int + (year_int / 4) - (year_int / 100) +
(year_int / 400) +
2;
day_one = day_one % 7;
}
//validating day returned
if (day_one < 0 || day_one >= 7) {
printf("Day: %d - Invalid Day\n", day_one);
} else {
switch (day_one) {
case 1:
weekday = "Sunday\n";
printf("%s", weekday);
break;
case 2:
weekday = "Monday\n";
printf("%s", weekday);
break;
case 3:
weekday = "Tuesday\n";
printf("%s", weekday);
break;
case 4:
weekday = "Wednesday\n";
printf("%s", weekday);
break;
case 5:
weekday = "Thursday\n";
printf("%s", weekday);
break;
case 6:
weekday = "Friday\n";
printf("%s", weekday);
break;
case 0:
weekday = "Saturday\n";
printf("%s", weekday);
break;
default:
printf("Error");
break;
}
}
switch (month) {
case 1:
month_name = "January";
month_day = 31;
printf("%s\t\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 2:
month_name = "February";
month_day = 28;
printf("%s\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 3:
month_name = "March";
month_day = 31;
printf("%s\t\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 4:
month_name = "April";
month_day = 30;
printf("%s\t\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 5:
month_name = "May";
month_day = 31;
printf("%s\t\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 6:
month_name = "June";
month_day = 30;
printf("%s\t\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 7:
month_name = "July";
month_day = 31;
printf("%s\t\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 8:
month_name = "August";
month_day = 31;
printf("%s\t\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 9:
month_name = "September";
month_day = 30;
printf("%s\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 10:
month_name = "October";
month_day = 31;
printf("%s\t\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 11:
month_name = "November";
month_day = 30;
printf("%s\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
case 12:
month_name = "December";
month_day = 31;
printf("%s\t\t\t\t\t%d\n", month_name, year_int);
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
break;
default:
printf("Invalid Month");
break;
}
for (int j = 1; j <= month_day; ++j) {
printf("%d\t", j);
if (j % 7 == 0) {
printf("\n");
}
}
month += 1;
printf("\n\n");
}
return 0;
用户在主菜单中输入“ 2000”后的输出:
Enter a year:2000
2000
Saturday
January 2000
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
问题:我需要每月的第一天从相应的计算日(周六)开始,以下数字也要相应地移动。如果可以简化,我将星期六链接到一个整数。我如何不使用函数或数组就可以有效地实现这一点? (不允许在作业中) 感谢您提供的任何指导。