批处理文件leap年? [英] Batch file leap year?

查看:92
本文介绍了批处理文件leap年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何创建一个批处理文件程序,如果我输入年份,该程序将识别是否为a年.

How should I create a batch file program that if I input a year the program will identify if it is a leap year or not.

@echo off
cls
echo.  LEAP YEAR
echo. Enter Year:
exit /b 0

好吧,只是我需要帮助,例如关于应该怎么做的建议.

Well, its just that i need help like suggestions to what should i do.

推荐答案

@echo off
setlocal

set /P "year=Enter year: "
set /A "leap=!(year%%4) + (!!(year%%100)-!!(year%%400))"
if %leap% equ 1 echo Is leap year

根据维基百科,如果一年可以被4整除,则是leap年.可被100整除,在这种情况下,仅当它也可被400整除时才是飞跃(可整除"表示除以给定数字的余数为零).这样,2000和2400是leap年,因为它们除以400时的余数为零,而2100、2200和2300则不是:这是特殊情况,因为它们除以100时的余数为零.

Accordingly to Wikipedia a year is leap if it is divisible by 4 excepting if it is also divisible by 100, in which case it is leap only if it is also divisible by 400 ("divisible" means that the remainder of the division by the given number is zero). This way, 2000 and 2400 are leap years because their remainders when they are divided by 400 are zero, but 2100, 2200 and 2300 are not: these are special cases because their remainders when they are divided by 100 are zero.

set /A命令中,如果!布尔型NOT运算符的操作数为零,则其值为1;在其他情况下,其值为0;因此,如果年份可被4整除,则set /A "leap=!(year%%4)"为1;而在其他情况下,则为零.这给出了结果的第一部分.

In set /A command the ! boolean NOT operator gives 1 if its operand is zero and gives 0 in any other case, so set /A "leap=!(year%%4)" gives 1 if the year is divisible by 4 and zero in any other case; this gives the first part of the result.

此后,我们需要在2100、2200和2300年中从该值中减去1,但在2000和2400年中则不减去任何值;即:

After that we need to subtract 1 from this value in years 2100, 2200 and 2300, but subtract nothing in years 2000 and 2400; that is:

year    year%%100    a=!!(year%%100)    year%%400    b=!!(year%%400)    a-b
2000    0              0                0              0                 0
2100    0              0                100            1                 -1
2200    0              0                200            1                 -1
2300    0              0                300            1                 -1
2400    0              0                0              0                 0

如果年份不能被100整除,则ab的值均等于1,因此a-b为零,结果仅由原始余数给出4.

If the year is not divisible by 100 then both a and b values are equal to 1, so a-b is zero and the result is given just by the original remainder by 4.

这样,公式set /A "leap=!(year%%4) + (!!(year%%100)-!!(year%%400))"给出了完整的结果.

This way, the formula set /A "leap=!(year%%4) + (!!(year%%100)-!!(year%%400))" gives the complete result.

这篇关于批处理文件leap年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