DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Celsius <-> Fahrenheit in COBOL  (Read 14,158 times)

Tuxman

  • OMG not him again!
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,522
    • Participated in NANY Challenge Seven Times
Celsius <-> Fahrenheit in COBOL
« on: June 12, 2023, 01:33 PM »
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)

Code
       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.
.

Quote
% ./cf c 0
+  0.00 °C = + 32.00 °F
I bet when Cheetahs race and one of them cheats, the other one goes "Man, you're such a Cheetah!" and they laugh & eat a zebra or whatever.
« Last Edit: June 12, 2023, 04:39 PM by Tuxman »

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 9,827
    • Participated in NANY Challenge Eight Times
    • Cody Award
Re: Celsius <-> Fahrenheit in COBOL
« Reply #1 on: June 12, 2023, 03:34 PM »
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

  • OMG not him again!
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,522
    • Participated in NANY Challenge Seven Times
Re: Celsius <-> Fahrenheit in COBOL
« Reply #2 on: June 12, 2023, 04:31 PM »
Oh, indeed! It was the default choice and I was too lazy to write [ code] myself this time.
Fixed!
I bet when Cheetahs race and one of them cheats, the other one goes "Man, you're such a Cheetah!" and they laugh & eat a zebra or whatever.

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 9,827
    • Participated in NANY Challenge Eight Times
    • Cody Award
Re: Celsius <-> Fahrenheit in COBOL
« Reply #3 on: June 14, 2023, 02:29 AM »
Oh, I see! You picked code highlighting from the dropdown. I just click the # icon which adds code tags without any specific highlighting. :)

Tuxman

  • OMG not him again!
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,522
    • Participated in NANY Challenge Seven Times
Re: Celsius <-> Fahrenheit in COBOL
« Reply #4 on: June 14, 2023, 09:11 AM »
Ha! Learned a new thing today…  :D
I bet when Cheetahs race and one of them cheats, the other one goes "Man, you're such a Cheetah!" and they laugh & eat a zebra or whatever.

Original address: https://www.donationcoder.com/forum/index.php?topic=53489.0