如何在COBOL中根据库存总量添加对应数量的星号字符串?
Got it, let's tackle this COBOL problem. The goal is to generate the right number of asterisks based on inventory levels and append them to your output line. Here are a couple of clean, practical ways to implement this:
First, Set Up Your Working Storage
Start by defining the necessary variables in your WORKING-STORAGE SECTION—these will hold your inventory count, the asterisk string, and your final output line:
WORKING-STORAGE SECTION. 01 WS-INVENTORY-TOTAL PIC 9(3) VALUE 0. *> Holds current inventory level 01 WS-ASTERISKS PIC X(5) VALUE SPACES. *> Max 5 asterisks needed 01 WS-OUTPUT-LINE PIC X(80) VALUE SPACES. *> Your output line (adjust length as needed)
Method 1: Use STRING with REPEAT (COBOL 85+)
If your compiler supports COBOL 85 or later, the STRING statement's REPEAT option is the most concise way to generate repeated characters. Here's how it works:
PROCEDURE DIVISION. *> Example inventory value (replace with your actual inventory field) MOVE 42 TO WS-INVENTORY-TOTAL. *> Reset asterisk field to spaces first MOVE SPACES TO WS-ASTERISKS. *> Determine how many asterisks to generate IF WS-INVENTORY-TOTAL < 10 STRING '*' REPEAT 5 TIMES DELIMITED BY SIZE INTO WS-ASTERISKS ELSE IF WS-INVENTORY-TOTAL < 50 STRING '*' REPEAT 2 TIMES DELIMITED BY SIZE INTO WS-ASTERISKS END-IF. *> Append asterisks to your output line *> Use DELIMITED BY SPACE to capture only the populated part of your line STRING WS-OUTPUT-LINE DELIMITED BY SPACE, WS-ASTERISKS DELIMITED BY SIZE INTO WS-OUTPUT-LINE END-STRING. *> Print the final line DISPLAY WS-OUTPUT-LINE.
Why this works:
The REPEAT keyword tells COBOL to repeat the '*' character the specified number of times, and DELIMITED BY SIZE ensures we fill the entire WS-ASTERISKS field (or as much as needed).
Method 2: Compatibility with Older COBOL Versions
If you're working with a pre-COBOL 85 compiler that doesn't support REPEAT, use a pre-defined asterisk constant and substring it as needed:
WORKING-STORAGE SECTION. *> Add a constant with the maximum number of asterisks we'll need 01 WS-FULL-ASTERISKS PIC X(5) VALUE '*****'. *> Keep the other variables from earlier 01 WS-INVENTORY-TOTAL PIC 9(3) VALUE 0. 01 WS-ASTERISKS PIC X(5) VALUE SPACES. 01 WS-OUTPUT-LINE PIC X(80) VALUE SPACES. PROCEDURE DIVISION. *> Example emergency inventory level MOVE 7 TO WS-INVENTORY-TOTAL. MOVE SPACES TO WS-ASTERISKS. IF WS-INVENTORY-TOTAL < 10 *> Use all 5 asterisks MOVE WS-FULL-ASTERISKS TO WS-ASTERISKS ELSE IF WS-INVENTORY-TOTAL < 50 *> Take the first 2 asterisks from the constant MOVE WS-FULL-ASTERISKS(1:2) TO WS-ASTERISKS END-IF. *> Append to output line (same as method 1) STRING WS-OUTPUT-LINE DELIMITED BY SPACE, WS-ASTERISKS DELIMITED BY SIZE INTO WS-OUTPUT-LINE END-STRING. DISPLAY WS-OUTPUT-LINE.
Alternative: Perform Loop (For Ultimate Compatibility)
If you need to support extremely old compilers, you can use a PERFORM loop to build the asterisk string one character at a time:
WORKING-STORAGE SECTION. 01 WS-INVENTORY-TOTAL PIC 9(3) VALUE 0. 01 WS-ASTERISKS PIC X(5) VALUE SPACES. 01 WS-OUTPUT-LINE PIC X(80) VALUE SPACES. 01 WS-COUNT PIC 9(1) VALUE 0. *> Counter for the loop PROCEDURE DIVISION. MOVE 35 TO WS-INVENTORY-TOTAL. MOVE SPACES TO WS-ASTERISKS. IF WS-INVENTORY-TOTAL < 10 PERFORM VARYING WS-COUNT FROM 1 BY 1 UNTIL WS-COUNT > 5 MOVE '*' TO WS-ASTERISKS(WS-COUNT:1) END-PERFORM ELSE IF WS-INVENTORY-TOTAL < 50 PERFORM VARYING WS-COUNT FROM 1 BY 1 UNTIL WS-COUNT > 2 MOVE '*' TO WS-ASTERISKS(WS-COUNT:1) END-PERFORM END-IF. *> Append to output line STRING WS-OUTPUT-LINE DELIMITED BY SPACE, WS-ASTERISKS DELIMITED BY SIZE INTO WS-OUTPUT-LINE END-STRING. DISPLAY WS-OUTPUT-LINE.
This is more verbose, but it works on virtually any COBOL compiler.
Pro Tip for Fixed-Length Output Lines
If your output line is a fixed length (like 80 characters), you can skip the STRING step and directly place the asterisks at the end of the line:
*> For an 80-character line, positions 76-80 are the last 5 characters MOVE WS-ASTERISKS TO WS-OUTPUT-LINE(76:5)
This is faster and avoids any issues with DELIMITED BY SPACE if your line has embedded spaces.
内容的提问来源于stack exchange,提问作者becca

