如何在Cobol语言中制作一个像金字塔一样显示的循环


IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NAME PIC A(9) VALUE 'FELIX'.

还有其他循环方式吗?

PROCEDURE DIVISION.
PERFORM A-PARA THRU E-PARA.
A-PARA.
DISPLAY 'L'.
B-PARA.
DISPLAY 'I I I'.
C-PARA.
DISPLAY 'A A A A A'.
D-PARA.
DISPLAY 'M M M M M M M'.
This is the Output:
L
III
AAAAA
MMMMMMM
My expecting Output Should be:
L
III
AAAAA
MMMMMMM

感谢您的提前回复。我对这种编程语言很陌生,而且很困惑。

真的不应该让别人做作业。

我是在openvms机器上完成的。无论您使用什么COBOL环境,您都必须进行调整。

$ cob/reserved_words=200x PYRAMID.COB
$ LINK PYRAMID
$ RUN PYRAMID
*     
***    
*****   
*******  
********* 

如果你可以访问一个好的VT-100模拟器,你可以在同一个地方获得一个免费帐户。

https://eisner.decus.org/

IDENTIFICATION DIVISION.
PROGRAM-ID.    A.
*>  COB/RESERVED_WORDS=200X PYRAMID.COB
*>  LINK PYRAMID
*>  RUN PYRAMID
DATA DIVISION.
WORKING-STORAGE SECTION.

01 MY-NUMBERS.
05 START-POS    PIC 9(4) USAGE IS COMP VALUE IS ZERO.
05 END-POS      PIC 9(4) USAGE IS COMP VALUE IS ZERO.
05 SUB-POS      PIC 9(4) USAGE IS COMP.
05 DISTANCE     PIC 9(4) USAGE IS COMP.

77 MAX-WIDTH       PIC 9(4) USAGE IS COMP VALUE IS 10.
77 CENTER-POS      PIC 9(4) USAGE IS COMP VALUE IS 5.
01 MY-DISPLAYS.
05 DISPLAY-LINE.
10 DISPLAY-CELL   PIC X OCCURS 10.

PROCEDURE DIVISION.
A000-MAIN.
PERFORM B000-CREATE-PYRAMID
VARYING DISTANCE FROM 0 BY 1
UNTIL DISTANCE IS GREATER THAN 4.
STOP RUN.
B000-CREATE-PYRAMID.
MOVE SPACES TO DISPLAY-LINE.
SUBTRACT DISTANCE FROM CENTER-POS GIVING START-POS.
ADD DISTANCE TO CENTER-POS GIVING END-POS.
PERFORM C000-FILL-LINE
VARYING SUB-POS FROM START-POS BY 1
UNTIL SUB-POS IS GREATER THAN END-POS.
DISPLAY DISPLAY-LINE.
C000-FILL-LINE.
MOVE '*' TO DISPLAY-CELL (SUB-POS).

要在COBOL中创建基于字符的金字塔,首先要确定的是金字塔底部的宽度。这取决于电平的数量;斜率";侧面。对于示例1,以每行一个字符的斜率,基数将为(2 * (number-of-levels - 1)) + 1,或为基数宽度的(2 * (4 - 1)) + 1 = 7

接下来找到金字塔顶部的起始位置(中点(。这是(width-of-base + 1) / 2。对于示例1,(7 + 1) / 2 = 4

对于棱锥体的每个级别(PERFORM VARYING(,将起始位置减少1,将宽度增加2。

对于实施例2;斜率";每行使用两个字符。底部为13,起始位置为7,起始位置减小2("斜率"(,宽度增加4。

对于示例1和2,将星号(**(插入到要放置的字符的位置,然后使用INSPECT语句将星号更改为正确的字符。CCD_ 9也是一个";循环;声明(内部(。

对于实施例3,一种金字塔状结构;斜率";其中之一是根据文本构建的。层数、宽度和起点是根据要显示的文本的长度计算的。


代码:

data division.
working-storage section.
1 pyramid-chars pic x(4) value "LIAM".
1 level-number comp pic 9(4).
1 start-point comp pic 9(4).
1 width comp pic 9(4).
1 display-line pic x(50).
1 number-of-levels comp pic 9(4).
1 max-move comp pic 9(4).
1 text-ptr comp pic 9(4).
1 para-text pic x(116) value "This is a long paragraph to "
& "demonstrate building a pyramid from text. The only "
& "explicit loop is the PERFORM VARYING.".
procedure division.
* Example 1
move space to display-line
move 4 to start-point
move 1 to width
perform varying level-number from 1 by 1
until level-number > 4
move all "*" to display-line (start-point:width)
inspect display-line replacing
all "*" by pyramid-chars (level-number:1)
subtract 1 from start-point
add 2 to width
display display-line
end-perform
* Example 2
display space
move space to display-line
move 7 to start-point
move 1 to width
perform varying level-number from 1 by 1
until level-number > 4
move all "* " to display-line (start-point:width)
inspect display-line replacing
all "*" by pyramid-chars (level-number:1)
subtract 2 from start-point
add 4 to width
display display-line
end-perform
* Example 3
display space
compute number-of-levels = ( function sqrt
( 16 * function length (para-text) ) ) / 4
if (number-of-levels *
(2 + 2 * (number-of-levels - 1))) / 2
< function length (para-text)
add 1 to number-of-levels
end-if
compute start-point =
( 2 * (number-of-levels - 1) + 1 ) / 2 + 1
move 1 to width
move 1 to text-ptr
move space to display-line
perform varying level-number from 1 by 1
until level-number > number-of-levels
compute max-move = function min ( (function length
(para-text) - text-ptr + 1) width )
move para-text (text-ptr:max-move)
to display-line (start-point:width)
add max-move to text-ptr
subtract 1 from start-point
add 2 to width
display display-line
end-perform
stop run
.

输出:

L
III
AAAAA
MMMMMMM
L
I I I
A A A A A
M M M M M M M
T
his
is a
long p
aragraph
to demonstr
ate building
a pyramid from
text. The only ex
plicit loop is the
PERFORM VARYING.

在cobol 中有几种循环方式

  1. go to如何在COBOL中使用GO to(我不建议使用(
  2. perform语句https://www.tutorialspoint.com/cobol/cobol_loop_statements.htm

最新更新