如何在COBOL中打印星形图案?

时间:2018-10-01 18:02:40

标签: cobol mainframe

我试图在cobol中打印星形图案。我构造了以下代码。但我认为可以优化。而且我还需要在运行时输入多个输入。在运行时无法接受输入。谁能指导我?

 IDENTIFICATION DIVISION.
 PROGRAM-ID. HELLO.
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01 WS-TABLE.
  05 WS-A OCCURS 10 TIMES INDEXED BY I.
     10 WS-B PIC X(1).
     10 WS-C PIC X(1) OCCURS 10 TIMES INDEXED BY J.
01 N PIC 9(1).
PROCEDURE DIVISION.
ACCEPT N.
PERFORM A-PARA VARYING I FROM 1 BY 1 UNTIL I>N.
PERFORM C-PARA VARYING I FROM 1 BY 1 UNTIL I>N.
STOP RUN.
A-PARA.
PERFORM B-PARA VARYING J FROM 1 BY 1 UNTIL J>I.
B-PARA.
MOVE '*' TO WS-C(I,J).
C-PARA.
DISPLAY WS-A(I).

2 个答案:

答案 0 :(得分:1)

Sayantan Sur,欢迎您使用Stack Overflow。很高兴知道您已经解决了自己的问题并将其发布为答案。您可以edit提出任何更新/改进/优化解决方案的第一个答案时,不必发布2个答案。 edit选项正好在您的答案下方,介于shareflag之间。

关于您的问题, 您只能有一个PERFORM循环来打印星号图案。请看下面的代码。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.

DATA DIVISION. 
WORKING-STORAGE SECTION.
01 WS-OUT PIC X(80) VALUE SPACES.
01 WS-I PIC 9(2) VALUE 0. 
01 WS-N PIC 9(2) VALUE 5. 
01 WS-CENTER PIC 9(2) VALUE 1.  

PROCEDURE DIVISION.

PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > WS-N
MOVE ALL '*' TO WS-OUT(WS-CENTER:WS-I)
DISPLAY WS-OUT
END-PERFORM. 

STOP RUN.

注意::在以上代码段中,我没有包含在运行时进行输入的步骤。相反,我使用了VALUE子句将输入分配给WS-N

或者,您还可以使用以下代码。感谢 Rick Smith 的宝贵建议。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.

DATA DIVISION. 
WORKING-STORAGE SECTION.
01 WS-OUT PIC X(10) VALUE ALL '*'.
01 WS-I PIC 9(2) VALUE 0. 
01 WS-N PIC 9(2) VALUE 5. 
01 WS-CENTER PIC 9(2) VALUE 1.  

PROCEDURE DIVISION.

PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > WS-N
DISPLAY WS-OUT(WS-CENTER:WS-I)
END-PERFORM. 

STOP RUN.

输出:两种代码均提供以下输出。

*                                                                               
**                                                                              
***                                                                             
****                                                                            
*****

您可以尝试运行代码here

答案 1 :(得分:0)

npm i -g create-react-native-app
create-react-native-app my-project
cd my-project
npm start

这里循环得到了进一步优化,甚至段落数也减少了。