topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Wednesday December 11, 2024, 11:40 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Celsius <-> Fahrenheit in COBOL  (Read 5757 times)

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Celsius <-> Fahrenheit in COBOL
« on: June 12, 2023, 08:33 AM »
I migrated to macOS, so my C/F converter in PowerShell makes less sense to me. That's why I rewrote it ...



... in COBOL.  8)

       IDENTIFICATION DIVISION.
       PROGRAM-ID. cf.
       DATA DIVISION.
       
       WORKING-STORAGE SECTION.
       
       *> We store four variables here:
       *> - input unit (c/C or f/F)
       *> - input value (decimal number)
       *> - formatted input (without leading zeroes)
       *> - output value (without leading zeroes)
       01 input-unit PIC X(1) VALUE SPACES.
       01 input-value PIC S9(10)V999 COMP-3.
       01 input-formatted PIC +ZZ9.99.
       01 output-value PIC +ZZ9.99.

       PROCEDURE DIVISION.
       PERFORM 0000-START.

       0000-USAGE.
           DISPLAY "Usage: cf <c or f> <value>."
           STOP RUN.

       0000-START.
           ACCEPT input-unit FROM ARGUMENT-VALUE
           IF input-unit = SPACE OR LOW-VALUES THEN
               PERFORM 0000-USAGE
           ELSE
               *> Cleanup: trim() input-unit
               INSPECT input-unit REPLACING ALL SPACES BY LOW-VALUES
               MOVE FUNCTION UPPER-CASE(input-unit) to input-unit
           END-IF

           ACCEPT input-value FROM ARGUMENT-VALUE
           MOVE input-value TO input-formatted

           IF input-unit = "C" THEN
               COMPUTE output-value = input-value * (9 / 5) + 32
               DISPLAY input-formatted " °C = " output-value " °F"
           ELSE
               COMPUTE output-value = (input-value - 32) * (5 / 9)
               DISPLAY input-formatted " °F = " output-value " °C"
           END-IF

       STOP RUN.
.

% ./cf c 0
+  0.00 °C = + 32.00 °F
« Last Edit: June 12, 2023, 11:39 AM by Tuxman »

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,776
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Celsius <-> Fahrenheit in COBOL
« Reply #1 on: June 12, 2023, 10:34 AM »
NOTE: The first line needs to be indented - this forum breaks COBOL code.

You have it formatted as plaintext. Remove that from the code tag and it will preserve the indentation.

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: Celsius <-> Fahrenheit in COBOL
« Reply #2 on: June 12, 2023, 11:31 AM »
Oh, indeed! It was the default choice and I was too lazy to write [ code] myself this time.
Fixed!

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,776
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Celsius <-> Fahrenheit in COBOL
« Reply #3 on: June 13, 2023, 09:29 PM »
Oh, I see! You picked code highlighting from the dropdown. I just click the # icon which adds code tags without any specific highlighting. :)

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: Celsius <-> Fahrenheit in COBOL
« Reply #4 on: June 14, 2023, 04:11 AM »
Ha! Learned a new thing today…  :D