ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Coding Snacks

Celsius <-> Fahrenheit in COBOL

(1/1)

Tuxman:
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
--- End quote ---

Deozaan:
NOTE: The first line needs to be indented - this forum breaks COBOL code.
-Tuxman (June 12, 2023, 08:33 AM)
--- End quote ---

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

Tuxman:
Oh, indeed! It was the default choice and I was too lazy to write [ code] myself this time.
Fixed!

Deozaan:
Oh, I see! You picked code highlighting from the dropdown. I just click the # icon which adds code tags without any specific highlighting. :)

Tuxman:
Ha! Learned a new thing today…  :D

Navigation

[0] Message Index

Go to full version