Katecoco 2024-06-17 20:27 采纳率: 50%
浏览 1

C语言打鱼晒网问题求解

C语言程序设计打鱼晒网问题要求有目录,可以求输入日期距离系统时间的相差天数,并输出输入日期为星期几,判断是否为公历节日

img

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-06-17 20:27
    关注

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


    这个问题涉及到C语言的多个知识点,包括日期计算、闰年判断、星期计算、以及使用time.h头文件中的函数。下面是一个大致的解决方案,包括一些关键的函数实现和主函数的框架。

    首先,你需要包含必要的头文件,并定义一些基本的函数原型:

    #include <stdio.h>
    #include <time.h>
    
    // 函数原型声明
    int isLeapYear(int year);
    int calculateWeekday(int year, int month, int day);
    int calculateDaysBetween(struct tm *tm1, struct tm *tm2);
    int isHoliday(int year, int month, int day);
    void calculateFishingDay(int year, int month, int day);
    
    int main() {
        // 主函数实现
        // ...
        return 0;
    }
    
    // 其他函数实现
    

    接下来,实现isLeapYear函数来判断是否为闰年:

    int isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    

    实现calculateWeekday函数来计算星期:

    int calculateWeekday(int year, int month, int day) {
        int zellerFormula = day + ((13 * (month + 1)) / 5) + year + (year / 4) - (year / 100) + (year / 400);
        return zellerFormula % 7; // 返回值为0-6,对应星期天到星期六
    }
    

    实现calculateDaysBetween函数来计算两个日期之间的天数差:

    int calculateDaysBetween(struct tm *tm1, struct tm *tm2) {
        time_t t1 = mktime(tm1);
        time_t t2 = mktime(tm2);
        return (int)(difftime(t2, t1) / (60 * 60 * 24));
    }
    

    实现isHoliday函数来判断是否为公历节日,这里需要你根据实际情况来填充节日的判断逻辑:

    int isHoliday(int year, int month, int day) {
        // 这里需要根据公历节日的日期来编写判断逻辑
        // 示例:如果是1月1日,则为新年
        if (month == 1 && day == 1) {
            return 1; // 表示是节日
        }
        // 其他节日的判断...
        return 0; // 表示不是节日
    }
    

    实现calculateFishingDay函数来判断渔夫是打鱼还是晒网:

    int calculateFishingDay(int year, int month, int day) {
        int sum = 0;
        for (int y = 1990; y < year; y++) {
            if (isLeapYear(y)) {
                sum += 366;
            } else {
                sum += 365;
            }
        }
        // 计算输入年份的天数
        sum += day;
        if (month > 1) {
            sum += (month - 1) * 31; // 简化计算,实际需要根据每个月的天数来计算
        }
        return (sum % 5 < 4); // 判断是打鱼还是晒网
    }
    

    最后,在main函数中实现用户输入和多分支选择结构,调用上述子函数:

    int main() {
        struct tm current_tm;
        time_t current_time;
    
        // 获取当前时间
        time(&current_time);
        localtime_r(&current_time, &current_tm);
    
        int year, month, day;
        printf("请输入你想知道的年和月和日(年月用逗号隔开):");
        scanf("%d,%d,%d", &year, &month, &day);
    
        struct tm input_tm = {0};
        input_tm.tm_year = year - 1900; // tm结构体中的年份是从1900年开始的
        input_tm.tm_mon = month - 1;    // 月份是从0开始的
        input_tm.tm_mday = day;
    
        int daysBetween = calculateDaysBetween(&current_tm, &input_tm);
        int weekday = calculateWeekday(year, month, day);
        int isFishingDay = calculateFishingDay(year, month, day);
    
        printf("距今天还有%d天,是星期%d。\n", daysBetween, weekday);
        if (isHoliday(year, month, day)) {
            printf("这是一个公历节日!\n");
        }
        printf("渔夫%s。\n", isFishingDay ? "在打鱼" : "在晒网");
    
        return 0;
    }
    

    请注意,这个解决方案是一个基础的框架,可能需要根据你的具体需求进行调整和完善。例如,calculateWeekday函数中的月份天数计算是简化的,实际应用中需要根据每个月的实际天数来计算。同样,isHoliday函数也需要你根据公历节日的具体日期来填充判断逻辑。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月17日