|
View previous topic :: View next topic
|
| Author |
Message |
manyone
New User
Joined: 09 Mar 2006 Posts: 19
|
|
|
|
Hello fellow COBOL enthusiasts!
I've resurrected a late-'90s project: cobcalc, a pure COBOL algebraic expression evaluator inspired by 80s BYTE Magazine infix parser articles (now in our language).
It parses/evaluates expressions with precedence, parentheses, and floating-point support. Key features:
Basic ops: +, -, *, /
Exponentiation: ^
Parentheses nesting
Built-in SQRT (expandable)
Floating-point output
Example 1: Compile/run (cobc -x cobcalc.cob), enter expression:
| Code: |
ENTER EXPRESSION (OR END)
4+3^2
ANS= 13.00000 |
(Precedence: 3^2=9 +4=13.)
Example 2: (Amortization) solve for monthly payment: Int=5% Loan=$250000 N=30 years
| Code: |
ENTER EXPRESSION (OR END)
(5/1200*250000*((1+5/1200)^(30*12)))/(((1+5/1200)^(30*12))-1)
ANS= 1342.05405 |
Core in one file: cobcalc.cob (COBOL-85 recursion/structured). Tested with GnuCOBOL 3.1+—curious about mainframe runs?
Parser fans, legacy lovers: Check it out! Shared first on reddit.com/r/cobol for great feedback.
Bonus Challenge: Using Enterprise COBOL 6.4 UDFs, refactor into a callable function like
| Code: |
| COMPUTE WS-RESULT = COBCALC(4 + 3 ^ 2) |
Feedback/tweaks/"why COBOL?" tales welcome. Quirkiest expression parsed?
Happy coding!
manyone |
|
| Back to top |
|
 |
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1054 Location: Richmond, Virginia
|
|
|
|
| There is a Facebook group called Mainframers. If you join, this is the kind of post we appreciate, especially if you provide some implementation insight. |
|
| Back to top |
|
 |
Pedro
Global Moderator

Joined: 01 Sep 2006 Posts: 2622 Location: Silicon Valley
|
|
|
|
re: I've resurrected
Can you provide the details? Is it in Github? |
|
| Back to top |
|
 |
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1054 Location: Richmond, Virginia
|
|
|
|
I have no idea what Github is - I'm an old Mainframers lol.
Facebook has groups. This one is called Mainframers. |
|
| Back to top |
|
 |
manyone
New User
Joined: 09 Mar 2006 Posts: 19
|
|
|
|
| Pedro wrote: |
re: I've resurrected
Can you provide the details? Is it in Github? |
(click on the word cobcalc in the text to get the url)
github.com/manyone/cobcalc
click green buttom marked code dropdown, download the zip, unzip to get the source |
|
| Back to top |
|
 |
manyone
New User
Joined: 09 Mar 2006 Posts: 19
|
|
|
|
| manyone wrote: |
| Pedro wrote: |
re: I've resurrected
Can you provide the details? Is it in Github? |
(click on the word cobcalc in the text to get the url)
github.com/manyone/cobcalc
click green buttom marked code dropdown, download the zip, unzip to get the source |
OR you can just click on cobcalc cob in the repo to browse it, ctl-a to select all and open a notepad and ctl-v to paste it and save as cobcalc.cob |
|
| Back to top |
|
 |
Pedro
Global Moderator

Joined: 01 Sep 2006 Posts: 2622 Location: Silicon Valley
|
|
| Back to top |
|
 |
sergeyken
Senior Member

Joined: 29 Apr 2008 Posts: 2263 Location: USA
|
|
|
|
| Pedro wrote: |
re: I have no idea what Github is
It is a development platform. See this video:
github.com/why-github |
Watched the video.
Blah-blah-blah about nothing. Typical for 99.99% of nowadays "software experts". |
|
| Back to top |
|
 |
Pedro
Global Moderator

Joined: 01 Sep 2006 Posts: 2622 Location: Silicon Valley
|
|
|
|
re: Blah-blah-blah about nothing
My last job was as a developer for DB2 Admin Tool (IBM software). We switched to using Github the year before I retired (2016). We were doing mainframe development.
I found it pretty useful. You can check out code, edit, test and check it in. And you can do it in parallel with other developers. It has tools to easily merge code from other developers.
I recall it needed a custom editor plugin because IBM uses an IBM-only language. Other than that, it was standard. (but I was not part of the Github implementation team).
And being proficient with it lets you easily move to other development teams.
Even if you do not use it for yourself, you might still want to use it to inspect stuff like this cobcalc. There are numerous other example programs. |
|
| Back to top |
|
 |
PatrickDohman Currently Banned New User
Joined: 08 Oct 2025 Posts: 3 Location: United States
|
|
|
|
<Snip>
Feedback/tweaks/"why COBOL?" tales welcome. Quirkiest expression parsed?
<Snip>
Are you at all familiar with the Sum Of Integers cob?
| Code: |
[opc@instance-xxxxxxxx-1218 ~]$ /usr/bin/cat SumOfIntegers.cob
IDENTIFICATION DIVISION.
PROGRAM-ID. SumOfIntegers.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 n BINARY-LONG.
01 i BINARY-LONG.
01 total BINARY-LONG VALUE 0.
PROCEDURE DIVISION.
DISPLAY "Enter a positive integer"
ACCEPT n
PERFORM VARYING i FROM 1 BY 1 UNTIL i > n
ADD i TO total
END-PERFORM
DISPLAY "The sum is " total.
|
| Code: |
[opc@instance-xxxxxxxx-1218 ~]$ cobcrun SumOfIntegers
Enter a positive integer
8
The sum is +0000000036
|
|
|
| Back to top |
|
 |
manyone
New User
Joined: 09 Mar 2006 Posts: 19
|
|
|
|
| PatrickDohman wrote: |
<Snip>
Feedback/tweaks/"why COBOL?" tales welcome. Quirkiest expression parsed?
<Snip>
Are you at all familiar with the Sum Of Integers cob?
| Code: |
[opc@instance-xxxxxxxx-1218 ~]$ /usr/bin/cat SumOfIntegers.cob
IDENTIFICATION DIVISION.
PROGRAM-ID. SumOfIntegers.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 n BINARY-LONG.
01 i BINARY-LONG.
01 total BINARY-LONG VALUE 0.
PROCEDURE DIVISION.
DISPLAY "Enter a positive integer"
ACCEPT n
PERFORM VARYING i FROM 1 BY 1 UNTIL i > n
ADD i TO total
END-PERFORM
DISPLAY "The sum is " total.
|
| Code: |
[opc@instance-xxxxxxxx-1218 ~]$ cobcrun SumOfIntegers
Enter a positive integer
8
The sum is +0000000036
|
|
yes. i am.
you are using the loop method of computing it. it works but when the last number is high, say 10000 - you have to do 'add 1 to total' 10000 times to get the answer.
there's a short cut way of solving that. i'll give you a clue - say you have the numbers 1,2,3,4,5 which adds up to 15. see below
| Code: |
get the list 1 2 3 4 5
reverse them 5 4 3 2 1
add columns 6 6 6 6 6 sum is 30
note: 3 3 3 3 3 sum is 15 (the correct answer)
hint: add the first number (1) and the last number (5) for starters.
|
|
|
| Back to top |
|
 |
|
|