gpt4 book ai didi

计算明天日期的 C++ 函数给出今天的日期

转载 作者:太空宇宙 更新时间:2023-11-03 10:27:41 25 4
gpt4 key购买 nike

这是我编写的一个函数,它首先检查日期是否有效,然后计算出一天后的日期。为简单起见,闰年不适用于此程序。

#include <iostream>
using namespace std;

// Declaration of Date class
class Date {

public:
int year;
int month;
int day;
};

bool valid_date(Date &today);
bool getTomorrow(Date today, Date &tomorrow);

int main() {
Date today, tomorrow;
today.year=2015;
today.month=2;
today.day=28;

bool getTomorrow(today, tomorrow);
}

bool valid_date(Date &today) {
bool valid = true;
const int days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};
if (today.month<1 || today.month > 12) {
valid = false;
}
else if (today.day <1 || today.day > days[today.month-1]) {
valid = false;
}
return valid;
}


bool getTomorrow(Date today, Date &tomorrow) {
bool valid = true;
const int days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};

if (valid_date(today)==false) {
valid = false;
}
else if (today.day==31 && today.month==12) {
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year= today.year +1;

}
else if (today.day == days[today.month-1]) {
tomorrow.day = 1;
tomorrow.month = today.month +1;
tomorrow.year = today.year;
}
else {
tomorrow.day = today.day + 1;
tomorrow.month = today.month;
tomorrow.year= today.year;
}

return valid;
}

它无法运行,Xcode 向我发出此行的“标量初始化程序中的多余元素”警告:

bool getTomorrow(today, tomorrow);

非常感谢任何帮助!

最佳答案

这样的语句:

int xyzzy(42);

是一种将变量初始化为给定值的方法。这就是您的代码所发生的情况:

bool getTomorrow(today, tomorrow);

除了编译器提示您为初始化程序提供了两个值。

调用它的正确方法是:

bool myBoolVar = getTomorrow(today, tomorrow);

而且,为了提供建议,我不是“只有一个返回点”准则的忠实拥护者,尤其是当它使您的代码更长并且更容易出错时。从这个意义上讲,valid_date() 可以写得更简洁,包括分解 days[] 数组,因为它在多个地方使用并且永远不会改变:

static const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};

bool valid_date (Date today) {
// Check month first.
if (today.month < 1 || today.month > 12)
return false;

// Allow Feb 29 in leap year if needed.
if (today.month == 2 && today.day == 29) {
if (today.year % 400 == 0)
return true;
if ((today.year % 4 == 0) && (today.year % 100 != 0))
return true;
}

// Then check day.
if (today.day < 1 || today.day > days[today.month-1])
return false;

return true;
}

出于同样的原因,获取明天的代码似乎也有点折磨人,所以我会看类似的东西:

bool getTomorrow (Date today, Date &tomorrow) {
// Don't do anything for bad dates.
if (!valid_date (today)) return false;

// Just blindly add a day with no checks.
tomorrow.year = today.year;
tomorrow.month = today.month;
tomorrow.day = today.day + 1;

// Allow Feb 29 in leap year if needed.
if (tomorrow.month == 2 && tomorrow.day == 29) {
if (tomorrow.year % 400 == 0)
return true;
if ((tomorrow.year % 4 == 0) && (tomorrow.year % 100 != 0))
return true;
}

// Catch rolling into new month.
if (tomorrow.day > days[tomorrow.month-1]) {
tomorrow.day = 1;
tomorrow.month++;

// Catch rolling into new year.
if (tomorrow.month == 13) {
tomorrow.month = 1;
tomorrow.year++;
}
}

return true;
}

您会注意到我还放入了代码(在两个函数中)以实际允许闰年的 2 月 29 日,基于 400 的倍数和 4 的倍数不是 100 的倍数的规则, 是闰年。如果不需要,只需将其删除 - 我只是坚持完整性:-)

关于计算明天日期的 C++ 函数给出今天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27852975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com