Leap Year Basic C++Boolean始终返回true

Leap Year Basic C++ Boolean Always returns true

本文关键字:返回 true C++Boolean Year Basic Leap      更新时间:2023-10-16

我写这段代码是为了确定输入的年份是否是闰年。也就是说,那些可以被4和400整除的年份是闰年,而被100或其他东西整除的则不是闰年。

但是,我的程序总是为布尔值返回true,这样输出就会认为每年都是闰年。

到目前为止,这是我的代码:

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
 bool leap_year(int year);
int main()
{
    int year; 
     bool leap_year(int year);
    cout << "Please input the year in question: ";
    cin >> year;
    if (leap_year == false)
    {
        cout << "The year is not a leap year.  ";
    }
    else
    {
        cout << "The year is a leap year. ";
    }
    return 0;
}
  bool leap_year(int year)
{
    if (year % 4 == 0)
    {
        bool leap_year = true;
    }
    else if (year % 400 == 0)
    {
        bool leap_year = true;
    }
    else if (year % 100 == 0)
    {
        bool leap_year = false;
    }
    else
    {
        bool leap_year = false;
    }
    if (bool leap_year = false)
    {
        return false;
    }
    else
    {
        return true;
    }
}

您正在声明一堆局部变量

else if (year % 100 == 0)
{
    bool leap_year = false;
}

一旦出现}括号,这个变量就会超出范围,您存储的值也会超出范围。

if (bool leap_year = false)
{
    return false;
}

这是定义一个变量leap_year并将其赋值为false。if反过来将求值为false,因此它将始终转到else条件。

我有权改写程序的一部分。现在,您可以看到对函数的调用是如何进行的。此外,在函数中,局部变量is_leap_year用于存储返回值,并最终返回。我还纠正了逻辑,因为前面的第一个%4检查将为true,而不会执行其他if语句,这不是您想要的。

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
bool leap_year(int year);
int main()
{
    int year; 
    cout << "Please input the year in question: ";
    cin >> year;
    if (leap_year(year) == false)  //Call the function and check if return is false
    {
        cout << "The year is not a leap year.  ";
    }
    else
    {
        cout << "The year is a leap year. ";
    }
    return 0;
}
  bool leap_year(int year)
{
    bool is_leap_year = false;
    if (year % 4 == 0)
    {
       is_leap_year = true;
    }
    if (year % 100 == 0)
    {
        is_leap_year = false;
    }
    if (year % 400 == 0)
    {
        is_leap_year = true;
    }
    return is_leap_year;
}

它应该是IF((年份%4==0&&年份%100!=0)||(年份%400==0))return ISLEAPYEAR。

//You can simplify it like this:
bool leap_year(int year)
{
    bool leap_year  = false;
    if ((year % 4 == 0 && year%100 !=0) || year % 400 == 0)
    {
        bool leap_year = true;
    }
    return leap_year; 
}

为了避免这种情况:

if (leap_year = false)
    {
        return false;
    }

您可以检查类似的布尔值

if (leap_year)
 {
        // code to execute when leap_year is true
 }

您的问题是您是否返回truefalse的条件是:

if (bool leap_year = false)
{
    return false;
}
else
{
    return true;
}

您正在将值false分配给在条件语句中初始化的变量leap_year。赋值运算符被定义为:T& T::operator =(const T2& b),这意味着你总是在你的条件下计算false

解决这个问题的一种方法是在bool leap_year(int year)的顶部声明leap_year,而不是每次使用它(这是毫无意义的行为)

bool leap_year(int year) {
    bool leap_year = false;
    if (year % 4 == 0) {
        leap_year = true;
    } else if (year % 400 == 0) {
        leap_year = true;
    } else if (year % 100 == 0) {
        leap_year = false;
    }
    if (leap_year == false) {
        return false;
    } else {
        return true;
    }
}

但更好的解决方案是使用C++已经提供的功能:

bool leap_year(int year) {
    tm bar = { 0, 0, 0, 29, 1, year - 1900 };
    return static_cast<time_t>(-1) != mktime(&bar) && bar.tm_mday == 29 && bar.tm_mon == 1 && bar.tm_year == year - 1900;
}

[实际示例]