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

时间:2015-01-09 02:33:05

标签: c++

这是我写的一个函数,它首先检查日期是否有效,然后计算出日期后的日期。为了简化事情,闰年不适用于此计划。

#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在标量初始化程序中给了我这个警告和多余的元素&#39;对于这一行:

bool getTomorrow(today, tomorrow);

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:4)

如下的陈述:

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的倍数的规则#39 ; t也是100的倍数,是闰年。如果不需要,只需删除它 - 我只是一个完整性的坚持者: - )

答案 1 :(得分:2)

我认为你的意思是:

bool variable = getTomorrow(today, tomorrow);
     ^^^^^^^^^^

答案 2 :(得分:2)

调用函数的语法不正确。将bool getTomorrow(today, tomorrow);更改为bool b = getTomorrow(today, tomorrow);

答案 3 :(得分:0)

我没有看过你的函数是否正确,但你的代码没有编译,因为你没有声明一个变量来保存函数调用的结果。

bool结果(getTomorrow(今天,明天));

bool result = getTomorrow(今天,明天);

以下是一些提示:

在“valid_date”中,您将“今天”作为可写参考传递。你应该使用const引用

bool valid_date(const Date&amp; Today){...}

“today”也是一个错误的参数名称,因为参数名称与函数的语义无关。

在“getTomorrow”中你通过值传递“今天”。您应该使用const引用:

bool getTomorrow(const Date&amp; today,Date&amp; tomorrow){...}