数字出力putdec()2014年10月12日 19:32

先に出てきたputdec(n,w)をみてみましょう。 数値nをw桁で印字します。必要があれば、桁は拡張されます。

RATFOR版は下記の通り。

# putdec.r4 -- put decimal integer n in field width >= W
      subroutine putdec(n, w)
      character chars(MAXCHARS)
      integer itoc
      integer i, n, nd, w

      nd = itoc(n, chars, MAXCHARS)
      for ( i = nd + 1; i <= w; i = i + 1)
          call putc(BLANK)
      for ( i = 1; i <= nd; i = i + 1)
          call putc(chars(i))
      return
      end

ここで、itoc(n,chars,MAXCHARS)は、数値nを長さMAXCHARS文字の文字配列charsに変換します。

RATFOR版は下記の通り。

# itoc.r4 -- convert integer int to character string str
      integer fnction itoc(int, str, size)
      integer abs, mod
      integer d, i, int, intval, j, size
      character str(size), k
      string digits "0123456789"

      intval = abs(int)
      str(1) = EOS
      i = 1
      repeat {                       # generate digits
          i = i + 1
          d = mod(intval, 10)
          str(i) = digits(d + 1)
          intval = intval / 10
          } until (intval == 0 ! i >= size)
      if (int < 0 & i < size) {      # then sign
          i = i + 1
          str(i) = MINUS
          }
      itoc = i - 1
      for (i = 1; j < i; j = j + 1) { # then reverse
          k = str(i)
          str(i) = str(j)
          str(j) = k
          i = i - 1
          }
      return
      end

string deigits "0123456789"とは、文字を文字配列にセットするマクロで、

      character digits(11)
      data digit(1) /'0'/
      data digit(2) /'1'/
      data digit(3) /'2'/
      data digit(4) /'3'/
      data digit(5) /'4'/
      data digit(6) /'5'/
      data digit(7) /'6'/
      data digit(8) /'7'/
      data digit(9) /'8'/
      data digit(10) /'9'/
      data digit(11) /EOS/
と展開されます。EOSは文字列の終わりを示す記号です。

Watcom Fortran77版のputdec()、itoc()は下記の通り。

c putdec.for -- put decimal integer n in field width >= w
      subroutine putdec(n,w)
      integer n, w
      integer*1 chars(100)              ! MAXCHARS(100)
      integer itoc, nd

      nd = itoc(n, chars, 100)          ! MAXCHARS(100)
      i = nd + 1
      while (i .le. w) do
          call putc(32)                 ! BLANK(32)
          i = i + 1
      end while
      i = 1
      while (i .le. nd) do
          call putc(chars(i))
          i = i + 1
      end while
      return
      end
c itoc.for -- convert integer int to character string in str
      integer function itoc(int,str,size)
      integer int, size
      integer*1 str(size)
      integer abs, mod
      integer d, i, intval, j
      integer*1 digits(11), k
      data digits/'0','1','2','3','4','5','6','7','8','9',-2/ ! EOS(-2)

      intval = abs(int)
      str(1) = -2                       ! EOS(-2)
      i = 1
      loop
          i = i + 1                     ! generate digits
          d = mod(intval,10)
          str(i) = digits(d+1)
          intval = intval / 10
      until ((intval .eq. 0) .or. (i .ge. size))
      if ((int .lt. 0) .and. (i .lt. size)) then ! then sign
          i = i + 1
          str(i) = 45                   ! MINUS(45)
      endif
      itoc = i - 1
      j = 1
      while (j .lt. i) do               ! then reverse
          k = str(i)
          str(i) = str(j)
          str(j) = k
          i = i - 1
          j = j + 1
      end while
      return
      end

ここまでで、charcount、wordcount、linecountのパーツがそろいましたので、 この3つをビルドしてみます。

まず、putdec()、itoc()を作成します。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fc putdec
Open Watcom FORTRAN 77/32 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
..\src\putdec.for: 17 ステートメント, 92 バイト, 4 拡張メッセージ, 0 警告エラー, 0 エラー
        1 個のファイルを移動しました。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fo putdec
Open Watcom Library Manager Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fc itoc
Open Watcom FORTRAN 77/32 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
..\src\itoc.for: 31 ステートメント, 172 バイト, 6 拡張メッセージ, 0 警告エラー, 0 エラー
        1 個のファイルを移動しました。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fo itoc
Open Watcom Library Manager Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.

charcountをビルドします。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fc charcount
Open Watcom FORTRAN 77/32 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
..\src\charcount.for: 12 ステートメント, 69 バイト, 5 拡張メッセージ, 0 警告エラー, 0 エラー
        1 個のファイルを移動しました。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fl charcount
Open Watcom Linker Version 1.9
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
オブジェクトファイルを読込み中
ライブラリを検索中
a Windows NT character-mode 実行可能 を作成中
        1 個のファイルを移動しました。

テストします。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>..\exe\charcount
123 456 7890
^Z
13

wordcount、linecountも同様にビルドしてください。