2401_83754279 2024-05-21 08:55 采纳率: 89.7%
浏览 2
已结题

c语言将一个程序写入另一个程序中

将第一个程序写入第二个程序中的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) {
    
}

  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2024-05-21 08:55
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    【您想要解决的问题】: 您的问题是要将第一个程序(一个可以生成年历的程序)的功能整合到第二个程序中,使得第二个程序的printYearCalendar(int year)函数能够打印出指定年份的日历。

    【问题出现原因】: 这个问题出现的原因是您需要将两个独立的程序模块合并,并且让第二个程序能够复用第一个程序中的日历打印功能。这涉及到代码的重构和模块化,确保第一个程序的功能能够被第二个程序正确调用。

    【问题解决方案】: 为了解决这个问题,您需要将第一个程序中与打印日历相关的代码复制到第二个程序的printYearCalendar(int year)函数中。同时,您可能需要确保第一个程序中的函数和变量在第二个程序中是可访问的,或者将它们作为函数参数传递。

    【提供代码】: 以下是修改后的第二个程序,其中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) {
        // ...(此处省略了第一个程序中的printMonth函数的代码)
    }
    
    // 第二个程序的函数和主函数
    void printMenu();
    void calculateFutureDate();
    void printYearCalendar(int year);
    void printMonthCalendar(int year, int month);
    
    int main() {
        // ...(此处省略了第二个程序的主函数代码)
    }
    
    void printMenu() {
        // ...(此处省略了第二个程序的printMenu函数代码)
    }
    
    void calculateFutureDate() {
        // ...(此处省略了第二个程序的calculateFutureDate函数代码)
    }
    
    // 打印某年的日历
    void printYearCalendar(int year) {
        // 打印日历的代码,使用第一个程序中的printMonth函数
        for (int month = 1; month <= 12; month++) {
            printMonth(year, month);
            printf("\n");
        }
    }
    
    void printMonthCalendar(int year, int month) {
        // ...(此处省略了第二个程序的printMonthCalendar函数代码)
    }
    

    【代码运行方式】:

    1. 将上述代码保存为一个.c文件。
    2. 使用C编译器(如GCC)编译代码,例如在命令行中使用gcc -o calendar calendar.c
    3. 运行编译后的程序,例如在命令行中使用./calendar

    【代码预期运行结果】: 当运行修改后的程序并选择打印年历的选项时,程序将打印出用户指定年份的所有月份的日历。

    【推荐相关链接】:

    1. C语言编程基础
    2. C语言函数使用
    3. C语言文件操作
    4. C语言结构化编程
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月29日
  • 已采纳回答 5月21日
  • 创建了问题 5月21日