Cevela about COBOL COBOL language - Home - Site Map - About Cevela - MX COBOL    <prev   next>
I/O dialog, char-strings handling and arithmetic - (demo b)
For more see - Formats in detail + Summary comment + How to use MX COBOL demos.

Simple Calculator
000000*..1....*....2....*....3....*....4....*....5....*....6....*....7..
      * Simple Calculator                                       mx_b1.cb
      *
       working-storage section.
       1   Inp pic x(10) value ".".
        88  Work-end value " ".
       1   Ope pic x.
        88  Valid-operator value "+" "-" "*" "/".
       1   Int pic 9999.
       1   Sum pic 999 value 0.
       1   Sum-ed pic zz9.
       1   Rem pic 999.
       1   Rem-ed pic zz9.
       1   Err pic 9.
      *
       procedure division.
           perform Directions-for-use.
      *
           perform until Work-end
              accept Inp
      *
              if not Work-end
                 unstring Inp
                    delimited "+" or "-" or "*" or "/" or space
                    into
                       Int delimiter Ope
                       Int
                 perform Calcul
              else
                 display "end of work"
                 accept Inp
              end-if
      *
           end-perform.
           stop run.
      *
       Calcul.
           move 0 to Err Rem.
      *
           if Ope = "+"
              add Int to Sum
                 size error move 1 to Err.
      *
           if Ope = "-"
              subtract Int from Sum
                 size error move 1 to Err.
      *
           if Ope = "*"
              multiply Sum by Int giving Sum
                 size error move 1 to Err.
      *
           if Ope = "/"
              divide Sum by Int
                 remainder Rem
                 size error move 1 to Err.
      *
           if not (Valid-operator)
              move 2 to Err.
      *
           if Err = 0
              move Sum to Sum-ed
              display "= " Sum-ed
              if Rem > 0
                 move Rem to Rem-ed
                 display "remainder = " Rem-ed
              end-if else
            if Err = 1
              display "Sum size error (max 999) or /0 !!!"
              display " "
            else
              display "invalid operator !!!"
              perform Directions-for-use.
      *
       Directions-for-use.
           display "Calculator - directions for use:".
           display
              " - input have to be operator and integer (max 3 digits) "
              "without space".
           display " - input is Enter only: program ends".
           display " - valid operators:
           display " ".
    
MX COBOL + C compile listings - see here

MX COBOL - free compiler - © Vlastimil Cevela 2006

Date 2006-06-04 - Text Builder 0.65 - Time 06:30:56