IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Cobcalc: An Algebraic Expression Evaluator in COBOL


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
manyone

New User


Joined: 09 Mar 2006
Posts: 19

PostPosted: Sun Oct 12, 2025 3:46 am
Reply with quote

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
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1054
Location: Richmond, Virginia

PostPosted: Sun Oct 12, 2025 6:30 am
Reply with quote

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
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2622
Location: Silicon Valley

PostPosted: Mon Oct 13, 2025 8:09 am
Reply with quote

re: I've resurrected

Can you provide the details? Is it in Github?
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1054
Location: Richmond, Virginia

PostPosted: Mon Oct 13, 2025 9:06 am
Reply with quote

I have no idea what Github is - I'm an old Mainframers lol.

Facebook has groups. This one is called Mainframers.
Back to top
View user's profile Send private message
manyone

New User


Joined: 09 Mar 2006
Posts: 19

PostPosted: Mon Oct 13, 2025 4:11 pm
Reply with quote

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
View user's profile Send private message
manyone

New User


Joined: 09 Mar 2006
Posts: 19

PostPosted: Tue Oct 14, 2025 12:02 am
Reply with quote

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
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2622
Location: Silicon Valley

PostPosted: Tue Oct 14, 2025 3:33 am
Reply with quote

re: I have no idea what Github is

It is a development platform. See this video:
github.com/why-github
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2263
Location: USA

PostPosted: Wed Oct 15, 2025 7:12 pm
Reply with quote

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
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2622
Location: Silicon Valley

PostPosted: Wed Oct 15, 2025 11:23 pm
Reply with quote

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
View user's profile Send private message
PatrickDohman
Currently Banned

New User


Joined: 08 Oct 2025
Posts: 3
Location: United States

PostPosted: Thu Oct 16, 2025 4:11 am
Reply with quote

<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
View user's profile Send private message
manyone

New User


Joined: 09 Mar 2006
Posts: 19

PostPosted: Thu Oct 16, 2025 4:56 am
Reply with quote

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
View user's profile Send private message
View previous topic : : View next topic  
Post new topic   Reply to topic All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts I wrote a Hallmark Movie Plot Generat... COBOL Programming 0
No new posts COBOL Text Adventure Engine for TK4- ... COBOL Programming 0
No new posts COBOL Infinite loops COBOL Programming 7
No new posts Problem moving a cobol program into E... COBOL Programming 3
No new posts COBOL sorting, with input GDG base COBOL Programming 7
Search our Forums:


Back to Top