如何在 C++ 中增加 Date 对象
我有一个一直在处理的受让人,但我陷入了最后一个功能。
使用函数 void Increment(int numDays = 1)
该函数应将日期向前移动参数中给定的日历天数。该参数的默认值为 1 天。示例:
Date d1(10, 31, 1998); // Oct 31, 1998
Date d2(6, 29, 1950); // June 29, 1950
d1.Increment(); // d1 is now Nov 1, 1998
d2.Increment(5); // d2 is now July 4, 1950
我不明白该怎么做。
void Date::Increment(int numDays = 1)
我被困住了,我知道如何通过 ++ 运算符告诉函数递增,但是当我必须让函数将每月的最后一天递增到第一天,或者在最后一天结束时,我会感到困惑例如该月的日期。 10月31日至11月1日,或6月29日至7月4日。我可以在7月5日至7月8日进行,但月份的变化让我感到困惑
I have an assignees that I've been working on and I'm stuck on the last function.
use the function void Increment(int numDays = 1)
This function should move the date forward by the number of calendar days given in the argument. Default value on the parameter is 1 day. Examples:
Date d1(10, 31, 1998); // Oct 31, 1998
Date d2(6, 29, 1950); // June 29, 1950
d1.Increment(); // d1 is now Nov 1, 1998
d2.Increment(5); // d2 is now July 4, 1950
I don not understand how to do this.
void Date::Increment(int numDays = 1)
I'm stuck, I know how to tell the function to increment, by the ++ operator but i get confuse when I have to get the function to increment the last day of the month to the the fist, or to end at the last date of that month for example. Oct 31 to Nov 1, or June 29 to July 4. I can do July 5 to July 8 but the changing months confuse me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要存储每个月有多少天的列表(或数组)。如果您将
numDays
添加到当前日期并且它变得比这个大,您还需要增加月份。例如,我们有一个代表 2010 年 3 月 29 日的日期对象。我们调用
Increment(4)
并向 day 变量添加 4,最终得到 2010 年 3 月 33 日。现在我们检查 3 月有多少天,以及发现它是 31(例如daysInMonth[3] == 31
)。由于 33 大于 31,我们需要从 33 中减去 31 并增加月份,最终得到 2010 年 4 月 2 日。您需要对闰年的 2 月进行特殊处理(任何能被 4 整除且不能被 100 整除的年份,除非它也能整除) 400)并在 12 月底之后增加。
You will need to store a list (or array) of how many days are in each month. If you add
numDays
to the current date and it becomes bigger than this, you need to increment the month as well.For example, we have a date object representing 29 March 2010. We call
Increment(4)
and add 4 to the day variable, ending up with 33 March 2010. We now check how many days March has and find out it's 31 (eg.daysInMonth[3] == 31
). Since 33 is greater than 31, we need subtract 31 from 33 and increase the month, ending up with 2 April 2010.You will need special handling for February in leap years (any year divisible by 4 and not divisible by 100 unless it's also divisible by 400) and for incrementing past the end of December.
30天有九月、四月、六月和十一月。其余的都是 31 天,除了二月有 28 天,闰年(每 4 年一次,2008 年是最后一年)有 29 天。
这应该足够让你继续前进了。
30 days has September, April, June, and November. The rest have 31 days, except for February, which has 28 days except on a leap year (every 4 years, and 2008 was the last one) when it has 29 days.
This should be plenty to get you going.
首先,构造一个函数,用于
计算从众所周知的日期(例如 1900 年 1 月 1 日)到特定日期经过的天数。
接下来,构造另一个函数,将日增量转换为日期
从您的示例中,
First, construct a function like
which counts number of days elapsed from a well known date (e.g. Jan 1 1900) to the specific Date.
Next, construct another function which converts that day-delta to Date
From your example,