日期函数定义的伪代码

Pseudocode for date function definition

本文关键字:伪代码 定义 函数 日期      更新时间:2023-10-16

我收到的伪代码:

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为真,因此不会发生任何操作。

    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是假的情况下才会发生。但是is_date不应该被解释为一些静态值,而应该被实现为检查日期是否有效(例如,我们有增量后的32天)。仅当新日期无效时,月份和/或年份才会递增。