中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 16:00
47,811 topics / 349,897 posts / today 0 new / 48,255 members
DOS批处理 & 脚本技术(批处理室) » Help me take a look at my code -- to determine a leap year, where is the problem?
Printable Version  3,282 / 16
Floor1 hhl Posted 2006-11-01 22:12
初级用户 Posts 14 Credits 35
set UDAY=0
for /L %%i in (1971,1,%YEAR%) do (
set /a temp1="%%i%%4"
::Remainder when divided by 4
set /a temp2="%%i%%100"
::Remainder when divided by 100
set /a temp3="%%i%%400"
::Remainder when divided by 400
if %temp1%==0 if not %temp2%==0 set /a UDAY="%UDAY%+366"&set TAG=r else if %temp3%==0 set /a UDAY="%UDAY%+366"&set TAG=r else set /a UDAY="%UDAY%+365"&set TAG=p)


::The code is to determine whether it is a leap year and accumulate days
It seems that the tempx is not calculated, and I can't figure it out. Great guys, please give me some pointers, thank you very much!

[ Last edited by hhl on 2006-11-1 at 10:32 PM ]
Floor2 hhl Posted 2006-11-01 22:25
初级用户 Posts 14 Credits 35
There is a problem with the use of variables in the `if` condition. In the `if` statement, when using variables like `%temp1%`, `%temp2%`, `%temp3%`, since it's in a loop, you should use delayed expansion. The corrected code should be like this (assuming delayed expansion is enabled):

```batch
@echo off
setlocal enabledelayedexpansion
set UDAY=0
set YEAR=2023
for /L %%i in (1971,1,%YEAR%) do (
set /a temp1=%%i%%4
::Remainder when divided by 4
set /a temp2=%%i%%100
::Remainder when divided by 100
set /a temp3=%%i%%400
::Remainder when divided by 400
if!temp1!==0 (
if not!temp2!==0 (set /a UDAY=!UDAY!+366&set TAG=r) else if!temp3!==0 (set /a UDAY=!UDAY!+366&set TAG=r) else (set /a UDAY=!UDAY!+365&set TAG=p))
else (set /a UDAY=!UDAY!+365&set TAG=p)
)
endlocal
```

The main issue was that without delayed expansion, the variables `temp1`, `temp2`, `temp3` couldn't get the correct values in the `if` condition during the loop iteration.
Floor3 不得不爱 Posted 2006-11-01 22:36
超级版主 Posts 2,044 Credits 5,310 From 四川南充
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set UDAY=0
for /L %%i in (1971,1,%YEAR%) do (
set/a temp1=%%i%%4
set/a temp2=%%i%%100
set/a temp3=%%i%%400
echo !temp1! !UDAY!
if !temp1!==0 if not !temp2!==0 (set /a UDAY=!UDAY!+366&set TAG=r) else (if !temp3!==0 (set /a UDAY=!UDAY!+366&set TAG=r) else (set /a UDAY=!UDAY!+365&set TAG=p)))
Floor4 hhl Posted 2006-11-01 22:40
初级用户 Posts 14 Credits 35
Ask the super moderator:

What does SETLOCAL ENABLEDELAYEDEXPANSION mean?

What does the echo !temp1! !UDAY! sentence mean?

Why do the tempx and UDAY variables need to be enclosed in !!?
Floor5 NaturalJ0 Posted 2006-11-01 22:44
银牌会员 Posts 533 Credits 1,181
Floor6 NaturalJ0 Posted 2006-11-01 22:45
银牌会员 Posts 533 Credits 1,181
Search the forum for "variable delay".
Floor7 不得不爱 Posted 2006-11-01 22:56
超级版主 Posts 2,044 Credits 5,310 From 四川南充
Originally posted by hhl at 2006-11-1 22:40:
Ask the super moderator:
What does SETLOCAL ENABLEDELAYEDEXPANSION mean?
What does the echo !temp1! !UDAY! mean?
Why do the tempx and UDAY variables need to be enclosed in !!?

The echo !temp1! !UDAY! is for debugging.
You can enter:
for/?
at the command line to know why the tempx and UDAY variables need to be enclosed in !!
But your code still has some problems, let's modify it again:


[ Last edited by 不得不爱 on 2006-11-1 at 10:57 PM ]
Floor8 namejm Posted 2006-11-01 23:04
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Originally posted by hhl at 2006-11-1 22:25:
::Algorithm, copied from the C language book, a leap year is a year divisible by 4 but not by 100; a year divisible by 400 is also a leap year...

  In my impression, as long as a year is divisible by 4, it is a leap year. Why is the algorithm in C so complicated and seems to be against the definition of a leap year?
Floor9 hhl Posted 2006-11-01 23:09
初级用户 Posts 14 Credits 35
Thanks, everyone upstairs, very grateful. Hehe, I'm a new CMD user, and I don't know many grammars and operators clearly.
Floor10 hhl Posted 2006-11-01 23:10
初级用户 Posts 14 Credits 35
Hehe, Moderator namejm, please refer to Tan Haoqiang's classic C book.
Floor11 不得不爱 Posted 2006-11-01 23:18
超级版主 Posts 2,044 Credits 5,310 From 四川南充
Originally posted by namejm at 2006-11-1 23:04:

In my impression, as long as the year is divisible by 4, it is a leap year. Why is the algorithm in C so complicated and seems a bit inconsistent with the definition of a leap year?

Haven't you studied the calendar?
Leap every 4 years
Leap less every 100 years
Leap more every 400 years
The reason is that each year is 365.2422 days
Floor12 namejm Posted 2006-11-01 23:28
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Hehe, I don't know much about the calendar. The concept of leap years even came from the math textbook in primary school. Please forgive me.
Floor13 youxi01 Posted 2006-11-02 00:45
高级用户 Posts 247 Credits 846 From 湖南==》广东
Haha, I didn't know that originally either. I just knew that those divisible by 4 are leap years. Now I know, thanks!
Floor14 pengfei Posted 2006-11-02 03:53
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
The conditions for determining a leap year are:
1. Divisible by 4 but not divisible by 100.
2. Divisible by 100 and also divisible by 400.

C language is really powerful, and the simplest way to determine a leap year is completed by logical operations. It's a pity that batch processing doesn't have such an operation type. I wonder if more complex CMD code can be used to simulate logical operations.





[ Last edited by pengfei on 2006-11-2 at 04:26 AM ]
Floor15 不得不爱 Posted 2006-11-02 04:41
超级版主 Posts 2,044 Credits 5,310 From 四川南充
```@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%i in (1971,1,2008) do (
set /a x=%%i%%4
set /a y=%%i%%100
set /a z=%%i%%400
if !x!==0 if !y!==0 (if !z!==0 set/a t+=1) else set/a t+=1)
set/a t=(2008-1971+1)*365+t
echo The total number of days from 1971 to 2008 is: %t%
pause
```
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023