标题: 帮忙看看我这段代码--判断闰年,哪里有问题?
[打印本页]
作者: hhl
时间: 2006-11-1 22:12
标题: 帮忙看看我这段代码--判断闰年,哪里有问题?
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 ]
作者: hhl
时间: 2006-11-1 22:25
上面的算法有问题,调整一下:
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 ]
作者: 不得不爱
时间: 2006-11-1 22:36
@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)))
作者: hhl
时间: 2006-11-1 22:40
请教超版:
SETLOCAL ENABLEDELAYEDEXPANSION 什么意思?
echo !temp1! !UDAY! 这句又是什么意思?
为什么tempx,UDAY变量要用!!包起来?
作者: NaturalJ0
时间: 2006-11-1 22:44
加上括号。
作者: NaturalJ0
时间: 2006-11-1 22:45
在论坛搜下“变量延迟”。
作者: 不得不爱
时间: 2006-11-1 22:56
Quote: |
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 ]
作者: namejm
时间: 2006-11-1 23:04
Quote: |
Originally posted by hhl at 2006-11-1 22:25:
::算法,抄C语言书上的,能被4整除,但不能被100整除的是闰年;能被400整除的也是闰年 ... |
|
在我的印象中,只要能被4整除的年份都是闰年,怎么C里的算法竟然如此复杂、并且似乎有点不合闰年的定义呢?
作者: hhl
时间: 2006-11-1 23:09
谢谢,楼上各位,非常感谢。呵呵,我是CMD新手,很多语法,运算符都不清楚。
作者: hhl
时间: 2006-11-1 23:10
呵呵,namejm版主,请参考谭浩强的那本经典的C.
作者: 不得不爱
时间: 2006-11-1 23:18
Quote: |
Originally posted by namejm at 2006-11-1 23:04:
在我的印象中,只要能被4整除的年份都是闰年,怎么C里的算法竟然如此复杂、并且似乎有点不合闰年的定义呢? |
|
你没有学过历法吗?
4年1润
100年少1润
400年多1润
原因是每年是365.2422天
作者: namejm
时间: 2006-11-1 23:28
呵呵,对历法了解不多,闰年的概念甚至来自于小学时的数学课本,各位见笑了。
作者: youxi01
时间: 2006-11-2 00:45
哈哈,我原来也不知道哦,也知道被4整除的才是闰年,现在知道了,感谢啊!
作者: pengfei
时间: 2006-11-2 03:53
判断是否为闰年条件是:
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 ]
作者: 不得不爱
时间: 2006-11-2 04:41
@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
作者: xycoordinate
时间: 2007-2-28 21:54
Quote: |
@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
) |
|
看的眼都花了!
再学习:namejm的《if……else……语句中值得注意的一个问题》
http://www.cn-dos.net/forum/view ... =1&highlight=if
修改一点:
Quote: |
@echo off
set /p year=请输入年份:
set /a x=%year%%%4
set /a y=%year%%%100
set /a z=%year%%%400
set term=0
if %z% == 0 set term=1
if %x% == 0 (if not %y% == 0 set term=1) |
|
[
Last edited by xycoordinate on 2007-2-28 at 09:04 AM ]
作者: slore
时间: 2007-2-28 22:51
判断是否为闰年条件是:
1. 能被4整除, 但不能被100整除.
2. 能被100整除, 又能被400整除.
不矛盾么?
能被400和100整除第一条能满足?
先判断2不满足2后再看1...
世纪年必须被400整除才是,非世纪年能被4整除就可以了。