简体   繁体   English

简单的程序来计算一天不工作

[英]simple program to calculate day not working

I am trying to write a simple program, that outputs the day given a date, using 1/1/0001 as Saturday. 我正在尝试编写一个简单的程序,使用1/1/0001作为星期六来输出给定日期的日期。 I have written the program, but have passed about 4 hours just to find some mistake which is making my answer wrong. 我已经编写了程序,但经过了大约4个小时才发现一些错误,这使我的答案是错误的。 The correct answer can be found here. 正确的答案可以在这里找到。 http://www.timeanddate.com/date/durationresult.html?d1=1&m1=1&y1=1&d2=1&m2=1&y2=400&ti=on http://www.timeanddate.com/date/durationresult.html?d1=1&m1=1&y1=1&d2=1&m2=1&y2=400&ti=on

The following is my program: 以下是我的程序:

#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;
}

According to the website, total no.of days from 1/1/1 to 1/1/400 should be 145,735 days, but according to my code, it is 145732. I cannot find where I am missing 3 days. 根据该网站,从1/1/1到1/1/400的总天数应为145,735天,但根据我的代码,则为145732天。找不到3天了。 Please help me. 请帮我。

You may use this online editor to compile my program. 您可以使用此在线编辑器来编译我的程序。 http://www.compileonline.com/compile_c_online.php http://www.compileonline.com/compile_c_online.php

The online calendar program is correct; 在线日历程序正确; then again, so is your code. 再说一遍,您的代码也是如此。 The problem lies in the specific time period for which you are calculating. 问题出在您要计算的特定时间段。

Your code adjusts for leap years: every 4 years, except for centuries (which are not) and quad-centuries (which again are ). 您的代码会根据leap年进行调整:每隔4年调整一次,除了几个世纪(不是)和四个世纪(再次 )。 However, both adjustments "once a century" were not observed in the Julian Calendar : 但是,“ 儒略历 ”中未观察到“一次世纪”的两种调整:

The Julian reform lengthened seven months and replaced the intercalary month with an intercalary day to be added every four years to February. 朱利安式改革延长了七个月,并用一个中间日代替了中间月,直到2月每四年增加一次。

For the period 46 BC - 4 October 1582, you should use the Julian way of calculating leap years. 对于公元前46年-1582年10月4日,您应该使用儒略式计算leap年。 For dates after that "closing date" (which is 15 October 1582 ), you can use the Gregorian (current) calculation. 对于该“截止日期”之后的日期( 1582年10月15日 ),可以使用公历 (当前)计算。

Without the 2 adjustment for leap years in whole centuries, you get the same result as the online calendar. 如果整个世纪都没有对leap年进行2次调整,您将获得与在线日历相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM