将第一个程序写入第二个程序中的void printYearCalendar(int year)中,使之成为第二个程序的一个功能
#include <stdio.h>
#include <stdlib.h>
int isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int daysInMonth(int month, int year) {
switch (month) {
case 2: return isLeapYear(year) ? 29 : 28;
case 4: case 6: case 9: case 11: return 30;
default: return 31;
}
}
void printMonth(int year, int month) {
int days = daysInMonth(month, year);
int startDay = 1; // Assuming the first day is always Sunday
// Print month header
printf("%d-%02d ", year, month);
// Print days of the week header
printf("Sun Mon Tue Wed Thu Fri Sat\n");
// Print leading spaces for the first day of the month
for (int i = 0; i < startDay; i++) {
printf(" ");
}
// Print days of the month
for (int day = 1; day <= days; day++) {
printf("%3d", day);
if ((startDay + day - 1) % 7 == 0) {
printf("\n");
}
}
}
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
// Open file to write the calendar
FILE *file = fopen("cal.txt", "w");
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
// Print calendar for each month
for (int month = 1; month <= 12; month += 3) {
fprintf(file, "%d ", month);
for (int i = 0; i < 3; i++) {
printMonth(year, month + i);
}
fprintf(file, "\n");
}
// Close the file
fclose(file);
return 0;
}
#include <stdio.h>
// 函数声明
void printMenu();
void calculateFutureDate();
void printYearCalendar(int year);
void printMonthCalendar(int year, int month);
int main() {
int choice;
do {
printMenu();
printf("请输入您的选择(1-3),或者输入其他数字退出程序:");
scanf("%d", &choice);
switch(choice) {
case 1:
calculateFutureDate();
break;
case 2: {
int year;
printf("请输入年份:");
scanf("%d", &year);
printYearCalendar(year);
break;
}
case 3: {
int year, month;
printf("请输入年份和月份:");
scanf("%d %d", &year, &month);
printMonthCalendar(year, month);
break;
}
default:
printf("谢谢使用,再见!\n");
return 0;
}
} while(1);
return 0;
}
// 打印功能菜单
void printMenu() {
printf("==== 日历问题 ====\n");
printf("1. 计算未来天数和星期\n");
printf("2. 打印年历\n");
printf("3. 打印月历\n");
}
void calculateFutureDate() {
// 在这里实现计算未来天数和星期的功能
}
// 打印某年的日历
void printYearCalendar(int year) {
}
void printMonthCalendar(int year, int month) {
}