|
hhl
初级用户
 
积分 35
发帖 14
注册 2006-10-16
状态 离线
|
『楼 主』:
帮忙看看我这段代码--判断闰年,哪里有问题?
使用 LLM 解释/回答一下
set UDAY=0
for /L %%i in (1971,1,%YEAR%) do (
set /a temp1="%%i%%4"
::除4取余数
set /a temp2="%%i%%100"
::除100取余数
set /a temp3="%%i%%400"
::除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)
::代码是判断是否闰年并累加天数
好像 tempx那里不计算,百思不得其解,大虾们,指点一下迷津,多谢多谢!
Last edited by hhl on 2006-11-1 at 10:32 PM ]
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 ]
|
|
2006-11-1 22:12 |
|
|
hhl
初级用户
 
积分 35
发帖 14
注册 2006-10-16
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
上面的算法有问题,调整一下:
set UDAY=0
for /L %%i in (1971,1,%YEAR%) do (
set /a temp1="%%i%%4"
::除4取余数
set /a temp2="%%i%%100"
::除100取余数
set /a temp3="%%i%%400"
::除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
)
::算法,抄C语言书上的,能被4整除,但不能被100整除的是闰年;能被400整除的也是闰年
帮帮看看tempx那里用set /a计算有什么问题?
Last edited by hhl on 2006-11-1 at 10:30 PM ]
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.
|
|
2006-11-1 22:25 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
@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)))
@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)))
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-11-1 22:36 |
|
|
hhl
初级用户
 
积分 35
发帖 14
注册 2006-10-16
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
请教超版:
SETLOCAL ENABLEDELAYEDEXPANSION 什么意思?
echo !temp1! !UDAY! 这句又是什么意思?
为什么tempx,UDAY变量要用!!包起来?
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 !!?
|
|
2006-11-1 22:40 |
|
|
NaturalJ0
银牌会员
    
积分 1181
发帖 533
注册 2006-8-14
状态 离线
|
|
2006-11-1 22:44 |
|
|
NaturalJ0
银牌会员
    
积分 1181
发帖 533
注册 2006-8-14
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
在论坛搜下“变量延迟”。
Search the forum for "variable delay".
|
|
2006-11-1 22:45 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by hhl at 2006-11-1 22:40:
请教超版:
SETLOCAL ENABLEDELAYEDEXPANSION 什么意思?
echo !temp1! !UDAY! 这句又是什么意思?
为什么tempx,UDAY变量要用!!包起来?
echo !temp1! !UDAY! 这句是调试用的
你在命令行下输入:
for/?
就知道为什么tempx,UDAY变量要用!!包起来了
不过你的代码还有点问题,再改下:
SETLOCAL ENABLEDELAYEDEXPANSION
set UDAY=0
set YEAR=2000
for /L %%i in (1971,1,%YEAR%) do (
set/a temp1=%%i%%4
set/a temp2=%%i%%100
set/a temp3=%%i%%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)
Last edited by 不得不爱 on 2006-11-1 at 10:57 PM ]
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:
SETLOCAL ENABLEDELAYEDEXPANSION
set UDAY=0
set YEAR=2000
for /L %%i in (1971,1,%YEAR%) do (
set/a temp1=%%i%%4
set/a temp2=%%i%%100
set/a temp3=%%i%%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)
Last edited by 不得不爱 on 2006-11-1 at 10:57 PM ]
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-11-1 22:56 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by hhl at 2006-11-1 22:25:
::算法,抄C语言书上的,能被4整除,但不能被100整除的是闰年;能被400整除的也是闰年 ...
在我的印象中,只要能被4整除的年份都是闰年,怎么C里的算法竟然如此复杂、并且似乎有点不合闰年的定义呢?
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?
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-1 23:04 |
|
|
hhl
初级用户
 
积分 35
发帖 14
注册 2006-10-16
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
谢谢,楼上各位,非常感谢。呵呵,我是CMD新手,很多语法,运算符都不清楚。
Thanks, everyone upstairs, very grateful. Hehe, I'm a new CMD user, and I don't know many grammars and operators clearly.
|
|
2006-11-1 23:09 |
|
|
hhl
初级用户
 
积分 35
发帖 14
注册 2006-10-16
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
呵呵,namejm版主,请参考谭浩强的那本经典的C.
Hehe, Moderator namejm, please refer to Tan Haoqiang's classic C book.
|
|
2006-11-1 23:10 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2006-11-1 23:04:
在我的印象中,只要能被4整除的年份都是闰年,怎么C里的算法竟然如此复杂、并且似乎有点不合闰年的定义呢?
你没有学过历法吗?
4年1润
100年少1润
400年多1润
原因是每年是365.2422天
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
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-11-1 23:18 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
呵呵,对历法了解不多,闰年的概念甚至来自于小学时的数学课本,各位见笑了。
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.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-1 23:28 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
哈哈,我原来也不知道哦,也知道被4整除的才是闰年,现在知道了,感谢啊!
Haha, I didn't know that originally either. I just knew that those divisible by 4 are leap years. Now I know, thanks!
|
|
2006-11-2 00:45 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
判断是否为闰年条件是:
1. 能被4整除, 但不能被100整除.
2. 能被100整除, 又能被400整除.
C语言真的好强, 而判断是否闰年最简单的是逻辑运算来完成了, 遗憾的是批处理中并没有这样的运算类型. 我想是不是可以通过更复杂的CMD代码来模拟逻辑运算.
@echo off
set /p year=请输入年份:
set /a x=%year%%%4
set /a y=%year%%%100
set /a z=%year%%%400
if %x%==0 (
if %y%==0 (
if %z%==0 (
set term=1
) else (
set term=0
)
) else (
set term=1
)
) else (
set term=0
)
if %term%==1 (
echo %year% is a leap year.
) else (
echo %year% is not a leap year.
)
pause
@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 term=1
) else (
set term=0
)
) else (
set term=1
)
) else (
set term=0
)
if !term!==1 (
set /a uday+=366
) else (
set /a uday+=365
)
)
echo 1971-2008累计天数为:%uday%
pause
Last edited by pengfei on 2006-11-2 at 04:26 AM ]
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.
@echo off
set /p year=Please enter the year:
set /a x=%year%%%4
set /a y=%year%%%100
set /a z=%year%%%400
if %x%==0 (
if %y%==0 (
if %z%==0 (
set term=1
) else (
set term=0
)
) else (
set term=1
)
) else (
set term=0
)
if %term%==1 (
echo %year% is a leap year.
) else (
echo %year% is not a leap year.
)
pause
@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 term=1
) else (
set term=0
)
) else (
set term=1
)
) else (
set term=0
)
if !term!==1 (
set /a uday+=366
) else (
set /a uday+=365
)
)
echo The total number of days from 1971 to 2008 is:%uday%
pause
Last edited by pengfei on 2006-11-2 at 04:26 AM ]
|
|
2006-11-2 03:53 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
@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 1971-2008累计天数为:%t%
pause
```@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
```
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-11-2 04:41 |
|