从 C++ 中的给定日期(从头开始)减去天数
我有一个下面的函数,用于向给定日期添加天数(格式:yyyy/mm/dd addDays)。它不适用于闰年(有一次仓促的失败尝试使闰年起作用。)我如何编辑它以便我可以从给定日期中减去天数?我想我需要一个 for 循环,它也从 addDays_ 中减去天数,但会减少并重置。我还想让它成功计算闰年日期。
int year;
int month;
int day;
int changeDays;
void DaysToDate::add(int addDays_) {
//Calculate the date add the changeDays.
// Do NOT input values greater than 730 days.
int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int daysInYear = 365;
if (isLeapYear(year) || isLeapYear(year+1) == true) {
monthdays[2] = 29;
daysInYear = 366;
}
if (addDays_ >= daysInYear) {
int addyears = addDays_ / daysInYear;
year += addyears;
if (isLeapYear(year) == true) {
monthdays[2] = 29;
daysInYear = 366;
}
}
int currentDays = day;
int i = month;
if (addDays_ >= monthdays[month]) {
while (addDays_ >= monthdays[i]) {
if (i + 1 >= 12) {
month = 1;
i = 1;
}
else
month++;
addDays_ -= monthdays[i];
//cout << "monthdays[i] "<< monthdays[i] << "\n";
if (addDays_ + day < monthdays[month]) {
day += addDays_;
}
i++;
}
}
if (addDays_ + day < monthdays[month]) {
day += addDays_;
}
if ((day == monthdays[month - 1]) && (month < 12))
{
day = 1;
month++;
}
if ((day == monthdays[month - 1]) && (month == 12))
{
day = 1;
month = 1;
year++;
}
else {
}
cout << year << " " << month << " " << day << " " << "\n";
}
bool DaysToDate::isLeapYear(int year_) {
if (year % 400 == 0) {
return true;
}
if (year % 100 == 0) {
return false;
}
if (year % 4 == 0) {
return true;
}
else {
return false;
}
}
输入 2009/04/05 7
- 输出 2009 4 12
I have a function below used to add a number of days to a given date (format: yyyy/mm/dd addDays). It does not work for leap years (there was a rushed failed attempt to make leap years work.) How can I edit this so I may subtract days from a given date? I imagine I will need a for loop which also subtracts days from addDays_ but with a decrement and reset. I'd also like to make it calculate leap year dates successfully.
int year;
int month;
int day;
int changeDays;
void DaysToDate::add(int addDays_) {
//Calculate the date add the changeDays.
// Do NOT input values greater than 730 days.
int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int daysInYear = 365;
if (isLeapYear(year) || isLeapYear(year+1) == true) {
monthdays[2] = 29;
daysInYear = 366;
}
if (addDays_ >= daysInYear) {
int addyears = addDays_ / daysInYear;
year += addyears;
if (isLeapYear(year) == true) {
monthdays[2] = 29;
daysInYear = 366;
}
}
int currentDays = day;
int i = month;
if (addDays_ >= monthdays[month]) {
while (addDays_ >= monthdays[i]) {
if (i + 1 >= 12) {
month = 1;
i = 1;
}
else
month++;
addDays_ -= monthdays[i];
//cout << "monthdays[i] "<< monthdays[i] << "\n";
if (addDays_ + day < monthdays[month]) {
day += addDays_;
}
i++;
}
}
if (addDays_ + day < monthdays[month]) {
day += addDays_;
}
if ((day == monthdays[month - 1]) && (month < 12))
{
day = 1;
month++;
}
if ((day == monthdays[month - 1]) && (month == 12))
{
day = 1;
month = 1;
year++;
}
else {
}
cout << year << " " << month << " " << day << " " << "\n";
}
bool DaysToDate::isLeapYear(int year_) {
if (year % 400 == 0) {
return true;
}
if (year % 100 == 0) {
return false;
}
if (year % 4 == 0) {
return true;
}
else {
return false;
}
}
Input 2009/04/05 7
- output 2009 4 12
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了修复代码以在闰年工作:
当然可以对我的建议进行一些改进,但我认为它很容易遵循,因为它的结构与您的非常相似。
作为一般提示,您有很多冗余的 if 语句,其中一些会导致错误,例如
如果您在三月之前开始,检查 isLeapYear(year+1) 会得到错误的天数
For fixing the code to work on leap years:
Surely some improvements can be done to my suggestion but I think it is easy to follow, as it has a very similar structure to yours.
As a general tip, you had a lot of redundant if statements and some of them would cause errors, such as
checking isLeapYear(year+1) will get you a wrong number of days if you start before march
您可以为此使用范围。将给定日期
ymd
添加(或减去)天数n
的操作可以视为:ymd< 的视图/code> 加上以下日期(最多 730 个)。
ranges::views::generate_n
代码将包含向给定日期添加(或减去)一天的逻辑:days_per_month
函数。days_per_month
函数将使用is_leap_year
函数。下面的代码:
[演示]
另一个可能更简单的解决方案是循环,直到消耗完要添加的天数,递增当前日期,直到下个月月初或直到用完要添加的天数(或者如果要减去,则递减直到上个月月底)。再次,我将使用
daysPerMonth
和isLeapYear
辅助函数:[演示]
You could use ranges for this. The operation of adding (or subtracting) a number of days
n
to a given dateymd
may be then thought as:ymd
plus the following dates (a maximum of 730).n
elements.The
ranges::views::generate_n
code will contain the logic for adding (or subtracting) a day to a given date:days_per_month
function.days_per_month
function will make use of theis_leap_year
function.The code below:
ymd
to hold the values of a given date.ymd
.[Demo]
Another, probably simpler, solution would be to loop until you consume the days you want to add, incrementing the current day whether until the beginning of the next month or until you run out of days to add (or decrementing until the end of the previous month if you are subtracting). Again, I would make use of
daysPerMonth
andisLeapYear
helper functions:[Demo]