fix sorting code and add details about integer sort

This commit is contained in:
vel 2024-10-21 20:27:10 -07:00
parent f6dc7afd27
commit 14746ee3e5
Signed by: velvox
GPG Key ID: 59D9762F674151DF
7 changed files with 180 additions and 114 deletions

View File

@ -1,23 +1,31 @@
# Language Proposal # Language Proposal
### 1. Hello World ### 1. Hello World
- source used: [Cobol Emulator](https://www.jdoodle.com/execute-cobol-online) - source used: [Cobol Emulator](https://www.jdoodle.com/execute-cobol-online)
- display the text "Hello World!" in the terminal - display the text "Hello World!" in the terminal
- https://www.tutorialspoint.com/cobol/cobol_program_structure.htm this website has useful information about the structure of the cobol language - https://www.tutorialspoint.com/cobol/cobol_program_structure.htm this website has useful information about the structure of the cobol language
- the Identification Division is mandatory for all programs and sub divisions have unique sentences within them and determine what is done with variables or IO in this example - the Identification Division is mandatory for all programs and sub divisions have unique sentences within them and determine what is done with variables or IO in this example
#### Input #### Input
there is no input for this example there is no input for this example
#### Output #### Output
the string "Hello World!" is sent to output via the included DISPLAY function the string "Hello World!" is sent to output via the included DISPLAY function
### 2. Quadratic Function ### 2. Quadratic Function
- Code altered from [Marco Biagini](https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation) - Code altered from [Marco Biagini](https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation)
- Solves a quadratic equation of the form **ax^2 + bx + c = 0** - Solves a quadratic equation of the form **ax^2 + bx + c = 0**
#### Input #### Input
Coefficients are a fixed value with a = 1, b = 5, c = 6 Coefficients are a fixed value with a = 1, b = 5, c = 6
#### Output #### Output
EQUATION: (1x^2) + 5x + 6 = 0 EQUATION: (1x^2) + 5x + 6 = 0
The equation has two distinct real roots: The equation has two distinct real roots:
@ -26,18 +34,30 @@
### 3. Integer Sort ### 3. Integer Sort
- Code altered from [Wikibooks]https://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Bubble_sort#COBOL
- Uses bubble sort to sort an array of integers.
#### Input
An array of integers
#### Output
The array sorted in ascending order
### 4. Language Features ### 4. Language Features
- #### Identifiers - #### Identifiers
All variables are declared within DATA DIVISION. Variable names accept letters(A-Z), digits(0-9), and hyphens(-). In the syntax of: All variables are declared within DATA DIVISION. Variable names accept letters(A-Z), digits(0-9), and hyphens(-). In the syntax of:
<level-number> <variable-name> <level-number> <variable-name>
[PIC/PICTURE <data-type(variable-length>)] [PIC/PICTURE <data-type(variable-length>)]
[VALUE <literal-value>] [VALUE <literal-value>]
Statements in [ ] are optional. Example: Statements in [ ] are optional. Example:
01 INT-VAR PIC 9(03 VALUE 123). 01 INT-VAR PIC 9(03 VALUE 123).
- #### Special words - #### Special words
- Note: Keywords are not case sensitive but good practice is to only use uppercase - Note: Keywords are not case sensitive but good practice is to only use uppercase
- Division statements divide the structure of the code. Some examples are PROGRAM-ID, DATA DIVISION, WORKING STORAGE DIVISION, PROCEDURE DIVISION, and STOP RUN. - Division statements divide the structure of the code. Some examples are PROGRAM-ID, DATA DIVISION, WORKING STORAGE DIVISION, PROCEDURE DIVISION, and STOP RUN.
- DISPLAY outputs data to user - DISPLAY outputs data to user
@ -45,10 +65,11 @@
- ACCEPT allows for user input to go into a variable - ACCEPT allows for user input to go into a variable
- Supports boolean values TRUE and FALSE - Supports boolean values TRUE and FALSE
- #### Literal - #### Literal
*> This is a comment in COBOL *> This is a comment in COBOL
Non-numeric Non-numeric
DISPLAY "This is a string in COBOL". DISPLAY "This is a string in COBOL".
@ -62,16 +83,19 @@
Decimal: 10.00 Decimal: 10.00
- #### Math Operators - #### Math Operators
Supports additon (+), subraction (-), multiplication (*), and division (/) and exponentation (**). Equations begin with COMPUTE.
Supports additon (+), subraction (-), multiplication (\*), and division (/) and exponentation (\*\*). Equations begin with COMPUTE.
COMPUTE total = 1 + 2 COMPUTE total = 1 + 2
- #### Relational Operators - #### Relational Operators
Supports =, >, <, >=, <=, NOT = Supports =, >, <, >=, <=, NOT =
IF a > b IF a > b
- #### Delimiters - #### Delimiters
Section headers and verb statements should end with a separator period (.) Section headers and verb statements should end with a separator period (.)
PROCEDURE DIVISION. PROCEDURE DIVISION.
@ -82,6 +106,7 @@
ADD 3 TO VAR1, VAR2. ADD 3 TO VAR1, VAR2.
- #### Lists - #### Lists
Supports arrays (known in COBOL as tables). Declared in DATA DIVISION. In the syntax of: Supports arrays (known in COBOL as tables). Declared in DATA DIVISION. In the syntax of:
01 <table-name>. 01 <table-name>.
@ -90,12 +115,14 @@
[DEPENDING ON] [DEPENDING ON]
[DESCENDING|ASCENDING KEY IS <key_var>] [DESCENDING|ASCENDING KEY IS <key_var>]
[INDEXED BY <index_name>] [INDEXED BY <index_name>]
Statements in [ ] are optional. Example: Statements in [ ] are optional. Example:
01 Students 01 Students
03 Student-grades PIC 9(03) OCCURS 6 TIMES. 03 Student-grades PIC 9(03) OCCURS 6 TIMES.
- #### Loops - #### Loops
Loops begin with a PERFORM statement and end with END-PERFORM. Note that a separator period (.) should not used within a PERFORM block. Loops begin with a PERFORM statement and end with END-PERFORM. Note that a separator period (.) should not used within a PERFORM block.
PERFORM <conditional statement> PERFORM <conditional statement>
@ -104,14 +131,13 @@
- #### Branching - #### Branching
Uses IF/END-IF and ELSE statements and nested statements. Note that a separator period (.) should not used within an IF statement. Uses IF/END-IF and ELSE statements and nested statements. Note that a separator period (.) should not used within an IF statement.
IF <condition> IF <condition>
DISPLAY "Option 1" DISPLAY "Option 1"
ELSE ELSE
DISPLAY "Option 2" DISPLAY "Option 2"
END-IF. END-IF.
` Language Feature Sources ` `Language Feature Sources`
- [tutorialspoint.com](https://www.tutorialspoint.com/cobol/cobol_basic_syntax.htm) - [tutorialspoint.com](https://www.tutorialspoint.com/cobol/cobol_basic_syntax.htm)

View File

@ -1,75 +1,55 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. sorting.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SORT-AREA.
05 WS-SORT-TABLE.
10 WS-SORT-ROW PIC X(10) OCCURS 100.
05 WS-TEMP-ROW PIC X(10).
05 WS-ROW-MAX PIC S9(4) COMP VALUE 100.
05 WS-SORT-MAX PIC S9(4) COMP.
05 WS-I PIC S9(4) COMP.
05 WS-J PIC S9(4) COMP.
05 WS-INDEX PIC S9(4) COMP.
*> code taken from https://craftofcoding.wordpress.com/2021/03/23/coding-cobol-a-bubblesort/ PROCEDURE DIVISION.
identification division. *> * Initialize test data
program-id. stats. MOVE "30" TO WS-SORT-ROW(1)
MOVE "10" TO WS-SORT-ROW(2)
MOVE "50" TO WS-SORT-ROW(3)
MOVE "20" TO WS-SORT-ROW(4)
MOVE "40" TO WS-SORT-ROW(5)
MOVE 5 TO WS-SORT-MAX
environment division. *> * Display original array
input-output section. DISPLAY "Original Array Contents:"
file-control. DISPLAY "---------------------"
select input-file assign to dynamic fname-inp PERFORM VARYING WS-INDEX FROM 1 BY 1
organization is line sequential. UNTIL WS-INDEX > WS-SORT-MAX
DISPLAY "Element " WS-INDEX ": " WS-SORT-ROW(WS-INDEX)
END-PERFORM
DISPLAY SPACE
data division. *> * Simplified bubble sort
file section. PERFORM VARYING WS-I FROM 1 BY 1
fd input-file. UNTIL WS-I > WS-SORT-MAX - 1
01 sample-input pic x(80). PERFORM VARYING WS-J FROM 1 BY 1
UNTIL WS-J > WS-SORT-MAX - WS-I
IF WS-SORT-ROW(WS-J) > WS-SORT-ROW(WS-J + 1)
MOVE WS-SORT-ROW(WS-J) TO WS-TEMP-ROW
MOVE WS-SORT-ROW(WS-J + 1) TO WS-SORT-ROW(WS-J)
MOVE WS-TEMP-ROW TO WS-SORT-ROW(WS-J + 1)
END-IF
END-PERFORM
END-PERFORM
working-storage section. *> * Display sorted array
77 n pic 9999 value 0. DISPLAY "Sorted Array Contents:"
77 feof pic A(1). DISPLAY "--------------------"
77 temp pic s9(14)v9(4) usage is computational-3. PERFORM VARYING WS-INDEX FROM 1 BY 1
77 fname-inp pic x(30). UNTIL WS-INDEX > WS-SORT-MAX
77 i pic 9999. DISPLAY "Element " WS-INDEX ": " WS-SORT-ROW(WS-INDEX)
77 j pic 9999. END-PERFORM.
77 jp1 pic 9999.
77 jp2 pic 9999.
01 array-area. STOP RUN.
02 x pic s9(14)v9(4) usage is computational-3
occurs 1000 times.
01 input-value.
02 in-x pic s9(14)v9(4).
02 filler pic x(62).
procedure division.
display "Input filename? "
accept fname-inp.
open input input-file.
perform input-loop until feof='Y'
perform bubblesort.
perform print-nums.
perform finish.
input-loop.
read input-file into input-value
at end move 'Y' to feof
not at end
add 1 to n
move in-x to x(n)
end-read.
bubblesort.
perform varying i from 1 by 1 until i is greater than n
compute jp1 = n - i
perform varying j from 1 by 1 until j is greater than jp1
compute jp2 = j + 1
if (x(j) > x(jp2))
move x(j) to temp
move x(jp2) to x(j)
move temp to x(jp2)
end-if
end-perform
end-perform.
print-nums.
move 1 to i.
perform until i > n
display i "->"x(i)
add 1 to i
end-perform.
finish.
close input-file.
stop run.

View File

@ -0,0 +1,6 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
DISPLAY "Hello World!"
STOP RUN.
if

View File

@ -1 +0,0 @@
answer = 2020+4

View File

@ -0,0 +1,55 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. sorting.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SORT-AREA.
05 WS-SORT-TABLE.
10 WS-SORT-ROW PIC X(10) OCCURS 100.
05 WS-TEMP-ROW PIC X(10).
05 WS-ROW-MAX PIC S9(4) COMP VALUE 100.
05 WS-SORT-MAX PIC S9(4) COMP.
05 WS-I PIC S9(4) COMP.
05 WS-J PIC S9(4) COMP.
05 WS-INDEX PIC S9(4) COMP.
PROCEDURE DIVISION.
*> * Initialize test data
MOVE "30" TO WS-SORT-ROW(1)
MOVE "10" TO WS-SORT-ROW(2)
MOVE "50" TO WS-SORT-ROW(3)
MOVE "20" TO WS-SORT-ROW(4)
MOVE "40" TO WS-SORT-ROW(5)
MOVE 5 TO WS-SORT-MAX
*> * Display original array
DISPLAY "Original Array Contents:"
DISPLAY "---------------------"
PERFORM VARYING WS-INDEX FROM 1 BY 1
UNTIL WS-INDEX > WS-SORT-MAX
DISPLAY "Element " WS-INDEX ": " WS-SORT-ROW(WS-INDEX)
END-PERFORM
DISPLAY SPACE
*> * Simplified bubble sort
PERFORM VARYING WS-I FROM 1 BY 1
UNTIL WS-I > WS-SORT-MAX - 1
PERFORM VARYING WS-J FROM 1 BY 1
UNTIL WS-J > WS-SORT-MAX - WS-I
IF WS-SORT-ROW(WS-J) > WS-SORT-ROW(WS-J + 1)
MOVE WS-SORT-ROW(WS-J) TO WS-TEMP-ROW
MOVE WS-SORT-ROW(WS-J + 1) TO WS-SORT-ROW(WS-J)
MOVE WS-TEMP-ROW TO WS-SORT-ROW(WS-J + 1)
END-IF
END-PERFORM
END-PERFORM
*> * Display sorted array
DISPLAY "Sorted Array Contents:"
DISPLAY "--------------------"
PERFORM VARYING WS-INDEX FROM 1 BY 1
UNTIL WS-INDEX > WS-SORT-MAX
DISPLAY "Element " WS-INDEX ": " WS-SORT-ROW(WS-INDEX)
END-PERFORM.
STOP RUN.

View File

@ -5,11 +5,8 @@ DIGIT [0-9]
LETTER [a-zA-Z] LETTER [a-zA-Z]
%% %%
(" "|\t|\n) /* skip whitespace */ (" "|\t|\n) /* skip whitespace */
\+ { return TOKEN_ADD; } IDENTIFICATION { return TOKEN_IDENTIFICATION; }
"=" { return TOKEN_ASSIGNMENT; } DIVISION { return TOKEN_KEYWORD_DIVISION; }
while { return TOKEN_WHILE; }
{LETTER}+ { return TOKEN_IDENT; }
{DIGIT}+ { return TOKEN_NUMBER; }
. { return TOKEN_ERROR; }
%% %%
int yywrap() { return 1; } int yywrap() { return 1; }

View File

@ -1,9 +1,12 @@
typedef enum { typedef enum {
TOKEN_EOF = 0, TOKEN_EOF = 0,
TOKEN_WHILE, TOKEN_IDENTIFICATION,
TOKEN_ADD, TOKEN_KEYWORD_DIVISION,
TOKEN_ASSIGNMENT, TOKEN_PROGRAM_ID,
TOKEN_IDENT, TOKEN_PROCEDURE,
TOKEN_NUMBER, TOKEN_DISPLAY,
TOKEN_ERROR TOKEN_STOP,
TOKEN_RUN,
TOKEN_STRING,
TOKEN_DOT
} token_t; } token_t;