keyword input by 바죠



포트란 90 문자열 처리 라이버러리 strings.f90을 활용한 예로서 컴퓨터 프로그램에서 입력 변수 값들을 파일로 부터 읽어 들이는 일반적인 방법을 소개한다. 소위 키워드 입력 방법이다.


입력을 정확하게 하기 위한 방법으로 볼 수 있다. 자유로운 양식에서 오해의 소지를 최소화하는 방식이 동원되어야 한다.


일반적으로 입력은 컴퓨터 프로그램에서 매우 중요하다. 자주 사용하는 응용 프로그램일수록 편리한 입력 방식은 더욱더 중요한  항목이 된다.


기술적으로는 포트란 기반 문자열 추출 방식으로 분류될 수 있다.


http://incredible.egloos.com/4836430


--------------------------------------------------------------------------------------------------------------------

       USE strings, ONLY : parse,value,lowercase



       npop=50 ; nmut=100 ; ncross=100
       idiff=1 ; kcount=1000
       njter=25 ; rate=0.983912d0
       lfault=.false.
       inquire(file='csa.in',exist=lfault)
       if(.not. lfault)then
       write(6,*) 'input file csa.in is not present'
       goto 119
                       endif
       open(81,file='csa.in',form='formatted')
       do
       read(81,'(a280)',err=911,end=999) str1
       delims=' ='
       call parse(str1,delims,args,nargs)
       if(nargs >= 1)then
       if(trim(args(1)) == '#')then
       cycle
                               endif
       if(trim(args(1)) == '!')then
       cycle
                               endif
                     endif
       if(nargs >= 2)then
       if(trim(lowercase(args(1))) == 'npop')then
       call value(args(2),npop,ios)
       if(ios /= 0) npop=50
                                             endif
       if(trim(lowercase(args(1))) == 'nmut')then
       call value(args(2),nmut,ios)
       if(ios /= 0) nmut=50
                                             endif
       if(trim(lowercase(args(1))) == 'ncross')then
       call value(args(2),ncross,ios)
       if(ios /= 0) ncross=50
                                               endif
       if(trim(lowercase(args(1))) == 'njter')then
       call value(args(2),njter,ios)
       if(ios /= 0) njter=25
                                              endif
       if(trim(lowercase(args(1))) == 'idiff')then
       call value(args(2),idiff,ios)
       if(ios /= 0) idiff=1
                                              endif
       if(trim(lowercase(args(1))) == 'kcount')then
       call value(args(2),kcount,ios)
       if(ios /= 0) kcount=1000
                                               endif
       if(trim(lowercase(args(1))) == 'rate')then
       call value(args(2),rate,ios)
       if(ios /= 0) rate=0.983912d0
                                             endif
                     endif
       enddo
  911  continue
       lfault=.true.
  999  continue
       close(81)
       if(njter <= 0) njter=25
       if(npop <= 0) npop=50
       if(nmut <= 0) nmut=100
       if(ncross <= 0) ncross=100
       if(kcount <= 0) kcount=1000
       if(idiff <= 0) idiff=1
       if(rate <= 0.d0) rate=0.983912d0
       write(6,*) npop,idiff,' npop,idiff'
       write(6,*) nmut,ncross,kcount,' nmut,ncross,kcount'
       write(6,*) rate,njter,' rate,njter'
       if((nmut <= 0) .or. (ncross <= 0))then
       write(6,*) 'nonsense input for nmut and ncross'
       nmut=100 ; ncross=100


--------------------------------------------------------------------------------------------------------------------


!234567890
       USE strings, ONLY : parse,value
       implicit none
       integer i,j,k,ivar
       real*8 avar,bvar,cvar
       logical lvar
       integer ios,nargs
       character*280 fname
       character*280 str1,args(40)
       character*20 delims
       logical lfault

       lfault=.false.
       open(1,file='input',form='formatted')
       do i=1,1000000
       read(1,'(a280)',err=911,end=999) str1
       delims=' '
       call parse(str1,delims,args,nargs)
       if(nargs >= 2)then
       call value(args(2),ivar,ios)
                     endif
       enddo
  911  continue
       lfault=.true.
  999  continue
       close(1)
       stop
       end
!   ifort -CB -check all -warn interface -assume realloc_lhs
!234567890


