我想在ABAP中编写Hanoi塔求解器。
这是我当前的代码:
FORM SOLVE USING s1 TYPE c
s2 TYPE c
s3 TYPE c
n TYPE i.
IF n = 1.
"move disc from tower X to tower Y"
WRITE:/ text-001, text-002, s1, text-003, text-002, s3.
ELSE.
PERFORM solve USING s1 s3 s2 n-1. "Here's my problem, n-1 doesn't work"
PERFORM solve USING s1 s2 s3 1.
PERFORM solve USING s2 s1 s3 n-1.
ENDIF.
ENDFORM.
我的问题:
如果我选择了USING
之类的n-1
参数,SAP会将其识别为(不存在的)结构,并向我抛出错误。
反正有办法做到吗?
非常感谢。
答案 0 :(得分:5)
您需要在运算符之间添加空格。因此,您需要输入n - 1
。另一方面,旧版本的ABAP不支持将内联操作数用作函数/执行参数。如果遇到错误,请首先将结果分配给新变量,然后将其作为参数传递。
答案 1 :(得分:0)
这是我的解决方案。效果很好。感谢您提供的有用提示。
*&---------------------------------------------------------------------*
*& Report ZS_SANDBOX_TOH
*&
*&---------------------------------------------------------------------*
*&
*& Datum: 19.09.2018
*& Beschreibung: Lösungsprogramm für das Türme von Hanoi - Problem
*&---------------------------------------------------------------------*
REPORT ZS_SANDBOX_TOH.
PARAMETERS: p_discs TYPE i.
CLASS myclass DEFINITION.
PUBLIC SECTION.
CLASS-METHODS solve IMPORTING s1 TYPE c
s2 TYPE c
s3 TYPE c
n1 TYPE i.
ENDCLASS.
CLASS myclass IMPLEMENTATION.
METHOD solve.
IF n1 = 1.
WRITE:/ 'Lege Scheibe von Turm ', s1, ' auf Turm ', s3.
ELSE.
solve( s1 = s1
s2 = s3
s3 = s2
n1 = n1 - 1 ).
solve( s1 = s1
s2 = s2
s3 = s3
n1 = 1 ).
solve( s1 = s2
s2 = s1
s3 = s3
n1 = n1 - 1 ).
ENDIF.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
CALL METHOD myclass=>solve( s1 = 'A'
s2 = 'B'
s3 = 'C'
n1 = p_discs ).