Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 计算不工作日的简单程序_C_Date Arithmetic - Fatal编程技术网

C 计算不工作日的简单程序

C 计算不工作日的简单程序,c,date-arithmetic,C,Date Arithmetic,我试图写一个简单的程序,输出给定日期的日期,使用1/1/0001作为星期六。我已经写了这个程序,但已经过了大约4个小时,只是发现了一些错误,这使得我的答案是错误的。正确答案可以在这里找到 以下是我的节目: #include <stdio.h> #include <string.h> long long int leap_days, days; long long int *no_leap_years(int day, int month, long long int

我试图写一个简单的程序,输出给定日期的日期,使用1/1/0001作为星期六。我已经写了这个程序,但已经过了大约4个小时,只是发现了一些错误,这使得我的答案是错误的。正确答案可以在这里找到

以下是我的节目:

#include <stdio.h>
#include <string.h>

long long int leap_days, days;

long long int *no_leap_years(int day, int month, long long int year){
    leap_days = (year-1)/4;
    leap_days -= (year-1)/100;
    leap_days += (year-1)/400;

    int is_leap;

    if(!(year%400)){
        is_leap = 1;
    } else if(!(year%100)){
        is_leap = 0;
    } else if(!(year%4)){
        is_leap = 1;
    } else {
        is_leap = 0;
    }

    printf("Leap year - %d\n", is_leap);
    printf("Leap days - %lld\n", leap_days);
    if(is_leap){
        if(month>2) leap_days++;
        else if(month==2){
            if(day>28) leap_days++;
        }
    }
    printf("Leap days - %lld\n", leap_days);
    return &leap_days;
}

long long int *no_of_days(int day, int month, long long int year){
    long long int leap_days = *no_leap_years(day, month, year);
    days = leap_days + (365*(year-1));
    if(month>1) days+=31;
    if(month>2) days+=28;
    if(month>3) days+=31;
    if(month>4) days+=30;
    if(month>5) days+=31;
    if(month>6) days+=30;
    if(month>7) days+=31;
    if(month>8) days+=31;
    if(month>9) days+=30;
    if(month>10) days+=31;
    if(month>11) days+=30;
    days+=day;
    printf("total - %lld\n", days);
    return &days;
}

char *day_computer(int day, int month, long long int year){
    long long int days = *no_of_days(day, month, year);
    printf("total - %lld\n", days);
    days %= 7;
    printf("remain - %lld\n", days);
    if(days==1) return "Saturday";
    else if(days==2) return "Sunday";
    else if(days==3) return "Monday";
    else if(days==4) return "Tuesday";
    else if(days==5) return "Wednesday";
    else if(days==6) return "Thursday";
    else return "Friday";
}

int main()
{
   int  dd = 1;
   int mm = 1;
   long long int yy = 400;

   printf("%s\n", day_computer(dd, mm, yy));

   return 0;
}
#包括
#包括
长-长-长-内-闰天,天;
长整年*无闰年(整日、整月、长整年){
闰日=(1年)/4;
闰日-=(1年)/100;
闰日+=(1年)/400;
int是_leap;
如果(!(年份%400)){
is_leap=1;
}否则,如果(!(年份%100)){
is_leap=0;
}否则,如果(!(第%4年)){
is_leap=1;
}否则{
is_leap=0;
}
printf(“闰年-%d\n”,是_Leap);
printf(“闰日-%lld\n”,闰日);
if(is_leap){
如果(月>2)闰日++;
否则,如果(月==2){
如果(天>28)闰日++;
}
}
printf(“闰日-%lld\n”,闰日);
返回和闰日;
}
长整日*无整日(整日、整月、整年){
long long int leap_天数=*无闰年(日、月、年);
天数=闰日+(365*(1年));
如果(月>1)天+=31;
如果(月>2)天+=28;
如果(月>3)天+=31;
如果(月>4)天+=30天;
如果(月>5)天+=31;
如果(月>6)天+=30天;
如果(月>7)天+=31;
如果(月>8)天+=31;
如果(月>9)天+=30天;
如果(月>10)天+=31;
如果(月>11)天+=30天;
天+=天;
printf(“总计-%lld\n”,天);
返回&天;
}
字符*日\计算机(整数日、整数月、长整数年){
long long int days=*无天数(日、月、年);
printf(“总计-%lld\n”,天);
天数%=7天;
printf(“剩余-%lld\n”,天);
如果(天==1)返回“星期六”;
否则,如果(天==2)返回“星期日”;
如果(天==3)返回“星期一”;
如果(天==4)返回“星期二”;
如果(天==5)返回“星期三”;
否则,如果(天==6)返回“星期四”;
否则返回“星期五”;
}
int main()
{
int-dd=1;
int mm=1;
长整型yy=400;
printf(“%s\n”,日计算机(日、月、年));
返回0;
}
根据网站,从1/1/1到1/1/400的总天数应该是145735天,但根据我的代码,是145732天。我找不到我错过了3天的地方。请帮帮我


你可以使用这个在线编辑器来编译我的程序

在线日历程序正确;同样,代码也是如此。问题在于您计算的特定时间段

您的代码针对闰年进行调整:每4年调整一次,但世纪(不是)和四世纪(同样是)除外。然而,这两项调整“一个世纪一次”在以下方面均未得到遵守:

朱利安改革延长了七个月,并将闰月改为每隔四年在二月增加一个闰日

对于公元前46年到1582年10月4日这段时间,你应该使用朱利安的方法来计算闰年。对于“截止日期”(即1582年10月15日)之后的日期,可以使用公历(当前)计算


如果没有对整个世纪的闰年进行2次调整,您将得到与在线日历相同的结果。

400年后您预计会有多少闰日?100,对吗?您的程序正在报告96。@FiddlingBits No。。实际上,数字可以除以100而不是400,这不是闰年。比如1900年、1800年、2100年不是。但是2004002000年是。更多地研究闰年的定义。阅读有关C的更多信息。使用所有警告和调试信息进行编译(例如,
gcc-Wall-g
如果使用…)。了解如何使用调试器(例如,Linux上的
gdb
)。