Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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_Calculator - Fatal编程技术网

C++ 天活计算器总是不能产生准确的结果

C++ 天活计算器总是不能产生准确的结果,c++,date,calculator,C++,Date,Calculator,我对这个程序有一个小问题。我正在尝试创建一个C++程序来计算一个人的生活天数。它接受三个参数,称为月、日和年。这就是该计划背后的全部逻辑: 首先计算今天的日期,并将其保存到三个变量中,即日、月和年,然后找出年差 假设年差为10,用365乘以9年的天数 假设输入月份为5,将4个月内的天数相加,计算出相应的天数,并将结果值加到总天数中 计算从输入年份到当前年份的闰年数,并将该值添加到总天数中 最终的输出由函数返回,但我得到一些日期的轻微错误。输出并不总是准确的。任何帮助都将不胜感激。 以下是全部代码

我对这个程序有一个小问题。我正在尝试创建一个C++程序来计算一个人的生活天数。它接受三个参数,称为月、日和年。这就是该计划背后的全部逻辑:

  • 首先计算今天的日期,并将其保存到三个变量中,即日、月和年,然后找出年差

  • 假设年差为10,用365乘以9年的天数

  • 假设输入月份为5,将4个月内的天数相加,计算出相应的天数,并将结果值加到总天数中

  • 计算从输入年份到当前年份的闰年数,并将该值添加到总天数中

  • 最终的输出由函数返回,但我得到一些日期的轻微错误。输出并不总是准确的。任何帮助都将不胜感激。 以下是全部代码:

    #include <iostream>
    #include <ctime>
    #include <cmath>
    #include <vector>
    using namespace std;
    
    bool IsLeap(int);
    int LeapCount(int);
    int current_year;
    int calculator(int month, int day, int year);
    
    int main()
    {
       cout << calculator(9,24,1994);
    }
    
    int calculator(int month, int day, int year)
    {
        int final_result;
        int day_difference;
        int total_days = 0;
        const int days_inayear = 365;
        vector <int> m_days = {31,28,31,30,31,30,31,31,30,31,30,31};
    
        time_t theTime = time(NULL);
        struct tm *aTime = localtime(&theTime);
    
        int current_day = aTime->tm_mday;
        int current_month = aTime->tm_mon + 1;
        current_year = aTime->tm_year + 1900;
    
        if(year > current_year){
            return 0;
        }
        else if (year == current_year && month > current_month){
            return 0;
        }
        else if(year == current_year && month == current_month && day > current_day){
            return 0;
        }
        else if(year == current_year &&month == current_month && day <= current_day){
                return abs(current_day - day);
        }else
        {
            int year_difference = current_year - year;
            int day_difference = abs(current_day - day);
           for(int a = month+1; a < m_days.size(); a++){
                total_days+= m_days[a];
           }
        int leap_years = LeapCount(year);
    
         total_days+=(year_difference)*days_inayear;
        if(year_difference <= 1 && month < 2){
            leap_years -= 2;
        }
    
        final_result = total_days+leap_years+day_difference;
        if(IsLeap(year) && month > 2){
            final_result = final_result;
        }
        return final_result;
    }
    }
    bool IsLeap(int year)
    {
        if(year%4 == 0 && year%100!=0){
            return true;
        }
        else if(year%4==0 && year%100==0 && year%400==0){
            return true;
        }
        else{
            return false;
        }
    }
    int LeapCount(int year)
    {
       int difference = current_year - year;
       int count = 0;
       for(int x = 0; x<=difference; x++){
            if(IsLeap(year+x)){
                count++;
            }
       }
       return count;
    }
    
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    布尔岛(国际);
    整数跳跃计数(int);
    int本年度;
    整数计算器(整数月、整数日、整数年);
    int main()
    {
    cout tmmday;
    int current_month=aTime->tm_mon+1;
    当前_年=aTime->tm_年+1900;
    如果(年>当前年){
    返回0;
    }
    否则如果(年==当前年和月>当前月){
    返回0;
    }
    否则如果(年==当前年和月==当前月和日>当前日){
    返回0;
    }
    
    否则,如果(年==当前年和月==当前月和日使用日期进行计算不是一项特别简单和直接的任务,更容易简单地使用经过验证的算法,而不是尝试调试损坏的算法

    根据Howard Hinnant的论文,这里有一个算法可以计算自1970-01-01以来的天数:

    #include <limits>
    #include <iostream>
    
    // Returns number of days since civil 1970-01-01.  Negative values indicate
    //    days prior to 1970-01-01.
    // Preconditions:  y-m-d represents a date in the civil (Gregorian) calendar
    //                 m is in [1, 12]
    //                 d is in [1, last_day_of_month(y, m)]
    //                 y is "approximately" in
    //                   [numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366]
    //                 Exact range of validity is:
    //                 [civil_from_days(numeric_limits<Int>::min()),
    //                  civil_from_days(numeric_limits<Int>::max()-719468)]
    template <class Int>
    constexpr
    Int
    days_from_civil(Int y, unsigned m, unsigned d) noexcept
    {
        static_assert(std::numeric_limits<unsigned>::digits >= 18,
                 "This algorithm has not been ported to a 16 bit unsigned integer");
        static_assert(std::numeric_limits<Int>::digits >= 20,
                 "This algorithm has not been ported to a 16 bit signed integer");
        y -= m <= 2;
        const Int era = (y >= 0 ? y : y-399) / 400;
        const unsigned yoe = static_cast<unsigned>(y - era * 400);      // [0, 399]
        const unsigned doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d-1;  // [0, 365]
        const unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy;         // [0, 146096]
        return era * 146097 + static_cast<Int>(doe) - 719468;
    }
    
    #包括
    #包括
    //返回自civil 1970-01-01以来的天数。负值表示
    //1970-01-01之前的天。
    //前提条件:y-m-d表示民事(公历)日历中的日期
    //m在[1,12]中
    //d在[1,月的最后一天(y,m)]
    //y是“近似”in
    //[数值限制::最小值()/366,数值限制::最大值()/366]
    //准确的有效范围为:
    //[civil_from_days(数值限制::min()),
    //civil_from_days(数字限制::max()-719468)]
    模板
    常量表达式
    Int
    从civil开始的天数(整数y,无符号m,无符号d)无例外
    {
    静态断言(标准::数字限制::数字>=18,
    “此算法尚未移植到16位无符号整数”);
    静态断言(标准::数字限制::数字>=20,
    “此算法尚未移植到16位有符号整数”);
    y-=m=0?y:y-399)/400;
    const unsigned yoe=static_cast(y-era*400);/[0399]
    常数无符号doy=(153*(m+(m>2?-3:9))+2)/5+d-1;//[0365]
    const unsigned doe=yoe*365+yoe/4-yoe/100+doy;//[0,146096]
    返回纪元*146097+静态施法(doe)-719468;
    }
    
    这个实现碰巧使用了C++14特性,但是将其转换为C++11甚至C++98应该并不困难

    这是一个相当聪明的算法。霍华德的论文中解释了细节

    要计算两个日期之间的天数,请使用此算法计算两个不同日期的历元天数,然后减去这些值,得到两个日期之间的天数

    int main() {
        int serial_birth_date = days_from_civil(1994, 9, 24);
        int serial_current_date = days_from_civil(2013, 11, 24);
        std::cout << "Days lived: " << serial_current_date - serial_birth_date << '\n';
    }
    
    intmain(){
    int serial_BARTH_date=从civil(1994年9月24日)算起的天数;
    int serial_current_date=从civil(2013年11月24日)算起的天数;
    
    std::如果您提供了两种情况,其中结果是正确的和不正确的,那么这将有助于跟踪问题。如果您能够找到计算器失败的情况的模式,那么找出逻辑中不正确的部分应该很简单。不看代码,您是否只跟踪如果闰日在范围内,是否需要添加闰日?(仅知道闰日发生的年份是不够的,但它是否在出生日期和当前日期之后