繁体   English   中英

用于日期函数定义的伪代码

[英]Pseudocode for date function definition

我收到的伪代码:

Date& operator++(){
    //add 1 to d  //tomorrow, unless we were at the end of the month
    //if is_date is false
    //            //need to change to first of next month
    //  set d to 1
    //  if m is December
    //            //need to change to next year too      
    //    set m to January
    //    increment y
    //  else
    //    increment m
    return *this;

}

我的解释:

Date& Date::operator++(){ 
    if (is_date==false){ 
        m=m+1; 
        d=1; 
    } 
    if (m==dec && d==29){ 
        m=jan; 
        y=y+1; 
    } 
    else{ 
        m=m+1; 
    } 
    d=d+1; 
}

这样看起来还好吗? 我正在根据Stroustrups的书进行硬件分配。 只是需要一些验证

让我们增加2010-03-10

    if (is_date==false){ 
        m=m+1; 
        d=1; 
    } 

我们假设is_date为true,所以不会发生任何操作。

    if (m==dec && d==29){ 
        m=jan; 
        y=y+1; 
    } 

m不是dec, d不是29,因此不会发生任何操作。

    else{ 
        m=m+1; 
    } 

等待! m递增。

    d=d+1;

d

我们现在有2010-04-11不是我们想要的。

再看一下伪代码-发生的第一件事是增加一天。 其他所有情况在is_date为false时发生。 但是is_date不应被解释为某个静态值,而应被实现为检查日期是否有效(例如,增量后有32天)。 仅当新日期无效时,月和/或年才递增。

暂无
暂无

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

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