Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;明天起作用的函数';今天给出的日期';日期_C++ - Fatal编程技术网

C++ C++;明天起作用的函数';今天给出的日期';日期

C++ C++;明天起作用的函数';今天给出的日期';日期,c++,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(Da

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

#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;
}
非常感谢任何帮助

我想你的意思是:

bool variable = getTomorrow(today, tomorrow);
     ^^^^^^^^^^
我想你的意思是:

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

这不是调用函数的正确语法。更改
boolgettomorrow(今天,明天)
to
bool b=getTomorrow(今天,明天)

这不是调用函数的正确语法。更改
boolgettomorrow(今天,明天)
to
bool b=getTomorrow(今天,明天)

类似以下语句:

int xyzzy(42);
是将变量初始化为给定值的一种方法。这就是您的代码发生的情况:

bool getTomorrow(today, tomorrow);
除非编译器抱怨您为初始化器提供了两个值

正确的调用方法是使用以下内容:

bool myBoolVar = getTomorrow(today, tomorrow);
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;
}

而且,为了提供建议,我不太喜欢“只有一个返回点”准则,特别是当它使代码更长、更容易出错时。从这个意义上讲,
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;
}
你会注意到我还输入了代码(在两个函数中)来实际考虑闰年中的2月29日,基于400的倍数和不等于100的倍数的4的倍数都是闰年的规则。如果它不是必需的,只需删除它-我只是一个完整性的坚持者:-)

一个类似以下的语句:

int xyzzy(42);
是将变量初始化为给定值的一种方法。这就是您的代码发生的情况:

bool getTomorrow(today, tomorrow);
除非编译器抱怨您为初始化器提供了两个值

正确的调用方法是使用以下内容:

bool myBoolVar = getTomorrow(today, tomorrow);
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;
}

而且,为了提供建议,我不太喜欢“只有一个返回点”准则,特别是当它使代码更长、更容易出错时。从这个意义上讲,
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;
}

你会注意到我还输入了代码(在两个函数中)来实际考虑闰年中的2月29日,基于400的倍数和不等于100的倍数的4的倍数都是闰年的规则。如果它不是必需的,只需删除它-我只是一个完整性的坚持者:-)

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

试一试

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

bool result=getTomorrow(今天,明天)

以下是一些提示:

在“有效日期”中,您将“今天”作为可写引用传递。您应该使用常量引用

bool有效日期(持续日期和今天){…}

另外,“today”是一个不好的参数名,因为参数名与函数的语义无关

在“getTomorrow”中,通过值传递“today”。您可能应该使用常量引用:


bool getTomorrow(const Date&today,Date&tomorrow){…}

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

试一试

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

bool result=getTomorrow(今天,明天)

以下是一些提示:

在“有效日期”中,您将“今天”作为可写引用传递。您应该使用常量引用

bool有效日期(持续日期和今天){…}

另外,“today”是一个不好的参数名,因为参数名与函数的语义无关

在“getTomorrow”中,通过值传递“today”。您可能应该使用常量引用:


bool getTomorrow(const Date&今天,Date&明天){…}

您需要自己处理吗?标准库使工作变得非常简单(例如,使用
localtime
mktime
)。将此作为一个小练习来编写,但肯定会查看现有库以简化工作。您需要自己处理吗?标准库使工作变得非常简单(例如,使用
localtime
mktime
)。将此作为一个小练习来编写,但肯定会查看现有库以简化工作。非常有用的建议!非常有用的建议!