说我有数字123456,如何将6移到开头,使其变为612345?
如果数字的位数较少,例如123变为312,则需要工作。
非常感谢。
答案 0 :(得分:4)
这是一种通用方法,适用于2到32位之间的任何整数。该数字必须为usage display
。
working-storage section.
1 a-number pic 9(6) value 123456.
1 b-number pic 9(3) value 123.
1 work pic x(32).
1 len-of-number binary pic 9(4).
procedure division.
begin.
display a-number
move a-number (1:) to work
perform swap-digit
move work to a-number (1:)
display a-number
display space
display b-number
move b-number (1:) to work
perform swap-digit
move work to b-number (1:)
display b-number
stop run
.
swap-digit.
move 0 to len-of-number
inspect work tallying
len-of-number for characters before space
move function reverse (work (1:len-of-number))
to work
move function reverse (work (2:len-of-number - 1))
to work (2:len-of-number - 1)
.
输出:
123456
612345
123
312
答案 1 :(得分:1)
这是解决方案。只需MOVE
您对VAR-INPUT
的输入。这是一个字母数字的图片子句,但这并不重要,因为您希望将字符串作为输出。
我还选择了随机图片子句的长度,您可以选择任意长度。
WORKING-STORAGE SECTION.
01 VAR-INPUT PIC X(20).
01 VAR1 PIC X(10).
01 VAR2 PIC X(10).
01 RESULT PIC X(20).
01 L PIC 9(02).
01 OFFSET PIC 9(02).
PROCEDURE DIVISION.
COMPUTE L = LENGTH OF VAR-INPUT
COMPUTE OFFSET = L - 1
MOVE VAR-INPUT(1:L) TO VAR1
MOVE VAR-INPUT(OFFSET:1) TO VAR2
STRING VAR1 DELIMITED BY SPACE
VAR2 DELIMITED BY SPACE
INTO RESULT
答案 2 :(得分:1)
这是来自OpenCobolIDE的有效示例
IDENTIFICATION DIVISION.
PROGRAM-ID. YOUR-PROGRAM-NAME.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 A PIC X(37).
01 B PIC 9(2).
01 C PIC X(1).
01 D PIC 9(2).
01 E PIC 9(2).
PROCEDURE DIVISION.
ACCEPT A.
INSPECT A TALLYING B FOR CHARACTERS BEFORE SPACE.
MOVE A(B:) TO C.
PERFORM VARYING D FROM B BY -1 UNTIL D = 1
MOVE D TO E
SUBTRACT 1 FROM E
MOVE A(E:1) TO A(D:1)
END-PERFORM.
MOVE C TO A(1:1).
DISPLAY A.
END PROGRAM YOUR-PROGRAM-NAME.
在PERFORM循环中插入一个DISPLAY A,以查看工作流程如何进行。