文字から数値への変換2014年10月19日 19:22

さて、itoc()の逆の関数をctoi(c,i)を作っておきます。これは、文字配列cのi番目から数値に変換します。

RATFOR版は次の通り。

# ctoi.r4 -- convert string at in(i) to integer, increment i
      integer ctoi(in,i)
      character in(ARB)
      integer iindex
      integer d, i
      string digits "0123456789"

      while (in(i) == BLANK | in(i) == TAB)
          i = i + 1
      for (ctoi = 0; in(i) != EOS; i = i + 1) {
          d = iindex(digits, in(i))
          if (d == 0)         # non-digit
              break
          ctoi = ctoi * 10 + d - 1
          }
      return
      end

ここで、iindex(s,c)は、文字配列sの中から文字cを探しその位置を返します。Watcom Fortran77には、 同様の組み込み関数index()がありますが、引数がchracter型であるため使えません。作り直します。

RATFOR版は下記の通り。

# iindex.r4 -- find character c in string str
      integer function iindex(str,c)
      character c,str(ARB)
      
      for (iindex = 1; str(iindex) != EOS; iindex = iindex + 1)
         if (str(iindex) == c)
             return
      iindex = 0
      return
      end

ctoi()、iindex()のWatcom Fortran77版は、下記の通り。

c ctoi.for -- convert string at in(i) to integer, increment i
      integer function ctoi(in,i)
      integer*1 in(*)                   ! ARB(*)
      integer i, d
      integer iindex
      integer*1 digits(11)
      data digits/'0','1','2','3','4','5','6','7','8','9',-2/  ! EOS(-2)

      while ((in(i) .eq. 32) .or. (in(i) .eq. 9)) do ! BLANK(32) TAB(9)
          i = i + 1
      end while

      ctoi = 0
      while (in(i) .ne. -2) do          ! EOS(-2)
          d = iindex(digits,in(i))
          if (d .eq. 0) then
              exit
          end if
          ctoi = ctoi*10 + (d - 1)
          i = i + 1
      end while
      ctoi = ctoi
      return
      end
c iindex.for -- find charcter c instring str
      integer function iindex(str,c)
      integer*1 c,str(*)

      iindex = 1
      while (str(iindex) .ne. -2) do    ! EOS(-2)
         if (str(iindex) .eq. c) then
             return
         end if
         iindex = iindex + 1
      end while
      iindex = 0
      return
      end

"+"や"-"の付いた数字列を取り扱えるようにctoi()を拡張しておくのは、有意義なことです。拡張版は、次の通り。

c ctoi2.for -- (extended vertion 1) convert string at in(i) to integer, increment i
      integer function ctoi(in,i)
      integer*1 in(*)                   ! ARB(*)
      integer i
      integer iindex
      integer d, s
      integer*1 digits(11)
      data digits/'0','1','2','3','4','5','6','7','8','9',-2/    ! EOS(-2)

      while ((in(i) .eq. 32) .or. (in(i) .eq. 9)) do ! BLANK(32) TAB(9)
          i = i + 1
      end while

      ctoi = 0
      if (in(i) .eq. 43) then           ! PLUS(43)
          s = 1
          i = i + 1
      else if (in(i) .eq. 45) then      ! MINUS(45)
          s = -1
          i = i + 1
      else
          s = 1
      end if
      while (in(i) .ne. -2) do          ! EOS(-2)
          d = iindex(digits, in(i))
          if (d .eq. 0) then
              exit
          end if
          ctoi = ctoi * 10 + (d - 1)
          i = i + 1
      end while
      ctoi = s*ctoi
      return
      end