--------------------------------------------------------------------------------------------------------------------


       USE strings, ONLY : parse,value,lowercase
       implicit none
       integer ios,nargs
       character*20 delims
       character*280 str1,args(40)
       logical lfault


       lfault=.false.
       open(81,file='ann.in',form='formatted')
       do
       read(81,'(a280)',err=911,end=999) str1
       delims=' ='
       call parse(str1,delims,args,nargs)
       if(nargs >= 1)then
       if(trim(args(1)) == '#') cycle
       if(trim(args(1)) == '!') cycle
                     endif
       if(nargs >= 2)then
       if(trim(lowercase(args(1)))  == 'nsize')then
       call value(args(2),ns,ios) ; if(ios /= 0) ns=10
                                               endif
       if(trim(lowercase(args(1)))  == 'ndata')then
       call value(args(2),ndata,ios) ; if(ios /= 0) ndata=1000
                                               endif
       if(trim(lowercase(args(1)))  == 'ntrain')then
       call value(args(2),ntrain,ios) ; if(ios /= 0) ntrain=ndata/2
                                               endif
       if(trim(lowercase(args(1)))  == 'ndeep')then
       call value(args(2),ndeep0,ios) ; if(ios /= 0) ndeep0=10
                                               endif
       if(trim(lowercase(args(1)))  == 'mpsoupdate')then
       call value(args(2),mpsoupdate0,ios) ; if(ios /= 0) mpsoupdate0=20
                                                    endif
       if(trim(lowercase(args(1)))  == 'nfactor')then
       call value(args(2),nfactor0,ios) ; if(ios /= 0) nfactor0=200
                                                    endif
       if(trim(lowercase(args(1)))  == 'ntimes')then
       call value(args(2),ntimes,ios) ; if(ios /= 0) ntimes=20
                                                    endif
       if(trim(lowercase(args(1)))  == 'npopulation')then
       call value(args(2),npop0,ios) ; if(ios /= 0) npop0=20
                                                     endif
       if(trim(lowercase(args(1)))  == 'ncrossover')then
       call value(args(2),ncrossover0,ios) ; if(ios /= 0) ncrossover0=2000
                                                    endif
       if(trim(lowercase(args(1)))  == 'nmutation')then
       call value(args(2),nmutation0,ios) ; if(ios /= 0) nmutation0=4000
                                                   endif
       if(trim(lowercase(args(1)))  == 'amplitude')then
       call value(args(2),amp0,ios) ; if(ios /= 0) amp0=10.d0
                                                   endif
       if(trim(lowercase(args(1)))  == 'delx')then
       call value(args(2),delx0,ios) ; if(ios /= 0) delx0=1.d-8
                                              endif
       if(trim(lowercase(args(1)))  == 'lshuffling')then
       if(trim(lowercase(args(2))) == '.true.') lshuffling=.true.
       if(trim(lowercase(args(2))) == 'true') lshuffling=.true.
       if(trim(lowercase(args(2))) == 't') lshuffling=.true.

                                                    endif
       if(trim(lowercase(args(1)))  == 'lpso')then
       if(trim(lowercase(args(2))) == '.true.') lpso=.true.
       if(trim(lowercase(args(2))) == 'true') lpso=.true.
       if(trim(lowercase(args(2))) == 't') lpso=.true.
                                              endif
       if(trim(lowercase(args(1)))  == 'lcsa')then
       if(trim(lowercase(args(2))) == '.true.') lcsa=.true.
       if(trim(lowercase(args(2))) == 'true') lcsa=.true.
       if(trim(lowercase(args(2))) == 't') lcsa=.true.
                                              endif
       if(trim(lowercase(args(1)))  == 'lbfgs')then
       if(trim(lowercase(args(2))) == '.true.') lbfgs=.true.
       if(trim(lowercase(args(2))) == 'true') lbfgs=.true.
       if(trim(lowercase(args(2))) == 't') lbfgs=.true.
                                               endif
                     endif

       enddo
  911  continue
       lfault=.true.
  999  continue
       close(81)




--------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------


!234567890
       USE strings, ONLY : parse,value
       implicit none
       integer i,j,k,ivar
       real*8 avar,bvar,cvar
       logical lvar
       integer ios,nargs
       character*280 fname
       character*280 str1,args(40)
       character*20 delims
       logical lfault

       lfault=.false.
       open(1,file='input',form='formatted')
       do i=1,1000000
       read(1,'(a280)',err=911,end=999) str1
       delims=' '
       call parse(str1,delims,args,nargs)
       if(nargs >= 2)then
       call value(args(2),ivar,ios)
                     endif
       enddo
  911  continue
       lfault=.true.
  999  continue
       close(1)
       stop
       end
