在C或C日历日期算术++(N天添加到给定的日期) [英] Arithmetics on calendar dates in C or C++ (add N days to given date)

查看:254
本文介绍了在C或C日历日期算术++(N天添加到给定的日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑一个日期,我正在采取像(日,月,年)的输入: 12,03,87

现在我需要找出在 N 天的日期。

我已经写了code这一点,但它的效率不高。你能告诉我这工作快任何好的逻辑和有较少的复杂性。

 的#include<&stdio.h中GT;静态INT days_in_month [] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
INT日,月,年;无符号短day_counter;INT is_leap(int y)对{
    收益率((Y%4 == 0安培;&安培;!Y%100 = 0)|| Y%400 == 0);
}明天()
{
    天+ = 1; day_counter ++;
    如果(日> days_in_month [月]){
        天= 1;
        月+ = 1;
    如果(月> 12){
        一个月= 1;
        今年+ = 1;
        如果(is_leap(年)){
            days_in_month [2] = 29;
        }其他{
            days_in_month [2] = 28;
        }
    }
}
}set_date(INT D,INT男,int y)对
{
    M< 1? m = 1时:0;
    M> 12? M = 12:0;
    D< 1? D = 1:0;
    D> days_in_month [M] D = days_in_month [M]:0;
    如果(is_leap(y))为{
        days_in_month [2] = 29;
    }
    其他{
        days_in_month [2] = 28;
    }
    天= D;
    月=米;
    年= Y;
 }skip_days(INT X)
{
    INT I;
    对于(i = 0; I< X,我++)NEXT_DAY();
}print_date()
{
    的printf(天:%d月:%d年数:%d \\ n,日,月,年);
}INT主(INT ARGC,字符** argv的)
{
     INT I;     set_date(5,2,1980);     skip_days(40);
     day_counter = 0;
     / *此调用后NEXT_DAY每一天* /     print_date();     返回0;
}


解决方案

  

您能告诉我这工作快任何好的逻辑和有较少的复杂性。


如果这个确切的事情确实是你的应用程序的性能关键部分,你可能做错了什么。为了清晰和正确的缘故,你应该坚持现有的解决方案。选择一个最适合你的开发环境。


的C方式:

 的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;诠释的main()
{
    / *初始化* /
    INT Y = 1980年,M = 2,D = 5;
    结构TM T = {.tm_year = Y-1900,.tm_mon = M-1,.tm_mday = D};
    / * *修改/
    t.tm_mday + = 40;
    mktime(& T公司);
    / *显示结果* /
    的printf(%S,asctime(amp; T公司)); / *打印:孙月16日00:00:00 1980年* /
    返回0;
}

在C ++不使用升压方法:

 的#include<&的ctime GT;
#包括LT&;&iostream的GT;诠释的main()
{
    //初始化
    INT Y = 1980年,M = 2,D = 5;
    的std :: TM T = {};
    t.tm_year = Y-1900;
    t.tm_mon = M-1;
    t.tm_mday = D;
    //修改
    t.tm_mday + = 40;
    的std :: mktime(& T公司);
    //显示结果
    性病::法院LT&;<的std :: asctime(& T公司); //输出:孙月16日00:00:00 1980年
}

借助的Boost.Date_Time 方式:

 的#include<升压/ DATE_TIME /了posix_time / posix_time.hpp>
#包括LT&;&iostream的GT;诠释的main()
{
    使用空间boost ::格利高;
    //初始化
    日期D(1980,2,5);
    //修改
    D + =天数(40);
    //显示结果
    性病::法院LT&;< D<<的'\\ n'; //输出:1980-MAR-16
}

I have been given a date, Which I am taking as an input like (day, month, year): 12, 03, 87.

Now I need to find out the date after n days.

I have written code for this, But its not efficient. Can you please tell me any good logic which works faster and have less complexity.

#include <stdio.h>

static int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day, month, year;

unsigned short day_counter;

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

next_day()
{
    day += 1; day_counter++;
    if (day > days_in_month[month]) {
        day = 1;
        month += 1;
    if (month > 12) {
        month = 1;
        year += 1;
        if (is_leap(year)) {
            days_in_month[2] = 29;
        } else {
            days_in_month[2] = 28;
        }
    }
}
}

set_date(int d, int m, int y) 
{
    m < 1 ? m = 1 : 0;
    m > 12 ? m = 12 : 0;
    d < 1 ? d = 1 : 0;
    d > days_in_month[m] ? d = days_in_month[m] : 0;
    if (is_leap(y)){
        days_in_month[2] = 29;
    } 
    else {
        days_in_month[2] = 28;
    }
    day = d;
    month = m;
    year = y;
 }

skip_days(int x)
{
    int i;
    for (i=0;i<x;i++) next_day();
}

print_date()
{
    printf ("day: %d month: %d year: %d\n", day, month, year);
}

int main(int argc, char **argv)
{
     int i;

     set_date(5, 2, 1980);

     skip_days(40);
     day_counter = 0;
     /* after this call next_day each day */

     print_date();

     return 0;
}

解决方案

Can you please tell me any good logic which works faster and have less complexity.

If this exact thing is indeed a performance critical part of your application, you're likely doing something wrong. For the sake of clarity and correctness, you should stick to the existing solutions. Select the one that is most appropriate to your development environment.


The C approach:

#include <stdio.h>
#include <time.h>

int main()
{        
    /* initialize */
    int y=1980, m=2, d=5;    
    struct tm t = { .tm_year=y-1900, .tm_mon=m-1, .tm_mday=d };
    /* modify */
    t.tm_mday += 40;
    mktime(&t);
    /* show result */
    printf("%s", asctime(&t)); /* prints: Sun Mar 16 00:00:00 1980 */
    return 0;
}

The C++ without using Boost approach:

#include <ctime>
#include <iostream>

int main()
{        
    // initialize
    int y=1980, m=2, d=5;
    std::tm t = {};
    t.tm_year = y-1900;
    t.tm_mon  = m-1;
    t.tm_mday = d;
    // modify
    t.tm_mday += 40;
    std::mktime(&t);
    // show result
    std::cout << std::asctime(&t); // prints: Sun Mar 16 00:00:00 1980
}

The Boost.Date_Time approach:

#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>

int main()
{
    using namespace boost::gregorian;
    // initialize
    date d(1980,2,5);
    // modify
    d += days(40);
    // show result
    std::cout << d << '\n'; // prints: 1980-Mar-16
}

这篇关于在C或C日历日期算术++(N天添加到给定的日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