How to add a string of asterisks in Cobol? [closed]

青春壹個敷衍的年華 提交于 2019-12-13 03:43:05

问题


Problem:

If the inventory total is less than 50, add a string of two asterisks (**) at the end of the written row to notify the inventory manager that more inventory is needed. If the inventory total is less than 10, add a string of five asterisks (*****) at the end of the row to let the inventory manager know the need for more inventory is urgent.

How would I make a string of asterisks in Cobol?


回答1:


How would I make a string of asterisks in Cobol?

There are two methods.

The first controls the number of characters at the destination and works best when the data item is initialized before the move. The second controls the number of characters at the source and works best when initialization of the destination is of no concern or when used as part of a STRING statement.

Method 1:

move all "*" to data-name-1 (1:number-of-asterisks)

For example:

   program-id. aster.
   data division.
   working-storage section.
   1 n pic 99.
   1 asterisk-line pic x(10) value space.
   procedure division.
   begin.
       perform varying n from 10 by -1 until n < 1
           move all "*" to asterisk-line (1:n)
           display asterisk-line
           move space to asterisk-line
       end-perform
       stop run
       .

Output:

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

Notice that the program moves spaces to clear the destination after displaying the asterisks. This is prevent too many asterisks from showing on the following lines.

Method 2:

move asterisks (1:number-of-asterisks) to data-name-1

For example:

   program-id. aster2.
   data division.
   working-storage section.
   1 n pic 99.
   1 asterisks pic x(10) value all "*".
   1 asterisk-line pic x(10) value space.
   procedure division.
   begin.
       perform varying n from 10 by -1 until n < 1
           move asterisks (1:n) to asterisk-line
           display asterisk-line
       end-perform
       stop run
       .

The output is the same as above.

Notice there is no need to move spaces (or initialize) the destination before moving the asterisks.



来源:https://stackoverflow.com/questions/50642878/how-to-add-a-string-of-asterisks-in-cobol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!