!   ifort -CB -check all -warn interface -assume realloc_lhs
!234567890



---------------------------------------------------------------------------------------------


!234567890

       program incard

       USE strings, ONLY : parse,value,lowercase,is_digit

       implicit none

       integer i,j,k

       real*8 tmp

       character*1 ch1

       character*28000 str2

       character*280 args2(4000)

       character*280 stdname,otname

       logical lfault_stdout,lfault,lfault1

       integer ios,nargs,nargs2

       character*280 str1

       character*20 args(40)

       character*20 delims


       lfault=.false.

       str2=' '

       do

       str1=' '

       read(5,'(a280)',err=811,end=899) str1

       i=0

       do k=1,280

       if(str1(k:k) == '=' .or. str1(k:k) == ':') i=i+1

       enddo

       if(i == 0) cycle

       delims=' '

       call parse(str1,delims,args,nargs)

       if(nargs > 0)then

       ch1=args(1)(1:1)

       if(ch1 == '!' .or. ch1 == '#') cycle

       j=nargs

       do i=2,nargs

       ch1=args(i)(1:1)

       if(ch1 == '!' .or. ch1 == '#')then

       j=i-1

       exit

                                     endif

       enddo

       nargs=j

       do i=1,nargs

       str2=trim(str2)//trim(args(i))//' '

       enddo

                    endif

       str2=trim(str2)//' '

       enddo

  811  continue

       lfault=.true.

  899  continue

       write(6,*) trim(str2)

       delims=';'

       call parse(str2,delims,args2,nargs2)

       do j=1,nargs2

       str1=args2(j)

       delims='=:'

       call parse(str1,delims,args,nargs)

       if(nargs == 1 .or. nargs == 2)then

       ch1=args(1)(1:1)

       if(      is_digit(ch1))then

       write(6,*) 'input error'

                              stop

                              endif

                                     endif

       if(nargs == 0 .or. nargs == 1) cycle

       if(nargs == 2)then
       ch1=args(2)(1:1)
       if(.not. is_digit(ch1))then
       if(lowercase(args(2)) == '.true.')  args(2)='T'
       if(lowercase(args(2)) == '.true')   args(2)='T'
       if(lowercase(args(2)) == 'true.')   args(2)='T'
       if(lowercase(args(2)) == 'true')    args(2)='T'
       if(lowercase(args(2)) == 't')       args(2)='T'
       if(lowercase(args(2)) == '.t.')     args(2)='T'
       if(lowercase(args(2)) == 't.')      args(2)='T'
       if(lowercase(args(2)) == '.t')      args(2)='T'
       if(lowercase(args(2)) == '.false.') args(2)='F'
       if(lowercase(args(2)) == '.false')  args(2)='F'
       if(lowercase(args(2)) == 'false.')  args(2)='F'
       if(lowercase(args(2)) == 'false')   args(2)='F'
       if(lowercase(args(2)) == 'f')       args(2)='F'
       if(lowercase(args(2)) == 'f.')      args(2)='F'
       if(lowercase(args(2)) == '.f.')     args(2)='F'
       if(lowercase(args(2)) == '.f')      args(2)='F'
       if(lowercase(args(2)) == 'f.')      args(2)='F'
                              endif
       ch1=args(2)(1:1)
       if(.not. is_digit(ch1))then
       write(6,*) trim(args(1)),' ', trim(args(2))
                              endif
       if(      is_digit(ch1))then
       if(lowercase(args(1)) == 'i')then
       call value(args(2),k,ios)
       write(6,*) 'i',k
                                     endif
       if(lowercase(args(1)) == 'j')then
       call value(args(2),k,ios)
       write(6,*) 'j',k
                                     endif
       if(lowercase(args(1)) == 'k')then
       call value(args(2),k,ios)
       write(6,*) 'k',k
                                     endif
                              endif
                     endif
       enddo
       end program incard
!234567890




---------------------------------------------------------------------------------------------



핑백

덧글

댓글 입력 영역

최근 포토로그



MathJax