这个递归 Windows CMD 脚本有什么问题?它不会正确地做阿克曼

发布于 2024-08-29 19:46:34 字数 1596 浏览 6 评论 0原文

我正在尝试计算阿克曼函数。我想要实现的目标的描述位于 http://rosettacode.org/wiki/Ackermann_function

使用测试脚本,Test 0 4 给我 5,这是正确的。然而,测试 1 4 给出 5 而不是 6,测试 2 4 给出 5 而不是 11。

我哪里出错了?

::echo off
set depth=0
:ack
if %1==0 goto m0
if %2==0 goto n0

:else
set /a n=%2-1
set /a depth+=1
call :ack %1 %n%
set t=%errorlevel%
set /a depth-=1
set /a m=%1-1
set /a depth+=1
call :ack %m% %t%
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )

:m0
set/a n=%2+1
if %depth%==0 ( exit %n% ) else ( exit /b %n% )

:n0
set /a m=%1-1
set /a depth+=1
call :ack %m% %2
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )

我使用此脚本来测试它

@echo off
cmd/c ackermann.cmd %1 %2
echo Ackermann of %1 %2 is %errorlevel%

测试 1 1 的示例输出给出:

>test 1 1
>set depth=0
>if 1 == 0 goto m0
>if 1 == 0 goto n0
>set /a n=1-1
>set /a depth+=1
>call :ack 1 0
>if 1 == 0 goto m0
>if 0 == 0 goto n0
>set /a m=1-1
>set /a depth+=1
>call :ack 0 0
>if 0 == 0 goto m0
>set/a n=0+1
>if 2 == 0 (exit 1  )  else (exit /b 1  )
>set t=1
>set /a depth-=1
>if 1 == 0 (exit 1  )  else (exit /b 1  )
>set t=1
>set /a depth-=1
>set /a m=1-1
>set /a depth+=1
>call :ack 0 1
>if 0 == 0 goto m0
>set/a n=1+1
>if 1 == 0 (exit 2  )  else (exit /b 2  )
>set t=2
>set /a depth-=1
>if 0 == 0 (exit 2  )  else (exit /b 2  )
Ackermann of 1 1 is 2

I'm trying to get to calculate the Ackermann function. A description of what I'm trying to achieve is at http://rosettacode.org/wiki/Ackermann_function.

Using the test script, Test 0 4 gives me 5 which is correct. However Test 1 4 gives 5 not 6, and Test 2 4 gives 5 instead of 11.

Where am I going wrong?

::echo off
set depth=0
:ack
if %1==0 goto m0
if %2==0 goto n0

:else
set /a n=%2-1
set /a depth+=1
call :ack %1 %n%
set t=%errorlevel%
set /a depth-=1
set /a m=%1-1
set /a depth+=1
call :ack %m% %t%
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )

:m0
set/a n=%2+1
if %depth%==0 ( exit %n% ) else ( exit /b %n% )

:n0
set /a m=%1-1
set /a depth+=1
call :ack %m% %2
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )

I use this script to test it

@echo off
cmd/c ackermann.cmd %1 %2
echo Ackermann of %1 %2 is %errorlevel%

A sample output, for Test 1 1, gives:

>test 1 1
>set depth=0
>if 1 == 0 goto m0
>if 1 == 0 goto n0
>set /a n=1-1
>set /a depth+=1
>call :ack 1 0
>if 1 == 0 goto m0
>if 0 == 0 goto n0
>set /a m=1-1
>set /a depth+=1
>call :ack 0 0
>if 0 == 0 goto m0
>set/a n=0+1
>if 2 == 0 (exit 1  )  else (exit /b 1  )
>set t=1
>set /a depth-=1
>if 1 == 0 (exit 1  )  else (exit /b 1  )
>set t=1
>set /a depth-=1
>set /a m=1-1
>set /a depth+=1
>call :ack 0 1
>if 0 == 0 goto m0
>set/a n=1+1
>if 1 == 0 (exit 2  )  else (exit /b 2  )
>set t=2
>set /a depth-=1
>if 0 == 0 (exit 2  )  else (exit /b 2  )
Ackermann of 1 1 is 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拒绝两难 2024-09-05 19:46:34

将上面第 27 行从

call :ack %m% %2 

改为

call :ack %m% 1

Change line 27 above from

call :ack %m% %2 

to

call :ack %m% 1
天煞孤星 2024-09-05 19:46:34

你的变量 m 和 n 的范围是什么?

您可以在一次调用 :ack 中设置值,然后再次递归调用,设置值。你会覆盖它们吗?在基于堆栈的语言(例如 C 和 Java)中,局部变量很好,每一级递归都会获得新变量。 cmd 中会发生什么?

What is the scope of your variables m and n?

You set values within one call to :ack, and then recursively call again, setting values. Do you overwrite them? In Stack-based languages such as C and Java, local variables are fine, each level of recursion gets new variables. What happens in cmd?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文