数字出力putdec()
2014-10-12


先に出てきた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()を作成します。



続きを読む

[コンピューター]
[RATFOR]

コメント(全50件)
※コメントの受付件数を超えているため、この記事にコメントすることができません。


記事を書く
powered by ASAHIネット