You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

日历程序中Daycode简易循环计算方案求助(修复闰年异常)

修复循环版Daycode计算方案

问题分析

你之前的循环逻辑核心错误是遍历范围不对:计算year年的Daycode时,只需累加公元0年到year-1年的天数影响(因为year年的第一天是year-1年最后一天的次日),但你的循环遍历到了year年,多算了一年的天数增量,导致闰年场景下结果偏差。

修复后的代码实现

先实现准确的闰年判断(兼容你给出的公元0年定义),再用循环逐年累加天数对星期的影响:

#include <stdio.h>

#define TRUE    1
#define FALSE   0

int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
    " ",
    "\n\n\nJanuary",
    "\n\n\nFebruary",
    "\n\n\nMarch",
    "\n\n\nApril",
    "\n\n\nMay",
    "\n\n\nJune",
    "\n\n\nJuly",
    "\n\n\nAugust",
    "\n\n\nSeptember",
    "\n\n\nOctober",
    "\n\n\nNovember",
    "\n\n\nDecember"
};

int inputyear(void)
{
    int year;
    printf("Please enter a year (example: 1999) : ");
    scanf("%d", &year);
    return year;
}

// 适配公元0年的闰年判断:公元0年按定义为非闰年
int is_leap(int year) {
    if (year == 0) return FALSE;
    if (year % 400 == FALSE) return TRUE;
    if (year % 100 == FALSE) return FALSE;
    if (year % 4 == FALSE) return TRUE;
    return FALSE;
}

// 循环版Daycode计算函数
int determinedaycode(int year) {
    int daycode = 0; // 公元0年的Daycode为0
    
    // 累加公元0年到year-1年的天数影响
    for (int y = 0; y < year; y++) {
        if (is_leap(y)) {
            // 闰年366天,366%7=2,Daycode加2
            daycode = (daycode + 2) % 7;
        } else {
            // 非闰年365天,365%7=1,Daycode加1
            daycode = (daycode + 1) % 7;
        }
    }
    return daycode;
}

int determineleapyear(int year)
{
    if(year%4 == FALSE && year%100 != FALSE || year%400 == FALSE)
    {
        days_in_month[2] = 29;
        return TRUE;
    }
    else
    {
        days_in_month[2] = 28;
        return FALSE;
    }
}

void calendar(int year, int daycode)
{
    int month, day;
    for ( month = 1; month <= 12; month++ )
    {
        printf("%s", months[month]);
        printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );
        
        for ( day = 1; day <= 1 + daycode * 5; day++ )
        {
            printf(" ");
        }
        
        for ( day = 1; day <= days_in_month[month]; day++ )
        {
            printf("%2d", day );
            
            if ( ( day + daycode ) % 7 > 0 )
                printf("   ");
            else
                printf("\n ");
        }
            daycode = ( daycode + days_in_month[month] ) % 7;
    }
}

int main(void)
{
    int year, daycode, leapyear;
    
    year = inputyear();
    daycode = determinedaycode(year);
    determineleapyear(year);
    calendar(year, daycode);
    printf("\n");
}

关键说明

  1. 闰年判断修正:新增is_leap函数明确公元0年为非闰年,匹配你给出的Daycode定义(公元0年Daycode=0,公元1年Daycode=1)。
  2. 遍历范围调整:循环从y=0到y<year,仅计算到year-1年,确保year年的Daycode是前序所有年份的天数总和对7取余的结果。
  3. 增量逻辑:非闰年每年给Daycode加1(365%7=1),闰年加2(366%7=2),每次取余7避免数值溢出。

验证示例

  • 公元1年:循环执行y=0(非闰年),daycode=0+1=1,与定义一致。
  • 公元4年:循环执行y=0/1/2/3(均非闰年),daycode=0+1+1+1+1=4,与原公式计算结果一致。
  • 公元5年:循环执行到y=4(闰年),daycode=4+2=6,与原公式计算结果一致。

内容的提问来源于stack exchange,提问作者FireFox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 19:04:58