注册 登录
编程论坛 C++教室

从红色部分开始标的的逻辑是什么,看不懂,求指教

随风晨爱 发布于 2018-11-22 18:16, 1507 次点击
用嵌套if语句实现判断一个年份是否为闰年,有几处看不懂的,求指教,谢谢
闰年,1.能被4整除,但不能被一百整除。2.能被400 整除的年份都是闰年
嵌套if语句如下:
#include <iostream>
using namespace std ;
int main ()
{
    int year;
    bool leap;
    cout << "please enter year" << endl;
    cin >> year;
    if ( year % 4==0)
    {
        if (year % 100 ==0)
        {
            if (year % 400==0) leap=true;
            else leap=false;
        }
        else leap=true;
    }
    else leap=false;
    if (leap)
        cout << year <<"is";
    else
        cout << year <<"is not";
    cout << " a leap year." << endl;
    return 0;
}

从红色部分开始标的的逻辑是什么,看不懂
1 回复
#2
rohalloway2018-11-22 19:15
程序代码:

if (leap) //leap是bool类型,所以就等于if (leap == true)
    {
        cout << year << "is"; //if、for等代码段只有一行时,可以省略花括号
    }
    else
    {
        cout << year << "is not";
    }
    cout << " a leap year." << endl;
    return 0;
1